Skip to content Skip to sidebar Skip to footer

Telegram Bot Api Python Run_daily Method

Hey i am trying to run a telegram bot on a daily base so i tried different things. updater = Updater(, use_context=True) dp = updater.dispatcher dp.job_queue.run_dail

Solution 1:

Due to a bug click here Job_queue is skipping job if timezone is not provided for job.run_daily.

Add timezone in your code it will run perfectly.

datetime.time(hour=6, minute=27, tzinfo=pytz.timezone('Asia/Kolkata'))

Refer the below code if you get any error.

from telegram.ext import Updater, CommandHandler
import logging, datetime, pytz

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger()


def start(update, context):
    context.job_queue.run_daily(msg,
                                datetime.time(hour=6, minute=27, tzinfo=pytz.timezone('Asia/Kolkata')),
                                days=(0, 1, 2, 3, 4, 5, 6), context=update.message.chat_id)

def msg(context):
    context.bot.send_message(chat_id=context.job.context, text='text')

def error(update, context):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)


def main():
    updater = Updater( < YOUR_TOKEN > , use_context=True)
    dp = updater.dispatcher
    dp.add_handler(CommandHandler("start", start, pass_job_queue=True))
    dp.add_error_handler(error)

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

Post a Comment for "Telegram Bot Api Python Run_daily Method"