I have been experimenting with Telepot to read messages in Telegram and I am trying to have the bot read a user message msg['text']
. If I just print msg['text']
it echo's the user's message, so I am trying to assign it to a variable message
and then have the bot reply in a certain way if a specific string was in the user's message. Whatever I input, it always prints the "Ack was found in the message"
even if "Ack" or "ack"
isn't in the message. I have tried further specifying by making another if statement by saying
if 'ack' or "Ack" not in message:
bot.sendMessage(chat_id, "Ack not found")
instead of an else
, but this will just now print both if statement strings with whatever I input. Below is the full code:
import sys
import time
import telepot
from telepot.loop import MessageLoop
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
print(content_type, chat_type, chat_id)
message = msg['text']
if 'ack' or "Ack" in message:
bot.sendMessage(chat_id, "Ack was found in the messsage")
else:
bot.sendMessage(chat_id, "Ack not found")
MessageLoop(bot, handle).run_as_thread()
print ('Listening ...')
# Keep the program running.
while True:
time.sleep(10)
I am following this documentation here: https://telepot.readthedocs.io/en/latest/#receive-messages
SOLUTION: Updated the code to the answer suggested by @mkrieger1
if message in {"ack","Ack"}:
bot.sendMessage(chat_id, "Ack was found in the messsage")
if message not in {"ack","Ack"}:
bot.sendMessage(chat_id, "Ack not found")