Search code examples
pythontelegramtelegram-bot

Stand-alone Telegram Bot that sends a message to a group daily


I am new to Python programming and Telegram Bots. My plan is, to create a very simple Telegram Bot that sends a message everyday at, say, 00:01 to a group. This is the code:

def send_message():

    TOKEN = '9834759873459794576987495769845634'  # Fake Token
    GROUP_CHAT_ID = "90347904576097904578590734905" # Fake ID
    post_text = "Something"

    url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={GROUP_CHAT_ID}&text={post_text}"
    print(requests.get(url).json())

This works perfectly fine. However, what if I want this code to run everyday at 00:01? I can probably do something like this:

import schedule
schedule.every().day.at("00:01").do(send_message)

But, and this is my main problem, what happens if I turn off my computer? Will the bot still be running? Will it post the message everyday at 00:01? Basically, are the telegram bots standalone, or they should always be running on a computer/raspberry pi/whatever?


Solution

  • Your telegram bot must be hosted on hosting (this is a site where the program will not turn off, and the code will work even when the computer is turned off), pythonanywhere is a free hosting that turns off the code every night (that is, the code runs for 23 hours 59 minutes) and then you need restart it, but there are also paid hostings, for example heroku, on which your code will work constantly