简体   繁体   中英

How to get message updates from the user after the bot message telegram bot

So I'm new at making a telegram bot, I want to have like the botfather :

user: create bot

bot: provide name for your bot

user: foo bot

then the user text is used in the future.

I have a simple sample bot which defines a word that the user provide

user:/define internet

bot: defines the term

but I want to recreate it to

user:/define

bot: please send the word you want to define

user: internet

bot: defines the term

user: ....

I'm using pyTelegramBotAPI

Here is my code

@bot.message_handler(commands=['define'])
def dictionary(message):
user_message = message.text
msg = user_message.split(' ')
if len(msg) != 1:
    try:
        word = msg[1]
        botmsg = bot.send_message(message.chat.id, 'Finding the word in the dictionary')
        resp = requests.get(DICT_URL + word)
        respData = resp.json()
        pof = respData[0]['meanings'][0]['partOfSpeech']
        print(pof)
        defi = respData[0]['meanings'][0]['definitions'][0]['definition']
        print(defi)
        bot.send_message(message.chat.id, text=f'*Word:* {word} \n\n *Part Of Speech: *{pof} \n\n *Definition: *{defi}', parse_mode='Markdown')
        bot.delete_message(message.chat.id, botmsg.message_id)
    except:
        bot.send_message(message.chat.id, "Couldn't find in the dictionary.")
else:
    bot.send_message(message.chat.id, 'Please input a word to define.')

I've found the answer thanks to @furas who helped with his comments

incase someone has the same problem with me here's the code.

@bot.message_handler(commands=['define'])
def send_welcome(message):
    msg = bot.reply_to(message, "What word you want to define?")
    bot.register_next_step_handler(msg, define)


def define(message):
    chat_id = message.chat.id
    word = message.text
    bot.send_message(chat_id, f'the word you want to define is {word}')

bot.enable_save_next_step_handlers(delay=2)
bot.load_next_step_handlers()

bot.infinity_polling()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM