Schedule Telegram Messages

In this article, I will show you how to schedule telegram messages using a python program.

Schedule Telegram Messages Steps

  1. First, we will create a google sheet. We will use google Sheets to store all the messages which we want to send. In the google sheet itself, we will also provide the date and time at which we want to send telegram messages automatically.
  2. The next step will be to develop a python program that can read the messages from google sheets every minute and deliver the messages which are scheduled to be delivered in that timeframe.
  3. The third step is to make sure our program runs forever. You can host this program to run forever on any of the free hosting providers such as Heroku.

If you want to see the program in action, feel free to watch the video below.

Python Program

Below is a working python program that can be downloaded and executed. Make sure to replace all the below items with your content.

import pandas as pd
import requests
import time


while True:
    sheet_id = "1c178ED843VVtq4TWm-pyVjwQ26wiu_Q-E8IrYYfd0UM"
    
    url = f"https://docs.google.com/spreadsheets/d/{sheet_id}/gviz/tq?tqx=out:csv"

    df = pd.read_csv(url)
    df["Schedule Datetime"] = pd.to_datetime(df["Schedule Datetime"])
    df

    previous_minute = pd.datetime.now() + pd.Timedelta(minutes=-1)
    print(type(previous_minute))
    current_time = pd.datetime.now()
    print(type(current_time))
    df = df[(df["Schedule Datetime"] > previous_minute) &  (df["Schedule Datetime"]  < current_time)] 
    df


    def send_message(row):
        bot_id = "5137281086:AAEHvUJmTO01cRH0qhO0Zw_enI9DZccHDKw"
        chat_id = "@pythonindian"
        message = row[0]
        url = f"https://api.telegram.org/bot{bot_id}/sendMessage?chat_id={chat_id}&text={message}"

        return requests.get(url).json()

    if not df.empty:
        df['status'] = df.apply(send_message, axis=1)
    time.sleep(60)
    df

If you want to learn more about telegram automation, read this article. If you’re going to learn about telegram automation then please watch the complete playlist on our youtube channel here.

Comments are closed.

Scroll to Top