2016-01-04 01:56:22 +01:00
|
|
|
#!/usr/bin/env python
|
2022-05-15 14:08:40 +02:00
|
|
|
# pylint: disable=unused-argument, wrong-import-position
|
2016-01-04 01:56:22 +01:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
2019-02-13 13:38:07 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
Simple Bot to send timed Telegram messages.
|
2017-10-20 20:24:00 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
This Bot uses the Application class to handle the bot and the JobQueue to send
|
2016-01-04 01:56:22 +01:00
|
|
|
timed messages.
|
|
|
|
|
|
|
|
First, a few handler functions are defined. Then, those functions are passed to
|
2022-04-24 12:38:09 +02:00
|
|
|
the Application and registered at their respective places.
|
2016-01-04 01:56:22 +01:00
|
|
|
Then, the bot is started and runs until we press Ctrl-C on the command line.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
Basic Alarm Bot example, sends a message after a set time.
|
|
|
|
Press Ctrl-C on the command line or send a signal to the process to stop the
|
|
|
|
bot.
|
2022-12-02 12:08:11 +01:00
|
|
|
|
|
|
|
Note:
|
2023-03-25 12:48:34 +01:00
|
|
|
To use the JobQueue, you must install PTB via
|
|
|
|
`pip install python-telegram-bot[job-queue]`
|
2016-01-04 01:56:22 +01:00
|
|
|
"""
|
|
|
|
|
2018-05-21 15:00:47 +02:00
|
|
|
import logging
|
2018-05-21 15:00:47 +02:00
|
|
|
|
2022-05-15 14:08:40 +02:00
|
|
|
from telegram import __version__ as TG_VER
|
|
|
|
|
|
|
|
try:
|
|
|
|
from telegram import __version_info__
|
|
|
|
except ImportError:
|
|
|
|
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
|
|
|
|
|
|
|
|
if __version_info__ < (20, 0, 0, "alpha", 1):
|
|
|
|
raise RuntimeError(
|
|
|
|
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
|
|
|
|
f"{TG_VER} version of this example, "
|
2022-06-09 17:22:32 +02:00
|
|
|
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
|
2022-05-15 14:08:40 +02:00
|
|
|
)
|
2020-10-31 16:33:34 +01:00
|
|
|
from telegram import Update
|
2022-05-12 19:36:25 +02:00
|
|
|
from telegram.ext import Application, CommandHandler, ContextTypes
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
# Enable logging
|
2016-05-22 18:26:57 +02:00
|
|
|
logging.basicConfig(
|
|
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
2016-12-13 21:57:37 +01:00
|
|
|
)
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
|
2019-10-11 20:10:21 +02:00
|
|
|
# Define a few command handlers. These usually take the two arguments update and
|
2022-04-24 12:38:09 +02:00
|
|
|
# context.
|
2021-06-06 12:16:23 +02:00
|
|
|
# Best practice would be to replace context with an underscore,
|
|
|
|
# since context is an unused local variable.
|
|
|
|
# This being an example and not having context present confusing beginners,
|
|
|
|
# we decided to have it present as context.
|
2022-05-12 19:36:25 +02:00
|
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Sends explanation on how to use the bot."""
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text("Hi! Use /set <seconds> to set a timer")
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def alarm(context: ContextTypes.DEFAULT_TYPE) -> None:
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Send the alarm message."""
|
2018-09-21 08:57:01 +02:00
|
|
|
job = context.job
|
2022-05-12 19:26:03 +02:00
|
|
|
await context.bot.send_message(job.chat_id, text=f"Beep! {job.data} seconds are over!")
|
2016-07-15 01:46:27 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
def remove_job_if_exists(name: str, context: ContextTypes.DEFAULT_TYPE) -> bool:
|
2020-10-15 18:48:12 +02:00
|
|
|
"""Remove job with given name. Returns whether job was removed."""
|
|
|
|
current_jobs = context.job_queue.get_jobs_by_name(name)
|
|
|
|
if not current_jobs:
|
|
|
|
return False
|
|
|
|
for job in current_jobs:
|
|
|
|
job.schedule_removal()
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def set_timer(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Add a job to the queue."""
|
2022-04-24 12:38:09 +02:00
|
|
|
chat_id = update.effective_message.chat_id
|
2016-01-04 01:56:22 +01:00
|
|
|
try:
|
|
|
|
# args[0] should contain the time for the timer in seconds
|
2022-05-12 19:36:25 +02:00
|
|
|
due = float(context.args[0])
|
2016-01-05 14:36:28 +01:00
|
|
|
if due < 0:
|
2022-05-12 19:36:25 +02:00
|
|
|
await update.effective_message.reply_text("Sorry we can not go back to future!")
|
2016-08-07 17:59:58 +02:00
|
|
|
return
|
2016-05-22 18:26:57 +02:00
|
|
|
|
2020-10-15 18:48:12 +02:00
|
|
|
job_removed = remove_job_if_exists(str(chat_id), context)
|
2022-05-12 19:26:03 +02:00
|
|
|
context.job_queue.run_once(alarm, due, chat_id=chat_id, name=str(chat_id), data=due)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2020-10-15 18:48:12 +02:00
|
|
|
text = "Timer successfully set!"
|
|
|
|
if job_removed:
|
|
|
|
text += " Old one was removed."
|
2022-05-12 19:36:25 +02:00
|
|
|
await update.effective_message.reply_text(text)
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
except (IndexError, ValueError):
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.effective_message.reply_text("Usage: /set <seconds>")
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def unset(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Remove the job if the user changed their mind."""
|
2020-10-15 18:48:12 +02:00
|
|
|
chat_id = update.message.chat_id
|
|
|
|
job_removed = remove_job_if_exists(str(chat_id), context)
|
|
|
|
text = "Timer successfully cancelled!" if job_removed else "You have no active timer."
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(text)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def main() -> None:
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Run bot."""
|
2022-04-24 12:38:09 +02:00
|
|
|
# Create the Application and pass it your bot's token.
|
|
|
|
application = Application.builder().token("TOKEN").build()
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
# on different commands - answer in Telegram
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(CommandHandler(["start", "help"], start))
|
|
|
|
application.add_handler(CommandHandler("set", set_timer))
|
|
|
|
application.add_handler(CommandHandler("unset", unset))
|
|
|
|
|
|
|
|
# Run the bot until the user presses Ctrl-C
|
|
|
|
application.run_polling()
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2016-05-22 18:26:57 +02:00
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|