Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JobQueue jobs not running #966

Closed
artsim opened this issue Jan 7, 2018 · 4 comments
Closed

JobQueue jobs not running #966

artsim opened this issue Jan 7, 2018 · 4 comments

Comments

@artsim
Copy link

artsim commented Jan 7, 2018

Steps to reproduce

  1. Tried using the timer code example but the queued jobs are not running after being added. I get the setting a timer message but never the BEEP.
def callback_alarm(bot, job):
    bot.send_message(chat_id=job.context, text='BEEP')


def callback_timer(bot, update, job_queue):
    bot.send_message(chat_id=update.message.chat_id, text='Setting a timer for 1 minute!')

    job_queue.run_once(callback_alarm, 60, context=update.message.chat_id)
   

timer_handler = CommandHandler('timer', callback_timer, pass_job_queue=True)
dispatcher.add_handler(timer_handler)

Configuration

Operating System:

Linux mamake 4.14.7-1-ARCH #1 SMP PREEMPT Sun Dec 17 19:13:39 UTC 2017 x86_64 GNU/Linux

Version of Python, python-telegram-bot & dependencies:

$ python -m telegram
python-telegram-bot 9.0.0
certifi 2017.11.05
future 0.16.0
Python 3.6.3 (default, Oct 24 2017, 14:48:20) [GCC 7.2.0]

Logs

2018-01-07 04:16:49,946 - JobQueue - DEBUG - Putting job callback_alarm with t=1515287869.946228

@Eldinnie
Copy link
Member

Eldinnie commented Jan 7, 2018

@artsim Hi,

Can you supply a bit more info on how you start the bot? So also include the way the polling or webhook is started (ofcourse redact your token)

@artsim
Copy link
Author

artsim commented Jan 7, 2018

Hi,

I am running the bot via flask. Here is the start of the chatbot,py file

app = Flask(__name__)
bot = telegram.Bot(TOKEN)
updater = Updater(token=TOKEN)
dispatcher = updater.dispatcher

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


@app.route('/webhooks', methods=['POST'])
def webhook_handler():
    if request.method == "POST":
        # retrieve the message in JSON and then transform it to Telegram object
        update = telegram.Update.de_json(request.get_json(force=True), bot)
        dispatcher.process_update(update)

    return 'ok'

And this at the bottom of the file

if __name__ == '__main__':
    app.debug = True
    app.run()

I am currently running it using $python chatbot.py

Hope that answers your question.

Thanks

@Eldinnie
Copy link
Member

Eldinnie commented Jan 7, 2018

@artsim Ah I see your problem here.
Because you use flask you never actually use Updater.start_* which means the threads for the dispatcher (and thus the jobqueue) is never started.
Take a look here for more info

@artsim
Copy link
Author

artsim commented Jan 7, 2018

@Eldinnie Thanks for the help so far.

I have updated my file to this so far

app = Flask(__name__)
bot = telegram.Bot(TOKEN)
updater = Updater(token=TOKEN)
# dispatcher = updater.dispatcher

update_queue = Queue()
dispatcher = Dispatcher(bot, update_queue)

# Start the thread
thread = Thread(target=dispatcher.start, name='dispatcher')
thread.start()

def webhook_handler():
    if request.method == "POST":
        # retrieve the message in JSON and then transform it to Telegram object
        update = telegram.Update.de_json(request.get_json(force=True), bot)
        # dispatcher.process_update(update)
        update_queue.put(update)

    return 'ok'

But I get the following error when running the timer command. I feel like I am missing something.

2018-01-07 16:51:34,610 - telegram.ext.dispatcher - ERROR - An uncaught error was raised while processing the update
Traceback (most recent call last):
  File "/home/artsim/.virtualenvs/telgram/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 279, in process_update
    handler.handle_update(update, self)
  File "/home/artsim/.virtualenvs/telgram/lib/python3.6/site-packages/telegram/ext/commandhandler.py", line 171, in handle_update
    return self.callback(dispatcher.bot, update, **optional_args)
  File "chatbot.py", line 129, in callback_timer
    job_queue.run_once(callback_alarm, 60, context=update.message.chat_id)
AttributeError: 'NoneType' object has no attribute 'run_once'

@artsim artsim closed this as completed Feb 1, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Aug 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants