Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(python) Telegram bot- how to send messages periodically?

I have a dilemma regarding my telegram bot. Let's say I have to create a function that will ask, every user connected to the bot, one time per week/month, a question:

def check_the_week(bot, update):
reply_keyboard = [['YES', 'NO']]
bot.send_message(
    chat_id=update.message.chat_id,
    text=report,

    reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))  # sends the total nr of hours
update.reply_text("Did you report all you working hour on freshdesk for this week?",
                  ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))

if update.message.text == "YES":
    update.message.reply_text(text="Are you sure?",
                              reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))

    # Asks confirmation
    if update.message.text == "YES":
        update.message.reply_text(text="Thank you for reporting your working hours in time!")

    elif update.message.text == "NO":
        update.message.reply_text(text="Please, check you time reports and add missing")

elif update.message.text == "NO":
    update.message.reply_text(text="Please, check you time reports and add missing")

I want this function to be triggered every week. I was thinking about using JobQueue. The problem is that in this case the function should have two parameters- bot AND job_queue, but no update:

def callback_30(bot, job):
    bot.send_message(chat_id='@examplechannel',
    text='A single message with 30s delay')

j.run_once(callback_30, 30)

How can I create a Job Scheduler (or any other solution) in telegram bot to be trigger my function once a week? p.s. No "while True"+time.sleep() solutions please. The loop just stuck forever, I tried it.

like image 207
Vasile Avatar asked Nov 07 '17 20:11

Vasile


People also ask

How to send messages to a telegram user using Python?

So in this post, we will be sharing how to send messages to a Telegram user using Python. First, create a bot using Telegram BotFather. To create a BotFather follow the below steps – Open the telegram app and search for @BotFather. Click on the start button or send “/start”.

How to create a telegram bot?

First, create a bot using Telegram BotFather. To create a BotFather follow the below steps – Open the telegram app and search for @BotFather. Click on the start button or send “/start”. Then send “/newbot” message to set up a name and a username. After setting name and username BotFather will give you an API token which is your bot token.

How to send messages periodically from Python-telegram-bot?

To send messages periodically, you can use JobQueue Extention from python-telegram-bot 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.

How to send messages to telegram subscribers?

A Telegram bot to send messages and medias to the subscribers directly through bot. Authorized users of the bot can send messages (Texts or Media) within the bot. Authorized users can get the subscriber count also. broadcast- send posts to the subscribers users - view subscribers count


1 Answers

You need to use the context parameter when defining the job in your function. Look at this example:

   from telegram.ext import Updater, CommandHandler, MessageHandler,    Filters, InlineQueryHandler


def sayhi(bot, job):
    job.context.message.reply_text("hi")

def time(bot, update,job_queue):
    job = job_queue.run_repeating(sayhi, 5, context=update)

def main():
    updater = Updater("BOT TOKEN")
    dp = updater.dispatcher
    dp.add_handler(MessageHandler(Filters.text , time,pass_job_queue=True))


    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

Now in your call back function wherever you need update. type job.context instead.

like image 75
lameei Avatar answered Nov 04 '22 02:11

lameei