I have created a simple telegram bot that will take an input from a user and perform some operations and return the result to the user.
However, I want to add a menu where based on the menu option will call a specific function and return the result to the user. Each operation takes from 0 to 60 seconds and the bot is used by more than one user.
I have a few concern. First, how to make sure that the bot will store the entered value before the menu choice. Second, since the operation takes N seconds, how can I make sure that the each user will get its searched value instead of getting the result of another user?
Here is my code so far:
!/usr/bin/env python3.8
api_id = 12345
api_hash = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
bot_token = 'XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
############################### Bot ############################################
from telethon.sync import TelegramClient, events,Button
from telethon import functions, types
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
def first_function(value):
#DO SOME OPERATION
return
def second_function(value):
#DO SOME OPERATION
return
bot = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
with bot:
@bot.on(events.NewMessage)
async def handler(event):
value=event.message.message
sender_id=event.message.peer_id.user_id
username = await bot.get_entity(sender_id)
#if menu option is 1
# first_function(value)
#else menu option is 2
#second_function(value)
await bot.send_message(username, value)
bot.run_until_disconnected()
2
Answers
To add a menu to your Telegram bot.
Here is add a menu code:
Make a callback:
Function send_message() write:
Two functions add in your code:
you should checkout the documentation here
In the documentation, you can also refer examples.
example you should look for is here.
Short example of adding buttons :