1

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.')
furas
  • 134,197
  • 12
  • 106
  • 148
ciumai
  • 21
  • 1
  • 6
  • `telebot` should have function which catchs/handles all messages. In `define()` you could set global variable `define_active = True`, and next in function which catchs/handles all message check `if define_active is True: .... catch word "internet" ... display message for term, set global varialbe define_active=False and term_active=True`. And in the same function should be `if term_active is True: ... catch definiton for word "internet"` – furas Jun 07 '22 at 02:13
  • see [next-step-handler](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/step_example.py) - it set which function should get next message from user. This way `/define` would set next handler to get `internet` and this handler would sent next handler to get `description for this `internet` – furas Jun 07 '22 at 02:27
  • @furas oh i was skipping that example cuz i think it is not the answer lol, thank you so much and have a great day! – ciumai Jun 07 '22 at 02:47

1 Answers1

0

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()
Redox
  • 9,321
  • 5
  • 9
  • 26
ciumai
  • 21
  • 1
  • 6