Coders Packet

Python Telegram Event Reminder Bot

By Sri Venkata Sai Kiran Chaitanya Agnihotram

Introducing the "Python Telegram Event Reminder Bot" - your personal assistant for event management. Set event reminders through Telegram, stay organized, and receive timely notifications.

Introduction

The "Python Telegram Event Reminder Bot" project is a Python-based application that leverages the Telegram platform to provide users with event reminder and notification functionality. The bot allows users to set event details, including the event name, date, and time, and sends reminders and notifications at the specified time.

The project utilizes the python-telegram-bot library to interact with the Telegram Bot API and handle user commands and messages. Upon receiving the /event command, the bot prompts the user to enter the event details in a specific format. It stores the event details and schedules a notification using the Python sched module.

The bot runs continuously, listening for user commands and messages. When the scheduled time for an event arrives, the bot sends a notification to the user with the event name. This helps users stay organized and reminds them of important events or tasks.

The project implementation involves creating a Telegram bot, setting up a development environment with Python and the necessary libraries, and writing the code to handle user interactions, store event details, and schedule notifications. The bot can be deployed on a hosting platform or server to make it accessible to users.

Overall, the "Python Telegram Event Reminder Bot" project provides a convenient and efficient way for users to manage their events and receive timely reminders through the popular Telegram messaging platform.


Libraries used:

The "Python Telegram Event Reminder Bot" project utilizes two key libraries:

  1. python-telegram-bot: This library provides a high-level interface to the Telegram Bot API. It simplifies the process of interacting with Telegram, handling user commands and messages, and sending and receiving updates. It offers various features and functionalities to build robust and interactive Telegram bots.

  2. sched: The sched module is part of the Python standard library and provides a simple interface for scheduling and managing events. It allows you to schedule function calls or notifications at specific times or after certain intervals. In this project, the sched library is used to schedule event notifications based on the user's specified date and time.

By combining the capabilities of these libraries, the "Python Telegram Event Reminder Bot" is able to seamlessly communicate with users, handle event details, and schedule notifications to ensure timely reminders for upcoming events.


Steps to Implement the Project:

the steps to successfully implement the "Python Telegram Event Reminder Bot" project:

  1. Create a Telegram bot:

    • Open the Telegram app and search for the "BotFather" bot.
    • Start a chat with BotFather and follow the instructions to create a new bot.
    • Obtain the bot token provided by BotFather, as you will need it to authenticate your bot.
  2. Set up your development environment:

    • Install Python on your machine if you haven't already.
    • Install the required Python libraries by running the following command in your terminal or command prompt:
      pip install python-telegram-bot
  3. Write the Python code:

    • Open a text editor or an Integrated Development Environment (IDE).
    • Write the Python code that includes the bot's functionality, such as handling commands, storing event details, scheduling notifications, and sending messages.
    • You can refer to the code provided earlier in our conversation as a starting point and make any necessary modifications.
  4. Replace the placeholder with your bot token:

    • In the Python code, replace the
      'YOUR_BOT_TOKEN'
      placeholder with the actual bot token you obtained from BotFather.
  5. Test the bot locally:

    • Save the Python code in a .py file.
    • Run the Python script using the command python your_script.py.
    • Verify that the bot is running and responding to commands and event details correctly.
  6. Deploy the bot:

    • Choose a hosting platform or server to deploy your bot, such as Heroku, AWS, or a VPS.
    • Set up the necessary configuration to deploy your Python code on the chosen platform.
    • Upload your Python script to the hosting platform and ensure that it is running correctly.
  7. Interact with your bot:

    • Start a conversation with your bot on Telegram.
    • Type the command /event to initiate the event setup process.
    • Follow the bot's instructions to enter the event details, including the event name, date, and time.
    • Verify that the bot confirms the event setup and displays the event reminder at the specified time.


Screenshots:
Telegram Bot Creation:

Telegram Bot Creation
Here Extract the "Bot Token" That needs to be replaced In the Code

Working:
Working of this project

 

Code:

import sched
import time
from datetime import datetime
from telegram import Bot, Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

# Initialize the bot
bot_token = 'REPLACE_WITH_YOUR_BOT_TOKEN'
bot = Bot(token=bot_token)

# Create a scheduler object
scheduler = sched.scheduler(time.time, time.sleep)

# Store the user's event details
user_data = {}

# Handle the /event command
def handle_event_command(update: Update, context):
    chat_id = update.effective_chat.id
    bot.send_message(chat_id=chat_id, text="Hi! Enter your event details in the following format:\n\n/event   \n\nExample: /event Meeting 2023-05-31 14:30")

# Handle event details entered by the user
def handle_event_details(update: Update, context):
    chat_id = update.effective_chat.id
    message = update.message.text
    event_details = message.split()

    if len(event_details) != 4 or event_details[0] != '/event':
        bot.send_message(chat_id=chat_id, text="Invalid command format. Please use the /event command followed by event details.")
        return

    event_name, event_date, event_time = event_details[1:]

    user_data[chat_id] = {
        'event_name': event_name,
        'event_date': event_date,
        'event_time': event_time
    }

    bot.send_message(chat_id=chat_id, text="You will be reminded of the event.")

    # Schedule the reminder
    schedule_notification(event_name, event_date, event_time, chat_id)


# Create an updater and dispatcher
updater = Updater(token=bot_token, use_context=True)
dispatcher = updater.dispatcher

# Handle the /event command
event_handler = CommandHandler('event', handle_event_details)
dispatcher.add_handler(event_handler)

# Handle other commands
unknown_handler = MessageHandler(Filters.command, handle_event_command)
dispatcher.add_handler(unknown_handler)

# Start the bot
updater.start_polling()


def send_notification(event_name, chat_id):
    bot.send_message(chat_id=chat_id, text=f"It's time for the event: {event_name}")


def schedule_notification(event_name, event_date, event_time, chat_id):
    # Combine event date and time
    datetime_str = f"{event_date} {event_time}"

    # Convert user input to datetime object
    trigger_time = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M")

    # Calculate the delay in seconds until the trigger time
    delay = (trigger_time - datetime.now()).total_seconds()

    # Schedule the notification
    scheduler.enter(delay, 1, send_notification, argument=(event_name, chat_id))

    # Start the scheduler
    scheduler.run()

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Sri Venkata Sai Kiran Chaitanya Agnihotram (SaiKiran0267)

Download packets of source code on Coders Packet