To develop or test a Telegram bot, you need to set a webhook (via the Telegram API) that points to a publicly accessible file on the web.
This means you can't develop a Telegram bot locally unless you expose your local folder to the web securely.
ngrok allows you to expose your localhost to the internet. It provides a temporary public domain pointing to your local server, which you can use to set up your bot's webhook.
This script automates the process of creating an ngrok tunnel and updating the Telegram webhook accordingly each time ngrok is started.
Related article (in Italian) here (use browser translation if needed).
These are all the (easy and common) prerequisites to run the script:
- Create a base Telegram bot (tutorial here)
- Clone this repository locally (
git clone LINK_TO_THIS_REPO) - Install Python
- Register for ngrok
- Download ngrok (see steps 1 and 2 here)
- Install the
pyngrokPython library - Install a local server (like XAMPP)
Once the prerequisites are met, place the ngrok.yml configuration file in the ngrok config folder (its location depends on your OS — more info here). You need also to put your authtoken into this file (you can find it into your ngrok account, under "Your Authtoken" in the menu):
...
authtoken: <YOUR NGROK AUTH TOKEN>
...Then, specify the path to that config file on line 40 of auto_ngrok.py:
...
ngrok_config_file_path = "/Here/The/Path/ngrok.yml"
...Now you can run the script from the command line:
py auto_ngrok.py ...The script accepts the following command-line arguments:
-f LOCAL_FOLDER_PATH: the path to the file that acts as the webhook endpoint. This file must be within your localhost folder (for example, in XAMPP, it's.../xampp/htdocs/)-t TELEGRAM_BOT_TOKEN: your Telegram Bot API token-c CUSTOM_CONFIG_FILE: name of a custom configuration file to easily reuse settings
If you use -c, the other parameters (-f and -t) will be read from the configuration file. Otherwise, they must be specified manually.
You can create custom configuration files with the following structure (for example crypto_bot_config.json):
{
"ngrok_config_file_path": "C:/path/to/ngrok/config/folder/ngrok.yml",
"local_folder_path": "/crypto_bot_project/index.php",
"telegram_bot_token": "238423979837589fwe8ydys7s7tyr78"
}Then, run the script like this:
py auto_ngrok.py -c crypto_bot_configBy default, you need to refer to the full script path every time (e.g. C:\Users\username\Desktop\ngrok-for-testing-telegram-bot\).
To avoid this, you can make it callable globally depending on your OS. Below are two ways to do that in Linux and Windows (other alternatives exist), to execute from anywhere in the terminal the script like this:
auto_ngrok ...You can remove the .py to auto_ngrok.py, then make the script executable:
chmod +x auto_ngrokThen, create a symbolic link to the script:
ln -s ~/original/path/auto_ngrok ~/.local/bin/auto_ngrokThe ~/.local/bin folder is usually included in your system’s PATH.
In Windows, add the script folder (e.g. C:\Users\username\Desktop\ngrok-for-testing-telegram-bot\) to the system's Path environment variable.
Then create a batch file auto_ngrok.bat as follows:
@echo off
set script_dir=%~dp0
py "%script_dir%auto_ngrok.py" %*
pause