Last active
March 14, 2022 21:28
-
-
Save rachejazz/a34b589da6677235390495c8f8068e6c to your computer and use it in GitHub Desktop.
Keka Reminder Telegram Bot. Is a telegram scheduler bot that can be used to send timely messages into chats/groups as desired
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import pytz | |
import datetime | |
from telegram import Update | |
from telegram.ext import Updater, CommandHandler, CallbackContext | |
API_KEY = "API TOKEN" | |
logging.basicConfig( | |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO | |
) | |
logger = logging.getLogger(__name__) | |
def start(update: Update, context: CallbackContext) -> None: | |
update.message.reply_text('Hi! This is Keka 11am Reminder Bot. Use /set to set your reminder.\nTo cancel reminders, use /unset.\nIncase of any issues/improvements, contact my creator, @rachejazz.') | |
def help_command(update: Update, context: CallbackContext) -> None: | |
update.message.reply_text('/set to set your reminder.\n/unset to cancel reminder.\n/help to show this menu again.\nIncase of any issues/improvements, contact my creator, @rachejazz.') | |
def alarm_1(context: CallbackContext) -> None: | |
job = context.job | |
context.bot.send_message(job.context, text='Keka Clock In Reminder!') | |
def alarm_2(context: CallbackContext) -> None: | |
job = context.job | |
context.bot.send_message(job.context, text='Keka Clock Out Reminder!') | |
def remove_job_if_exists(name: str, context: CallbackContext) -> bool: | |
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 | |
def set_timer(update: Update, context: CallbackContext) -> None: | |
chat_id = update.message.chat_id | |
try: | |
time_z = pytz.timezone('Asia/Kolkata') | |
due_i = datetime.time(9, 55, tzinfo=time_z) | |
day = (0,1,2,3,4) | |
due_o = datetime.time(18, 55, tzinfo=time_z) | |
job_removed = remove_job_if_exists(str(chat_id), context) | |
context.job_queue.run_daily(alarm_1, due_i, day, context=chat_id, name=str(chat_id)) | |
context.job_queue.run_daily(alarm_2, due_o, day, context=chat_id, name=str(chat_id)) | |
text = f'Keka Reminder is set for {update.effective_user.mention_markdown_v2()}\!' | |
if job_removed: | |
text += ' Old one was removed' | |
update.message.reply_markdown_v2(text) | |
except Exception as e: | |
print(e) | |
def unset(update: Update, context: CallbackContext) -> None: | |
chat_id = update.message.chat_id | |
job_removed = remove_job_if_exists(str(chat_id), context) | |
text = 'Reminder successfully cancelled!' if job_removed else 'You have no active timer.' | |
update.message.reply_text(text) | |
def main() -> None: | |
updater = Updater(API_KEY) | |
dispatcher = updater.dispatcher | |
dispatcher.add_handler(CommandHandler("start", start)) | |
dispatcher.add_handler(CommandHandler("set", set_timer)) | |
dispatcher.add_handler(CommandHandler("help", help_command)) | |
dispatcher.add_handler(CommandHandler("unset", unset)) | |
updater.start_polling() | |
updater.idle() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment