Return 如果在 C++ 中命令错误,则显示错误消息
Return error message if the command is wrong in C++
我正在做这个项目,我设置了一些我的 TelegramBot 可以执行的命令。如果编写的命令错误,我想添加一条错误消息。这是代码:
void handleNewMessages(int numNewMessages){
Serial.print("Handle New Messages: ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++){
// Chat id of the requester
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != chatId){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Print the received message
String text = bot.messages[i].text;
Serial.println(text);
String fromName = bot.messages[i].from_name;
if (text == "/FlashOn") {
flashState = HIGH;
digitalWrite(LED, flashState);
}
if (text == "/FlashOff") {
flashState = LOW;
digitalWrite(LED, flashState);
}
if (text == "/photo") {
sendPhoto = true;
Serial.println("New photo request");
}
if (text == "/PIRON"){
PirControler = 1;
bot.sendMessage(chatId, "PIR Sensor is ON, you will get a notification if a motion is detected.", "");
}
if (text == "/PIROFF"){
PirControler = 0;
bot.sendMessage(chatId, "PIR sensor is OFF, you will no longer receive any notification.", "");
if (text == "/start"){
String welcome = "Hi sir, here are the commands I can execute for you :\n";
welcome += "/Photo : Take a new photo.\n";
welcome += "/FlashOn : Turns LED On.\n";
welcome += "/FlashOff : Turns LED off\n";
welcome += "/PIRON : Activate your PIR sensor.\n";
welcome += "/PIROFF : Shut your PIR sensor down.\n";
// welcome += "/readings : request sensor readings\n\n";
welcome += "You'll receive a photo whenever motion is detected.\n";
bot.sendMessage(chatId, welcome, "Markdown");
}
/*else {
bot.sendMessage(chatId, "I don't understand, sorry. Refer to the commands I showed you above.", "");
} **(Here is the message that I'd like to add)**
}
}
但是,通过尝试添加最后一行,
/*else {
bot.sendMessage(chatId, "I don't understand, sorry. Refer to the commands I showed you above.", "");
}*/
它 returns 除了“/start”命令之外的所有正确命令都是错误的。
任何关于我如何做到这一点的想法将不胜感激:)
让我们缩小示例
if (text == "/FlashOn") {
do stuff
}
if (text == "/start"){
do stuff
}
else {
report error
}
此代码将始终测试 if (text == "/start")
,而不管 if (text == "/FlashOn")
的结果如何,如果 text
是“/FlashOn”,则它不能是“/start”并将执行 else
并打印错误信息。
解决方案
if (text == "/FlashOn") {
do stuff
}
else if (text == "/start"){
do stuff
}
else {
report error
}
现在 if (text == "/start")
将不会被测试并且 else
案例将不会 运行 if text == "/FlashOn"
.
如果完全缩进和括起来会是什么样子:
if (text == "/FlashOn") {
do stuff
}
else {
if (text == "/start"){
do stuff
}
else {
report error
}
}
我正在做这个项目,我设置了一些我的 TelegramBot 可以执行的命令。如果编写的命令错误,我想添加一条错误消息。这是代码:
void handleNewMessages(int numNewMessages){
Serial.print("Handle New Messages: ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++){
// Chat id of the requester
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != chatId){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Print the received message
String text = bot.messages[i].text;
Serial.println(text);
String fromName = bot.messages[i].from_name;
if (text == "/FlashOn") {
flashState = HIGH;
digitalWrite(LED, flashState);
}
if (text == "/FlashOff") {
flashState = LOW;
digitalWrite(LED, flashState);
}
if (text == "/photo") {
sendPhoto = true;
Serial.println("New photo request");
}
if (text == "/PIRON"){
PirControler = 1;
bot.sendMessage(chatId, "PIR Sensor is ON, you will get a notification if a motion is detected.", "");
}
if (text == "/PIROFF"){
PirControler = 0;
bot.sendMessage(chatId, "PIR sensor is OFF, you will no longer receive any notification.", "");
if (text == "/start"){
String welcome = "Hi sir, here are the commands I can execute for you :\n";
welcome += "/Photo : Take a new photo.\n";
welcome += "/FlashOn : Turns LED On.\n";
welcome += "/FlashOff : Turns LED off\n";
welcome += "/PIRON : Activate your PIR sensor.\n";
welcome += "/PIROFF : Shut your PIR sensor down.\n";
// welcome += "/readings : request sensor readings\n\n";
welcome += "You'll receive a photo whenever motion is detected.\n";
bot.sendMessage(chatId, welcome, "Markdown");
}
/*else {
bot.sendMessage(chatId, "I don't understand, sorry. Refer to the commands I showed you above.", "");
} **(Here is the message that I'd like to add)**
}
}
但是,通过尝试添加最后一行,
/*else {
bot.sendMessage(chatId, "I don't understand, sorry. Refer to the commands I showed you above.", "");
}*/
它 returns 除了“/start”命令之外的所有正确命令都是错误的。
任何关于我如何做到这一点的想法将不胜感激:)
让我们缩小示例
if (text == "/FlashOn") {
do stuff
}
if (text == "/start"){
do stuff
}
else {
report error
}
此代码将始终测试 if (text == "/start")
,而不管 if (text == "/FlashOn")
的结果如何,如果 text
是“/FlashOn”,则它不能是“/start”并将执行 else
并打印错误信息。
解决方案
if (text == "/FlashOn") {
do stuff
}
else if (text == "/start"){
do stuff
}
else {
report error
}
现在 if (text == "/start")
将不会被测试并且 else
案例将不会 运行 if text == "/FlashOn"
.
如果完全缩进和括起来会是什么样子:
if (text == "/FlashOn") {
do stuff
}
else {
if (text == "/start"){
do stuff
}
else {
report error
}
}