skip to Main Content

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


  1. To add a menu to your Telegram bot.
    Here is add a menu code:

    menu_keyboard = InlineKeyboardMarkup([[
            InlineKeyboardButton("Option 1", callback_data='1'),
            InlineKeyboardButton("Option 2", callback_data='2')
        ]])
    

    Make a callback:

    async def callback_query_handler(event):
        callback_query = event.data
        if callback_query == '1':
            await first_function(value)
        elif callback_query == '2':
            await second_function(value)
    

    Function send_message() write:

    await bot.send_message(username, "Please select an option from the menu:", reply_markup=menu_keyboard)
    

    Two functions add in your code:

    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)
            menu_keyboard = InlineKeyboardMarkup(
                [[InlineKeyboardButton("Option 1", callback_data='1'),
                  InlineKeyboardButton("Option 2", callback_data='2')]]
            )
            await bot.send_message(username, "Please select an option from the menu:", reply_markup=menu_keyboard)
            @bot.on(events.CallbackQuery)
            async def callback_query_handler(event):
                callback_query = event.data
                if callback_query == '1':
                    await first_function(value)
                elif callback_query == '2':
                    await second_function(value)
        bot.run_until_disconnected()
    
    
    Login or Signup to reply.
  2. 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 :

    from telethon import Button
    btns = [[Button.inline(“option 1”, data=“option1”), Button.inline(“Option 2”, data=“option2”)]
    await client.send_message(…, buttons=btns)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search