G
G
George1232016-10-01 00:47:47
Python
George123, 2016-10-01 00:47:47

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

2 answer(s)
Y
YO_N, 2016-10-03
@Georgy123


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.

B
BogBel, 2016-10-03
@BogBel

to select a random element in an array, use

import random
data = ['answ1', 'answ2', 'answ3', 'answ4', 'answ5']
>>>random.choice(data)
answ1
>>>random.choice(data)
answ4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question