Reputation: 404
I need to start sending notifications to a TG group, before that I want to run a function continuosly which would query an API and store data in DB. While this function is running I would want to be able to send notifications if they are available in the DB:
That's my code:
import telegram
from telegram.ext import Updater,CommandHandler, JobQueue
token = "tokenno:token"
bot = telegram.Bot(token=token)
def start(update, context):
context.bot.send_message(chat_id=update.message.chat_id,
text="You will now receive msgs!")
def callback_minute(context):
chat_id = context.job.context
# Check in DB and send if new msgs exist
send_msgs_tg(context, chat_id)
def callback_msgs():
fetch_msgs()
def main():
JobQueue.run_repeating(callback_msgs, interval=5, first=1, context=None)
updater = Updater(token,use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start",start, pass_job_queue=True))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
This code gives me error: TypeError: run_repeating() missing 1 required positional argument: 'callback'
Any help would greatly appreciated
Upvotes: 1
Views: 2415
Reputation: 7050
There are a few issues with your code, let me try to point them out:
1.
def callback_msgs(): fetch_msgs()
You use callback_msgs
as callback for your job. But job callbacks take exactly one argument of type telegram.ext.CallbackContext
.
JobQueue.run_repeating(callback_msgs, interval=5, first=1, context=None)
JobQueue
is a class. To use run_repeating
, which is an instance method, you'll need an instance of that class. In fact the Updater
already builds an instance for you, it's available as updater.job_queue
in your case. So the call should look like this:
updater.job_queue.run_repating(callback_msgs, interval=5, first=1, context=None)
CommandHandler("start",start, pass_job_queue=True)
This is not strictly speaking an issue, bot pass_job_queue=True
has no effect at all, because you use use_context=True
Please note that there is a nice tutorial on JobQueue
over at the ptb-wiki. There is also an example on how to use it.
Disclaimer: I'm currently the maintainer of python-telegram-bot
Upvotes: 1