'Telegram bot to send auto message every n hours with python-telegram-bot

I am quite new in building bots so I created a very simple Telegram bot and it works great but can't figure out how to make the bot send messages every n minutes or n hours when /start_auto command is initiated.

I made a workaround with while loop but it looks stupid and during the loop users won't be able to interact with the bot about other topics.

I want users to be able to start and stop this scheduled task by commands such as /start_auto and /stop_auto.

I know there are many other answered questions related to this topic but none of them seem to be working with my code.

import logging
import os
import time

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

logger = logging.getLogger(__name__)

PORT = int(os.environ.get('PORT', '8443'))


def start(update, context):
    """Sends a message when the command /start is issued."""
    update.message.reply_text('Hi!')


def help(update, context):
    """Sends a message when the command /help is issued."""
    update.message.reply_text('Help!')


def start_auto(update, context):
    """Sends a message when the command /start_auto is issued."""
    n = 0
    while n < 12:
        time.sleep(3600)
        update.message.reply_text('Auto message!')
        n += 1


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


def main():
    TOKEN = 'TOKEN_GOES_HERE'
    APP_NAME = 'https://my-tele-test.herokuapp.com/' 

    updater = Updater(TOKEN, use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", help))
    dp.add_handler(CommandHandler("start_auto", start_auto))

    # log all errors
    dp.add_error_handler(error)
    updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN, webhook_url=APP_NAME + TOKEN)
    updater.idle()


if __name__ == '__main__':
    main()


Solution 1:[1]

will post a solution which I found:

def callback_auto_message(context):
    context.bot.send_message(chat_id='12345678', text='Automatic message!')


def start_auto_messaging(update, context):
    chat_id = update.message.chat_id
    context.job_queue.run_repeating(callback_auto_message, 10, context=chat_id, name=str(chat_id))
    # context.job_queue.run_once(callback_auto_message, 3600, context=chat_id)
    # context.job_queue.run_daily(callback_auto_message, time=datetime.time(hour=9, minute=22), days=(0, 1, 2, 3, 4, 5, 6), context=chat_id)


def stop_notify(update, context):
    chat_id = update.message.chat_id
    context.bot.send_message(chat_id=chat_id, text='Stopping automatic messages!')
    job = context.job_queue.get_jobs_by_name(str(chat_id))
    job[0].schedule_removal()

And in the main function I created a commands:

dp.add_handler(CommandHandler("auto", start_auto_messaging))
dp.add_handler(CommandHandler("stop", stop_notify))

Solution 2:[2]

python-telegram-bot has a built-in feature for scheduling tasks, called JobQueue. Please have a look at this wiki page for more info.


Disclaimer: I'm currently the maintainer of python-telegram-bot.

Solution 3:[3]

Why dont you use pyrogram. It has made everything easier.

from pyrogram import Client, filters 
import time

app = Client('my_account',          #Update this line with
               api_id=API_ID,       #your API_ID
               api_hash=API_HASH,   #your API_HASH
               bot_token=BOT_TOKEN) #your BOT_TOKEN

def main():
    while(True):
        app.send_message(chat_id=123456789, text="This message is sent every 5 mins")
        time.sleep(5000) app.run()

If this answer was useful to you please help me find a solution for #72158677 question

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 CallMeStag
Solution 3