How to use Jobqueue in Python-telegram-bot
Asked Answered
P

4

7

I have able to make a bot very easily by reading the docs but Jobqueue is not working as per it is written. The run_daily method uses a datetime.time object to send the message at a particular time but this code neither does its job of sending a message nor shows any errors. It just keeps running

    import datetime
    from telegram import bot
    from telegram.ext import Updater
    def callback_minute(bot, job):
        bot.send_message(chat_id=475838704, text='PlEaSe wOrK!')

    def main():
        updater = Updater()
        bot = updater.bot
        job = updater.job_queue

        dispatcher = updater.dispatcher

        job.run_daily(callback_minute, time=datetime.time(6,33,00))

        updater.start_polling()
        updater.idle()

    if __name__ == '__main__':
        main()
Pairs answered 28/9, 2018 at 13:56 Comment(0)
L
7

Maybe this would help:

from telegram.ext import Updater, CommandHandler

def daily_job(bot, update, job_queue):
    """ Running on Mon, Tue, Wed, Thu, Fri = tuple(range(5)) """
    bot.send_message(chat_id=<YOUR CHAT ID>, text='Setting a daily notifications!')
    t = datetime.time(10, 00, 00, 000000)
    job_queue.run_daily(notify_assignees, t, days=tuple(range(5)), context=update)

def notify_assignees(bot, job):
    bot.send_message(chat_id=<CHAT ID>, text="Some text!")

updater = Updater(<BOT_TOKEN>)
updater.dispatcher.add_handler(CommandHandler('notify', daily_job, pass_job_queue=True))
updater.start_polling()

and say to bot /notify

Luck answered 20/4, 2019 at 19:58 Comment(0)
B
4

Simple usage example of JobQueue Extention from python-telegram-bot

from telegram.ext import Updater, CommandHandler

def callback_alarm(bot, job):
    bot.send_message(chat_id=job.context, text='Wait for another 10 Seconds')

def callback_timer(bot, update, job_queue):
    bot.send_message(chat_id=update.message.chat_id,
                      text='Wait for 10 seconds')
    job_queue.run_repeating(callback_alarm, 10, context=update.message.chat_id)

def stop_timer(bot, update, job_queue):
    bot.send_message(chat_id=update.message.chat_id,
                      text='Stopped!')
    job_queue.stop()

updater = Updater("YOUR_TOKEN")
updater.dispatcher.add_handler(CommandHandler('start', callback_timer, pass_job_queue=True))
updater.dispatcher.add_handler(CommandHandler('stop', stop_timer, pass_job_queue=True))

updater.start_polling()

the /start command will start the JobQueue and will send a message with an interval of 5 seconds, and the queue can be stopped by /stop command.

Bennettbenni answered 5/1, 2020 at 17:47 Comment(2)
Hi , have you been able to solve this, tzinfo by default is UTC, so it doesnt match with IST?Punt
@Punt add 5h30min to it :)Siderosis
H
3

use below codes :

from telegram.ext import CommandHandler, Updater

my_token = 'YOUR TOKEN'
updater = Updater(my_token, use_context=True)
job_queue = updater.job_queue

def send_message_job(context):
    context.bot.send_message(chat_id='@YOUR CHANELL ID',text='job executed')

job_queue.run_repeating(send_message_job,interval=10.0,first=0.0)
Haematogenesis answered 22/1, 2021 at 18:54 Comment(0)
F
1

Remember also to install the scheduler library upon which the telegram depends:

pip install APScheduler
Faradic answered 9/9, 2023 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.