Telegram bot to send auto message every n hours with python-telegram-bot
Question:
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()
Answers:
Why don’t 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()
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
.
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))
Everything is simple here, you only need the time library and one while loop to send an auto-message. time.sleep(30)) shows how often messages are sent in seconds.
import telebot
from telebot import custom_filters
import time
bot = telebot.TeleBot("TOKEN FOR BOT")
# Chat id can be private or supergroups.
@bot.message_handler(chat_id=[123456789], commands=['start']) # chat_id checks id corresponds to your list or not.
def admin_reply(message):
bot.send_message(message.chat.id, "You are allowed to use this command.")
while(True):
bot.send_message(message.chat.id, "Allowed to receive an auto-message every 30 seconds.", time.sleep(30))
@bot.message_handler(commands=['start'])
def not_admin(message):
bot.send_message(message.chat.id, "You are not allowed to use this command")
while(True):
bot.send_message(message.chat.id, "Allowed to receive an auto-message every minute.", time.sleep(60))
# Do not forget to register
bot.add_custom_filter(custom_filters.ChatFilter())
bot.polling(none_stop=True)
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()
Why don’t 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()
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
.
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))
Everything is simple here, you only need the time library and one while loop to send an auto-message. time.sleep(30)) shows how often messages are sent in seconds.
import telebot
from telebot import custom_filters
import time
bot = telebot.TeleBot("TOKEN FOR BOT")
# Chat id can be private or supergroups.
@bot.message_handler(chat_id=[123456789], commands=['start']) # chat_id checks id corresponds to your list or not.
def admin_reply(message):
bot.send_message(message.chat.id, "You are allowed to use this command.")
while(True):
bot.send_message(message.chat.id, "Allowed to receive an auto-message every 30 seconds.", time.sleep(30))
@bot.message_handler(commands=['start'])
def not_admin(message):
bot.send_message(message.chat.id, "You are not allowed to use this command")
while(True):
bot.send_message(message.chat.id, "Allowed to receive an auto-message every minute.", time.sleep(60))
# Do not forget to register
bot.add_custom_filter(custom_filters.ChatFilter())
bot.polling(none_stop=True)