Answer the question
In order to leave comments, you need to log in
How to make the bot reply to messages with phrases from an array?
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
phrases={"hello":"hello","how are you?":"good, how about you?","good":"that's great!"," bad":"cheer up"}
def get_answer(key,phrases):
return phrases.get(key)
def start(bot,update):
print("Called/start")
bot.sendMessage(update.message.chat_id, text= 'Hi.')
def talk_to_me(bot,update):
print('Message received: %s' % update.message.text)
key=input(":")
bot.sendMessage(update.message.chat_id, text=update .message.get_answer(key,phrases))
def bot_work():
updater=Updater("286223894:AAFR57Bru4f7xEcEBTQ4Q12pVw8SJXM0uU8")
dp=updater.dispatcher
dp.add_handler(CommandHandler('start',start))
dp.add_handler(MessageHandler([Filters.text], talk_to_me))
updater.start_polling()
updater.idle()
if __name__==("__main__"):
bot_work( )
Moreover, in order to answer, I only use the get_answer function. I will be very grateful.
PS Bot for telegram. The get_answer function can be changed. Thanks again
Answer the question
In order to leave comments, you need to log in
def hello_answer(bot,update):
bot.sendMessage(update.message.chat_id, text = "%s" % get_answer(update.message.text))
def get_answer(ask, answers = phrases):
ask = ask.lower( )
ask = ask.strip()
ask = re.sub('\W*$','',ask)
if answers.get(ask) == None:
return 'I don't understand...'
else:
return answers. get(ask)
Indentation, I think, you will put it where you need it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question