Telegram Bot: Difference between revisions
Initial publication |
→Starting and Stopping Your Bot: Changed caution box to info |
||
Line 97: | Line 97: | ||
You have a functional bot now, but you need a way to start and stop it. | You have a functional bot now, but you need a way to start and stop it. | ||
{{ | {{Info| | ||
Side note: Previous versions of this guide used Python scripts to start and stop the Telegram bot. Unfortunately, the people who wanted to write their bot in Node.js always complained that they didn't want to use Python for anything. Likewise, if we used Node.js to start and stop the bot, the Python people would inevitably complain about not wanting to use Node.js for anything. Therefore, we wrote the start and stop scripts in Bash CGI to make absolutely sure that no one is happy. | Side note: Previous versions of this guide used Python scripts to start and stop the Telegram bot. Unfortunately, the people who wanted to write their bot in Node.js always complained that they didn't want to use Python for anything. Likewise, if we used Node.js to start and stop the bot, the Python people would inevitably complain about not wanting to use Node.js for anything. Therefore, we wrote the start and stop scripts in Bash CGI to make absolutely sure that no one is happy. | ||
}} | }} |
Latest revision as of 02:40, 16 March 2025
Preface
This guide will work on any of the HelioHost servers.
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse.
Telegram, also known as Telegram Messenger, is a cloud-based, cross-platform, social media and instant messaging (IM) service. It was originally launched for iOS on 14 August 2013 and Android on 20 October 2013. It allows users to exchange messages, share media and files, and hold private and group voice or video calls as well as public livestreams. It is available for Android, iOS, Windows, macOS, Linux, and web browsers. Telegram offers end-to-end encryption in voice and video calls, and in optional private chats, which Telegram calls Secret Chats.
In June 2015, Telegram launched a platform for third-party developers to create bots. Bots are Telegram accounts operated by programs. They can respond to messages or mentions directly or can be invited into groups, and are able to perform tasks, integrate with other programs and host mini apps.
How to get Started with a Telegram Bot
The first step is to download the Telegram app and create an account, if you haven't done so already. Once you have an account you can search Telegram for the @BotFather. There are many people pretending to be the BotFather, likely to scam you, but make sure you click the one that actually says @BotFather.
His profile page also has some useful links such as core.telegram.org/bots and core.telegram.org/bots/api. Next click the start button at the bottom of the page.
A whole bunch of slash commands come up, but they're all useless until we run the first command he listed: /newbot
He'll ask you for a name for your bot. I picked HelioBot.
He'll also ask you for a username for your bot, and then give you the API token. Be sure to keep your token safe, and don't do anything dumb like uploading it to a wiki because then anyone who reads the wiki will have full control over your bot.
Write the Bot Script in Python
Great! You have a bot now, and you have an API token, but it doesn't do anything without some code to run it. Let's fix that! Login to your HelioHost account, continue to Plesk, and load up the file manager.
In the top left corner click the '+' plus sign, and select 'Create File'.
Then type the name of your new file: 'heliotelebot.py'.
You want to create this bot in your home directory so random hackers and bots on the internet can't access it directly. Make sure it says 'Add a file to: /' not 'Add a file to: /httpdocs' or any other folder.
Scroll down in the file manager and click on the new 'heliotelebot.py' file to edit it. Then copy/paste this code in:
#!/usr/bin/python3.12 import os from datetime import datetime, UTC from dotenv import load_dotenv load_dotenv() TOKEN = os.getenv('TELEGRAM_TOKEN') from telegram import Update from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes async def hello(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: ts = datetime.now(UTC).strftime('%Y-%m-%d %H:%M:%S') print(f"[{ts}] Saying hello to {update.effective_user.first_name}.") await update.message.reply_text(f"Hello {update.effective_user.first_name}, you have created a Telegram bot on HelioHost!") app = ApplicationBuilder().token(TOKEN).build() app.add_handler(CommandHandler("hello", hello)) app.run_polling()
The first line is called the shebang, and it tells the system which version of Python you want to use. Morty, Tommy, and Johnny also have Python 3.9, but we recommend using Python 3.12 like in the example.
Once you have your code copy/pasted in click save in the bottom left corner. Now we need the bot to be executable so in the file manager click the 'rw- r-- r--' on the heliotelebot.py line.
Check all three of the 'Execute/Search' boxes so it looks like this and then click save in the bottom left corner.
Now we need to provide our Telegram token to the bot so click the '+' plus sign, and select 'Create File' again and this time name the file '.env'.
Make sure you start the filename with a period. It's easy to miss that in the screenshot. This file needs to be in the same directory as your Python script.
If you ever think someone has gotten your token, for instance if you posted it on a public wiki like I just did, be sure to talk to the BotFather and use the /token command to reset it. Anyone who has your token has full access to control your bot. Now you need to paste this token into your '.env' file that you created. So go back to the Plesk file manager and click the '.env' file to edit it. Paste the token into the file like this:
Make sure the line starts with 'TELEGRAM_TOKEN=' as shown. Then click 'Save Changes' and close the tab. Saving your token separate from your Python code is good practice because this way you can share the code on GitHub or something like that without worrying about people getting access to your bot.
Starting and Stopping Your Bot
You have a functional bot now, but you need a way to start and stop it.

Side note: Previous versions of this guide used Python scripts to start and stop the Telegram bot. Unfortunately, the people who wanted to write their bot in Node.js always complained that they didn't want to use Python for anything. Likewise, if we used Node.js to start and stop the bot, the Python people would inevitably complain about not wanting to use Node.js for anything. Therefore, we wrote the start and stop scripts in Bash CGI to make absolutely sure that no one is happy.
We'll use Bash CGI to do that so you can control your bot through your web page. With the file manager, navigate to '/httpdocs' and create a new directory called 'bot_control'.
Now create a new file named '.htaccess' in the '/httpdocs/bot_control' directory.
Paste this code into the new '.htaccess' file.
Options +ExecCGI AddHandler cgi-script .sh DirectoryIndex index.sh
Next create a new file named 'index.sh' in the '/httpdocs/bot_control' directory.
Paste the code below into the new index.sh file. Remember to update the bot filename if you're not using heliotelebot.py like this guide says:
#!/bin/bash # edit this to match your bot filename bot_name="heliotelebot.py" ################################################################### printf 'Content-Type: text/html\n\n' username=`whoami` pwd=`printenv|grep '^PWD'|cut -f2 -d'='` main_domain=`echo "$pwd"|cut -f3 -d'/'` control=`echo "$pwd"|cut -f5 -d'/'` temp=`ps axo user:16,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,command|grep -v grep|grep "^$username "` running=`echo "$temp"|grep -c "$bot_name"` file_base=`echo $bot_name|tr -cd "a-zA-Z0-9"` log_name="$file_base.txt" if [ "$QUERY_STRING" == "" ]; then if [ $running -ne 0 ]; then mem_kb=`echo "$temp"|grep "$bot_name"|awk '{print $6}'` mem_mb=$( echo "scale=2;$mem_kb/1024"|bc ) mem_24=$( echo "scale=2;$mem_kb*1440/1048576"|bc ) echo "$bot_name is running. <a href='?action=stop'>Stop</a><br>Current memory usage: $mem_mb MB<br>Estimated 24 hour usage: $mem_24 GB - " else echo "$bot_name is not running. <a href='?action=start'>Start</a> - " fi echo "<a href='https://heliohost.org/dashboard/load/' target='_blank'>Check Load</a><br><br>Logs: <a href='?action=clear'>Clear Logs</a> - <a href='/$control/$log_name'>Full Logs</a><pre>" tail -30 $pwd/$log_name echo "</pre><script>reloading = setTimeout('window.location.reload();', 10000);</script>" fi ts=`date +"%Y-%m-%d %H:%M:%S"` if [ "$QUERY_STRING" == "action=stop" ]; then echo "[$ts] Stopping $bot_name." >> $pwd/$log_name pid=`echo "$temp"|grep "$bot_name"|tail -1|awk '{print $2}'` if [ ${#pid} -ne 0 ]; then kill $pid fi echo "Stopping $bot_name...<script>setInterval(\"window.location.replace('/$control/');\", 2000);</script>" fi if [ "$QUERY_STRING" == "action=start" ]; then if [ $running -ne 0 ]; then echo "$bot_name is already running..." else echo "[$ts] Starting $bot_name." >> $pwd/$log_name /usr/bin/python3.12 -u /home/$main_domain/$bot_name >> $pwd/$log_name 2>&1 & echo "Starting $bot_name...<script>window.location.replace('/$control/');</script>" fi fi if [ "$QUERY_STRING" == "action=clear" ]; then cat /dev/null > $pwd/$log_name echo "Clearing logs...<script>window.location.replace('/$control/');</script>" fi exit 0
Be sure to edit the variable at the top of the file to match your bot filename.
Now, we need to set the permissions of this 'index.sh' file to be executable. On the file manager click the 'rw- r-- r--' and check all the 'Execute/Search' boxes just like we did the heliotelebot.py file earlier.
Test It Out
Now everything should be ready to be tested. Open your browser and go to yourdomain.helioho.st/bot_control/ and when you click Start it should start the bot. You can check if the bot is running by going to t.me/YourBotName on Telegram. If everything is working you should be able to type /hello and it will reply to you.
If it doesn't work go back and check all the steps again to make sure you did everything correctly. You can also check out the logs in the bot_control dashboard to see if there is any errors listed. For example, if you try to import a modules that isn't already installed it will give an error like this:
If you can't figure out what is wrong let us know by opening a customer service ticket.
Watch Your Account Load

Bots use a lot of system resources so we recommend keeping an eye on your account load.
We enforce a memory limit of no more than 200 GB per day and a limit of 10,000 CPU usage per day on our shared hosting servers.
- On the Tommy and Johnny servers, if you exceed these limits, your account will be suspended.
- On the Morty server, if you exceed these limits, we will charge you for overages.
On the bot_control dashboard it shows the current memory usage, and the estimated usage over the next 24 hours if your bot continues using the same amount of memory.
If you followed the guide exactly your bot will use about 60 GB of memory per day, which is 1/4 of your allowed amount. If you edit your bot code, make your bot more complicated, and import more modules it will increase the memory usage though. Obviously if you have some sort of memory leak it could easily allow your bot to continue consuming more and more memory until you get suspended on Tommy and Johnny, or charged for overages on Morty.
If everything is working you can shut your bot down by clicking the Stop button on the bot_control dashboard. It's a good idea to keep your bot offline if you don't need it so you don't cause too much load. If you're having trouble staying under the limits you could also consider moving to the Morty plan where there is no load limit, or moving to a VPS where you have root SSH access, dedicated memory, dedicated CPU, and no load limits. You can get a 10% discount when you pay for 6 months upfront.
Automatically Restarting the Bot
We've had a lot of people ask us if we intentionally stop their bots after 60 minutes or so. The answer is no. The only time we kill processes automatically is if your account got suspended for high load. In our testing the bots can run for weeks or longer without needing to be restarted. Our servers are stable enough to run for months or years without a reboot as well. The issue is undoubtedly that people write bad code that crashes, but rather than admitting that their code is buggy, it's easier to just blame us.
If your bot keeps stopping on its own, the best solution is to check the logs and fix the issue. If there is nothing shown in the logs that means you need to add more logging to your code to figure out why it's crashing. If you still can't figure it out you could enable auto-restart, but it's really just a crutch for buggy code rather than a real fix. If your bot code really is stable enough to run for weeks or months without crashing, auto-restart can also benefit you in the rare case of us rebooting the whole server, your bot will automatically start up as soon as the reboot is finished.

Enabling auto-restart could increase your chance of getting suspended. If you don't currently need your bot online we recommend shutting it down entirely rather than leaving it restarting over and over.
To set up auto-restart login to Plesk and navigate to:
Login > Plesk > Websites & Domains > [ domain ] > Scheduled Tasks
Next, click 'Add Task' and select the 'Fetch a URL' option.
In the URL box put the start link from your bot_control page if you would click 'Start'. The easiest way to get this value is to stop your bot, and then right click on the 'Start' link, and 'Copy link address'.
If you followed this guide without changing too much it should be something like 'yourdomain.helioho.st/bot_control/?action=start'.
For those of you who want to minimize the downtime, and maximize your suspension chance you can set the schedule task up to run every minute like this with Cron Style and '* * * * *'. If everything is working correctly it will check if the bot is up every minute. Then if the bot is stopped it will restart it, and if the bot is already running it won't do anything.

Running a scheduled task every minute increases your load by quite a bit even if your bot never needs to be restarted. Simply checking if it has crashed causes some load. Worst case scenario: this scheduled tasks starts a new bot process every minute even though you already have a copy of the bot running, and you get suspended on Tommy or Johnny in record time, or reach the maximum billing on Morty faster than anyone has ever dreamed possible.