skip to Main Content

I’m new in development and python too. I tried to write a simple telegram bot using Telebot. The scenario is to show inline keyboard to user when user click on button do some logic. In Example below I cut the code but it showing the problem. And the problem is:
When first user start working he gets correct and all notifications. But when second user starts to work with bot he gets correct keyboard but notification will send to First user.

Here is a code example:

import telebot
import datetime

bot = telebot.TeleBot(insert_token_here)

keyboard1 = telebot.types.ReplyKeyboardMarkup(True)
keyboard1.row('Choose date', 'dont push it')

@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id, 'Welcome', reply_markup=keyboard1)


def dates_inline():
    current_date = datetime.datetime.today()

    # Inline keyboard
    keyboard_dates = telebot.types.InlineKeyboardMarkup()
    key_now = telebot.types.InlineKeyboardButton(text=current_date.strftime('%d.%m.%Y') + ' (Today)',
                                                 callback_data=current_date.strftime('%Y-%m-%d'))
    keyboard_dates.add(key_now)

    return keyboard_dates
@bot.message_handler(content_types=['text'])
def choose_message(message):

    if message.text == "Choose date":

        bot.send_message(message.chat.id, 'Choose date:', reply_markup=dates_inline())

        @bot.callback_query_handler(func=lambda call: True)
        def choose_date(call):
            dt = call.data
            print('chose_date dt: %s' % dt)
            bot.send_message(message.chat.id, 'All done')
        print('end')
    else:
        print('smth else')



def main():

    bot.polling(none_stop=True)


if __name__ == '__main__':
    main()

3

Answers


  1. Chosen as BEST ANSWER

    @wowkin2 Here is a sample code:

    @bot.message_handler(content_types=['text'])
    def choose_message(message):
    
        if message.text == "Choose date":
            bot.send_message(message.chat.id, 'Choose date:', reply_markup=dates_inline())
            print('end')
    
        else:
            print('smth else')
            
        @bot.callback_query_handler(func=lambda call: True)
        def choose_date(call):
            dt = call.data
            bot.send_message(message.chat.id, 'All done')
    

  2. You need to move following code to top-level indentation. Otherwise it works not as you intended.

        @bot.callback_query_handler(func=lambda call: True)
        def choose_date(call):
            dt = call.data
            print('chose_date dt: %s' % dt)
            bot.send_message(message.chat.id, 'All done')
    
    Login or Signup to reply.
  3. I also faced a similar problem.

    1. Do not create a handler/decorator inside another one. It doesn’t work like that. I’m also relatively new to python, so I don’t know the exact reason. I also learned it from my mistake.
    2. Do not send messages back to message.chat.id . send it to call.from_user.id so that it’ll always send the reply back to the user from whom the call came.
    @bot.message_handler(content_types=['text'])
    def choose_message(message):
    
        if message.text == "Choose date":
            bot.send_message(message.chat.id, 'Choose date:', reply_markup=dates_inline())
        else:
            print('smth else')
    
    
    @bot.callback_query_handler(func=lambda call: True)
        def choose_date(call):
            dt = call.data
            print('chose_date dt: %s' % dt)
            bot.send_message(call.from_user.id, 'All done')
            print('end')
        
    
    

    I am also in the development of a bot right now and this is working fine for me.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search