shurillu / CTBot

A simple (and easy to use) Arduino Telegram BOT Library for ESP8266/ESP32
MIT License
147 stars 34 forks source link

Conditional logic is not working as expected #29

Open indirwanadi opened 4 years ago

indirwanadi commented 4 years ago

conditional_false

Hello, sir. i want to add conditional by provide : 1. type 'feed' to feed cat 2. 'status' to check food availability and 3. 'ip' to display device IP Address and when i type anything else bot send help message. but it always show help message especially when i type 'feed', 'status', and 'ip' text instead.

here the sketch list:

// if there is an incoming message... if (myBot.getNewMessage(msg)) { if(msg.messageType == CTBotMessageText) { if (msg.text.equalsIgnoreCase("feed")) { // if the received message is "LIGHT ON"... digitalWrite(led, LOW); // turn on the LED (inverted logic!) for (pos = 8; pos <= 168; pos += 1) { // goes from 8 degrees to 168 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(20); // waits 20ms for the servo to reach the position } for (pos = 168; pos >= 8; pos -= 1) { // goes from 168 degrees to 8 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(20); // waits 20ms for the servo to reach the position } digitalWrite(buzer, HIGH); delay(200); digitalWrite(buzer, LOW); delay(200); digitalWrite(buzer, HIGH); delay(200); digitalWrite(buzer, LOW); delay(200); myBot.sendMessage(msg.sender.id, "Kucing sudah diberi makan"); // notify the sender Serial.println("cat already fed >_<"); } if (msg.text.equalsIgnoreCase("status")) { calcRemainingFood(); myBot.sendMessage(msg.sender.id, "Sisa ketersediaan makanan: " + String(percentageFood) + " % (Distance to food: " + String(distance) + " cm). ", ""); Serial.println("displaying food stock:"); } if (msg.text.equalsIgnoreCase("ip")) { String feederIP = WiFi.localIP().toString(); myBot.sendMessage(msg.sender.id, "IP Address perangkat: " + (feederIP), ""); } else { // the user write anithing else --> show a hint message myBot.sendMessage(msg.sender.id, "coba ketik 'feed' untuk memberi makan, dan 'status' untuk mengecek sisa makanan"); } //if (msg.text.equalsIgnoreCase("/help")) { //String reply; //reply = (String)"Halo " + msg.sender.username + (String)". ketik 'feed' untuk memberi makan, dan 'status' untuk mengecek sisa makanan"; //myBot.sendMessage(msg.sender.id, reply); //} } //else if (msg.messageType == CTBotMessageText) { //} } // wait 500 milliseconds delay(500);

shurillu commented 4 years ago

Hello indirwanadi, this space is for issues related to the library. In other word, if you have issues related to your code, you should not use this tool. Anyway, every time you put a source code here, please use the <> button above and indent the code: it will be more easy to read. The problem that you has encountered is due to the if() else block: all the conditions (feed, status, ip) are mutually exclusive, so you have to put an else, like this:

    // if there is an incoming message...
    if (myBot.getNewMessage(msg)) {
        if (msg.messageType == CTBotMessageText) {
            if (msg.text.equalsIgnoreCase("feed")) { // if the received message is "LIGHT ON"...
                digitalWrite(led, LOW); // turn on the LED (inverted logic!)
                for (pos = 8; pos <= 168; pos += 1) { // goes from 8 degrees to 168 degrees
                // in steps of 1 degree
                    myservo.write(pos); // tell servo to go to position in variable 'pos'
                    delay(20); // waits 20ms for the servo to reach the position
                }
                for (pos = 168; pos >= 8; pos -= 1) { // goes from 168 degrees to 8 degrees
                    myservo.write(pos); // tell servo to go to position in variable 'pos'
                    delay(20); // waits 20ms for the servo to reach the position
                }
                digitalWrite(buzer, HIGH);
                delay(200);
                digitalWrite(buzer, LOW);
                delay(200);
                digitalWrite(buzer, HIGH);
                delay(200);
                digitalWrite(buzer, LOW);
                delay(200);
                myBot.sendMessage(msg.sender.id, "Kucing sudah diberi makan"); // notify the sender
                Serial.println("cat already fed >_<");
            }
            else if (msg.text.equalsIgnoreCase("status")) {
                calcRemainingFood();
                myBot.sendMessage(msg.sender.id, "Sisa ketersediaan makanan: " + String(percentageFood) +
                    " % (Distance to food: " + String(distance) + " cm). ", "");
                Serial.println("displaying food stock:");
            }
            else if (msg.text.equalsIgnoreCase("ip")) {
                String feederIP = WiFi.localIP().toString();
                myBot.sendMessage(msg.sender.id, "IP Address perangkat: " + (feederIP), "");
            }
            else {
                // the user write anithing else --> show a hint message
                myBot.sendMessage(msg.sender.id, "coba ketik 'feed' untuk memberi makan, dan 'status' untuk mengecek sisa makanan");
            }
            //if (msg.text.equalsIgnoreCase("/help")) {
            //String reply;
            //reply = (String)"Halo " + msg.sender.username + (String)". ketik 'feed' untuk memberi makan, dan 'status' untuk mengecek sisa makanan";
            //myBot.sendMessage(msg.sender.id, reply);
            //}
        }
        //else if (msg.messageType == CTBotMessageText) {
        //}
    }
    // wait 500 milliseconds
    delay(500);

This changes should resolve your issue: let me know if it works (and in that case, please close the issue). Cheers

Stefano