I want to create a telegram bot that, after seeing the command /define, asks for the word.
I want to extract the word sent by the user after the bot asks for it. How do I do it?
import telegram
from telegram.ext import Updater
from telegram.ext import MessageHandler, Filters
from telegram.ext import CommandHandler
updater = Updater(token='******************')
dispatcher = updater.dispatcher
def define(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Enter word")
word = '''get content of following message'''
definition = get_definition(word)
bot.send_message(chat_id=update.message.chat_id, text=definiton)
definition_handler = CommandHandler('define', define)
dispatcher.add_handler(definition_handler)
updater.start_polling()
2
Answers
Actually, the token is the only thing you need to create any bot you want. The codes for the bot like yours should follow the same logic structure:
Okay, each bot requires a
message_handler
to process the incoming information.In your case, it is a command that triggers the bot to ask for a word to extract into a list. If you do not define
bot.register_next_step_handler()
, this command will not do any action at all (except the fact it asks for a word).The function
extract_msg()
appends the next word written by a user and prints out themsg
list into your console.The function
main_loop()
runs the bot until suspension and provokes it to idle for a second after each word extraction. To stop the bot, press Ctrl + C.I hope that suffices. The next step would be to track the person who types
/define
or/Define
and extract his/her word request. Also, it would be better to makemsg
list more descriptive, or implement absolutely different extraction method. This one is simply informative and hardly applicable in practice.I fixed an error in when calling stderr: