Closed muk3shb closed 2 years ago
Hi @muk3shb, I've just uploaded a new release 2.0.8 with this bug fixed. Now the bot will reply always in the chat where message was sent, whether it comes from a user or a group.
You need to take care with command ( words preceded by "/") coming from group, because Telegram in that case add automatically the id of group like this for example: /my_command@myGroupName
You need to handle this messages in a different way than the lightBot.ino example do.
For example a quick mod would be using indexOf()
instead equals()
or storing the group name in a variable.
if (msgText.indexOf("/light_on") > -1) { // if the received message contains /light_on"...
digitalWrite(LED, LOW); // turn on the LED
myBot.sendMessage(msg, "Light is now ON"); // notify the sender
}
else if (msgText.indexOf("/light_off") > -1) { // if the received message contains "/light_off"...
digitalWrite(LED, HIGH); // turn off the led
myBot.sendMessage(msg, "Light is now OFF"); // notify the sender
}
Hi @muk3shb, I've just uploaded a new release 2.0.8 with this bug fixed. Now the bot will reply always in the chat where message was sent, whether it comes from a user or a group.
You need to take care with command ( words preceded by "/") coming from group, because Telegram in that case add automatically the id of group like this for example: _/my_command@myGroupName_
You need to handle this messages in a different way than the lightBot.ino example do. For example a quick mod would be using
indexOf()
insteadequals()
or storing the group name in a variable.if (msgText.indexOf("/light_on") > -1) { // if the received message contains /light_on"... digitalWrite(LED, LOW); // turn on the LED myBot.sendMessage(msg, "Light is now ON"); // notify the sender } else if (msgText.indexOf("/light_off") > -1) { // if the received message contains "/light_off"... digitalWrite(LED, HIGH); // turn off the led myBot.sendMessage(msg, "Light is now OFF"); // notify the sender }
Sir, I executed this, it is working fine. I have few questions to ask.. Here they are..
How can we disregard other commands while allowing the bot to work with only one command ?
for example - if /High is input than the output is high and i get the replied msg like "Something is high" However if i write anything other than "/High" than the BOT should not reply or accept other command.
My concern - If someone who knows the command other than the user then the BOT will work for the actual user and the user who knows the command.
The BOT works as universal so i think there's something which is fixed on the working side of the bot.
When your bot receive a new message, inside the struct TBMessage msg
there are all informations about the sender.
You can check for a specific userid or even for a specific username.
In addition you could ask user to confirm command with a password, check this example as a starting point: OTA_password.ino
if (myBot.getNewMessage(msg)) {
Serial.print("\nMessage received from: ");
Serial.print(msg.sender.id);
Serial.print("@");
Serial.println(msg.sender.username);
......
This is Compiling well and on the output side, Only Default case is being executed. How can i call for "insert pw" in message document case. or is there any other method ? i didnt understood the working Of Messagedocument case, i only half understood it, how to execute for calling for pw with text of some specific text ?
Here in the code I have just wrote tha passkey, pley don't confused i haven't assigned any passkey yet.
/Code_removed
MessageDocument type is when you receive a binary file as attached document (as in case for a new firmware). If you are working only with text message, simply apply the same workflow with text received. If you set force_reply property, the user will be prompted to respond as reply to your specific message. In this way you can handle better the next reply wich will be of MessageReply type containing the password string
......
MessageType msgType = msg.messageType;
String msgText = msg.text;
Serial.print("\nText message received: ");
Serial.println(msgText);
switch (msgType) {
// Normal text message
case MessageText :
if (msgText.indexOf("/askMePassword") > -1) {
msg.force_reply = true;
myBot.sendMessage(msg, "Please insert password", "");
}
break;
// Reply to a message
case MessageReply:
tgReply = msg.text;
// User has confirmed with right password, replying to my message
if ( msgText .equals("your password) ) {
Serial.println("Password correct");
}
break;
}
Thanks sir, i got the refrence and this is working fine. However, Since i have defined the lightbot outside switch structure, it is easily accessible. how i make the loop end by turning lightbot on and reverting back to first step or welcome msg step ? and how can i add /start to start the loop everytime ?. The Bot is working pretty well.. just this minor details will enhance it.
switch (msgType) {
// Normal text message
case MessageText :
if (msgText.indexOf("/Click_here") > -1) {
msg.force_reply = true;
myBot.sendMessage(msg, "Please insert password", "");
}
break;
// Reply to a message
// Reply to a message
case MessageReply:
tgReply = msg.text;
// User has confirmed with right password, replying to my message
if ( msgText.equals("Badu") )
{
Serial.println("Password correct");
String reply;
reply = "Welcome " ;
reply += msg.sender.username;
reply += ".\nTry /light_on or /light_off ";
myBot.sendMessage(msg, reply);
}
break;
}
if (msgText.indexOf("/light_on") > -1) { // if the received message contains /light_on"... digitalWrite(LED, HIGH); // turn on the LED myBot.sendMessage(msg, "Light is now ON"); // notify the sender } else if (msgText.indexOf("/light_off") > -1) { // if the received message contains "/light_off"... digitalWrite(LED, LOW); // turn off the led myBot.sendMessage(msg, "Light is now OFF"); // notify the sender }
Thankyou So much!! I found that code on the example and with little changes, it is working!! i have not changed anything just very minute changes. There are no errors but the message is continuously generating. Here is the code..
// i am reverifying the chatid by using simple operator and it is working!! but the bot is continuosly generating the message. the code that i have mentioned will work fine
Originally posted by @muk3shb in https://github.com/cotestatnt/AsyncTelegram2/issues/41#issuecomment-1018844366