Closed goguelnikov closed 9 months ago
Hello!
So if you put a code like this you won't get any answer like I do?
if (text == "test message") { bot.sendMessage(chat_id, "Test message recieved", ""); }
Please consider to share your code. At least a function which handleNewMessages
Hello, Thanks for your feedback. is it what you get when you invite your bot in the group chat? that's what I a trying to do... the bot should reply in the group the message comes from
here is my code
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(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 != CHAT_ID){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Print the received message
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
}
}
it's the standard example. when the bot starts, it posts to the correct group (the one I created with me and the bot). I got "Bot Started" then when I post messages to the group, I have no reaction, except with "/". For example
got response
handleNewMessages
1
/test message
but when I chat directly with the bot, I got: Unauthorized user
No idea? Can anybody help?
Unauthorized user
Well... It's because you have a condition in your code. If chat_id no equal CHAT_ID then bot .send Message "Unauthorized user"
if (chat_id != CHAT_ID){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
No idea? Can anybody help?
Just to double check: Do you have a call of handleNewMessages() from void loop() inside for loop and?
And again! Add this code in your handleNewMessages function inside for loop to answer on text "test message"
if (text == "test message") {
bot.sendMessage(chat_id, "Test message recieved", "");
}
Or make it echo bot.
bot.sendMessage(chat_id, text , "");
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(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 != CHAT_ID){
// bot.sendMessage(chat_id, "Unauthorized user", "");
// continue;
//}
// Print the received message
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
// answer on text "test message"
if (text == "test message") {
bot.sendMessage(chat_id, "Test message recieved", "");
}
// echo
bot.sendMessage(chat_id, text , "");
}
}
I used the standard handleNewMessages you can check the code here : https://github.com/goguelnikov/TelegramMinitelChat/blob/main/src/main.cpp the part : if (chat_id == CHAT_ID){ bot.sendMessage(chat_id, text, ""); } else { bot.sendMessage(chat_id, "Unauthorized", ""); } is commented the handleNewMessages is indeed inside void loop() I'll try you're coode (but I think I did already)
hello here is my code: void handleNewMessages(int numNewMessages) { //Serial.println(F("handleNewMessages")); Serial.print(F("NB message: ")); Serial.println(String(numNewMessages));
for (int i=0; i<numNewMessages; i++) { // Chat id of the requester Serial.print(F("From Chat_ID: ")); String chat_id = String(bot.messages[i].chat_id); Serial.println(chat_id); Serial.print(F("Message: ")); String text = bot.messages[i].text; Serial.println(text); Serial.print(F("From_name: ")); String from_name = bot.messages[i].from_name;
if (chat_id == CHAT_ID){
bot.sendMessage(chat_id, text, "");
} else {
bot.sendMessage(chat_id, "Unauthorized", "");
}
} }
here is my complete analysis : If I put the bot in a group with group ID XYZ (Chat_ID)
If I chat directly withe the bot (Chat_ID = bot_id)
Have you disable Group Privacy in your bot settings?
go to @BotFather send /mybots select your bot click Bot Settings click Group Privacy (it should go like "Privacy mode is disabled for your_bot_name_bot")
REMOVE your bot from the group Add your bot to the group
Please read https://core.telegram.org/bots/features#privacy-mode
yeah !!! it seems to be working ! nice ! this was the problem thank you !
Hello, Amazing work Brian, thank you so much! The library is really nice and easy to use! I have a question: I can have the bot to get the message / reply when I chat with it directly one to one. I'd like my bot to be part of a Telegram group and be able to get the message from this group and respond to this group. The ESP32 only get triggered when I put "/" at the beginning of the message in a group... and in this case, it does not reply to the group… Do you have any idea? Best