0

The issue is when I want to call the '/cancel' command to my bot is it doesn't respond.I tried logging the messages sent and received to see what is going wrong.it seems like the messages aren't being received to the bot . Here is my code

from telegram.ext import Updater,CommandHandler,MessageHandler,Filters,ConversationHandler,CallbackQueryHandler
from telegram import InlineKeyboardButton,InlineKeyboardMarkup,ReplyKeyboardMarkup,KeyboardButton
import sys
import logging
sys.path.append('/storage/emulated/0/portfolio (upcoming project)/telegram_Bots/')

import api_key
from timer_function import  starttime
# Set the logging level and format
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')


#states
TIMER_RUNNING=range(1)
# Create a logger for your bot
logger = logging.getLogger(__name__)
logger.info('Starting bot')

def start_command(update,context):
    update.message.reply_text(''' Hi, 
    I'm your Pomodoro Timer. 
    Let's work? Send me minutes to set the timer to or press button below. 
    In a group please send commands like this: /@bot_name
    Send /rules to see Pomodoro Technique. 
    Send /settings to change sprint details, 
    like pomodoro and rest duration.
    If you like me, you can donate at me in patreon
    ''')
    keyboard=[['/5','/15','/25'],['/sprint'],['/help','/stats','/repeat','/cancel']]
    reply_markup=ReplyKeyboardMarkup(keyboard)

    update.message.reply_text(text='choose below',reply_markup=reply_markup)
    
def rules_command(update,context):
    update.message.reply_text('''Pomodoro Technique consists of 5 simple steps:
1. Choose the task
2. Start the timer
3. Work on the task until the timer rings
4. Take a short break (3-5 minutes)
5. After four pomodoros, take a longer break (15-30 minutes).

Sprint consists of four 25-minutes pomodoros.  ''')
def timer(update,context,my_time):
    keyboard=[[InlineKeyboardButton('⌛',callback_data='1')]]
    reply_markup=InlineKeyboardMarkup(keyboard)
    update.message.reply_text(f''' Pomodoro {my_time} minutes started.
    ''',reply_markup=reply_markup)
    context.user_data['timer_duration'] = my_time
    context.user_data['running_time']=True
    return TIMER_RUNNING
def button_click(update, context):
    query = update.callback_query
    query.answer()
    
    
    logger.info('Button clicked with data: %s', query.data)

    if query.data == '1':
        running_time = context.user_data.get('running_time', False)
        logger.info('Running time status: %s', running_time)
        if running_time:
            my_time = context.user_data.get('timer_duration')
            the_time = starttime(my_time)
            message = query.message.reply_text('Time left')
            for i in the_time:
                context.bot.edit_message_reply_markup(chat_id=message.chat_id, message_id=message.message_id, reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(f'Time remaining left: {i}', callback_data='0')]]))
                if i == '00:00':
                    query.message.reply_text('Pomodoro done! How\'s it going?')
                    running_time = False
                    return ConversationHandler.END
                    
                
          

        # Rest of the function...
def cancel_command(update, context):
    update.message.reply_text('Ok,no problem')
    if context.user_data['running_time']:
        update.message.reply_text('Ok,no problem')
        context.user_data['running_time']=False
        return ConversationHandler.END
    else:
        update.message.reply_text('No timer is running')
def help_command(update,context):
    update.message.reply_text(''' 
    Hi, I'm your Pomodoro Timer. Let's work? Send me minutes to set the timer to or press button below. Send /rules to see Pomodoro Technique. Send /settings to change sprint details, like pomodoro and rest duration.

In a group please send commands like this: /25@pomodoro_timer_bot.
     
       ''')         
    
def log_messages(update, context):
    message = update.message.text
    logger.info('Received message: %s', message)

#running bot
if __name__ == '__main__':
    updater = Updater(api_key.APIKEY, use_context=True)
    dp = updater.dispatcher
    #conversation handler
    conversation=ConversationHandler(entry_points=[
    MessageHandler(Filters.regex('^/5$'), lambda update, context: timer(update, context, my_time=5*60)),
    MessageHandler(Filters.regex('^/15$'), lambda update, context: timer(update, context, my_time=15*60)),
    MessageHandler(Filters.regex('^/25$'), lambda update, context: timer(update, context, my_time=25*60)),
     MessageHandler(Filters.regex('^/sprint$'), lambda update, context: timer(update, context, my_time=120*60)),
       ],states={
       TIMER_RUNNING:[CallbackQueryHandler(button_click)]
       },
       fallbacks=[MessageHandler(Filters.regex('^/cancel$',),cancel_command)]
       
       
       )
    
    dp.add_handler(MessageHandler(Filters.text & ~Filters.command, log_messages))
    dp.add_handler(conversation)
    dp.add_handler(MessageHandler(Filters.regex('^/help$'),help_command))
    
   
    # Run the bot
    updater.start_polling(1.0)
    updater.idle()


I tried to debug my code using logging but I still can't figure it out.

Yarik
  • 1
  • You should really reduce your code down to [MCVE]. It is so hard to read and understand. :( – doneforaiur Aug 16 '23 at 17:34
  • please note that the `python-telegram-bot` team is no longer providing support for versions v13.15 and older. I recommend to upgrade to v20. – CallMeStag Aug 16 '23 at 18:42

0 Answers0