A simple Telegram Bot Command.
-
Follow the official Telegram guide for creating a Bot.
-
Input
TOKEN
andBOT_NAME
env variable fields. The token value should be available from the BotFather and the valueBOT_NAME
is the bot username that ends with either_bot
orBot
. -
Click on Create to create the project, then on Deploy to deploy the script.
-
Grab the URL that's displayed under Domains in the Production Deployment card.
-
Visit the following URL (make sure to replace the template fields):
https://api.telegram.org/bot<YOUR_TOKEN>/setWebhook?url=<DOMAIN_NAME>/<YOUR_TOKEN>
Replace <YOUR_TOKEN> with the token from the BotFather and
<DOMAIN_NAME>
with the URL from the previous step. -
Add a command to the bot by visiting the following URL:
https://api.telegram.org/bot<YOUR_TOKEN>/setMyCommands?commands=[{"command":"ping","description":"Should return a 'pong' from the Bot."}]
-
Now you can invite the bot to a Group Chat or just PM the bot with the following command "/ping".
You can run the example program on your machine using
deno
:
TOKEN=<your_telegram_bot_token> BOT_NAME=<bot_username> deno run --allow-env --allow-net https://raw.githubusercontent.com/denoland/deploy_examples/main/telegram/mod.ts
You need to use a tool like ngrok to tunnel Telegram requests to the app running on your machine.
- Run
ngrok http 8080
(assuming that the application is running on port8080
) - While registering the bot, use the https URL output by ngrok for url query.
Example:
https://api.telegram.org/bot<YOUR_TOKEN>/setWebhook?url=<ngrok_url>/<YOUR_TOKEN>
That's it.