Skip to content

giuseppetrivi/ngrok-for-testing-telegram-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤖 ngrok-for-testing-telegram-bot

❓ What is this script

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).


🛠️ What to do

Prerequisites

These are all the (easy and common) prerequisites to run the script:

Setup

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 ...

Arguments

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.

Configuration file

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_config

💡 Optional: run the script globally from the terminal

By 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 ...

Linux

You can remove the .py to auto_ngrok.py, then make the script executable:

chmod +x auto_ngrok

Then, create a symbolic link to the script:

ln -s ~/original/path/auto_ngrok ~/.local/bin/auto_ngrok

The ~/.local/bin folder is usually included in your system’s PATH.

Windows

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

About

Python script that uses the tool "ngrok" to test Telegram Bots locally, speeding up the production process

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages