Here is a simple Telegram bot template specifically designed for beginners
Click on the Use this template
button in the upper right or click here will immediately add this template to your own GitHub repository.Alternatively, you can click on the button to directly deploy it to Vercel.
Before using this template, it is important to have a basic understanding of the following content.
- Get Your Telegram bot TOKEN => Telegram: Contact @botfather
- Install Vercel CLI => Vercel CLI Overview | CLI Commands | Vercel Docs
- Install Ngrok => [Day-37] 使用 ngrok 讓外網連接你的 API - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天 (ithome.com.tw)
Click on the Use this template
button in the upper right or click here will immediately add this template to your own GitHub repository.
Create a .env
file in the root directory and add the following content.
TELEGRAM_BOT_TOKEN=<TELEGRAM_BOT_TOKEN>
Set the provided TOKEN from Telegram Bot.
Enter the following content in the terminal.
vercel dev
> Vercel CLI 30.0.0
> Ready! Available at http://localhost:3000
Open ngrok to obtain a publicly accessible URL, then enter the following content in another terminal.
ngrok http 3000
Finally, set up the webhook and copy the following URL
https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/setWebhook?url=<Webhook_URL>
Replace the content with your own TOKEN and webhook URL.
- TELEGRAM_BOT_TOKEN
- Webhook_URL
Note that when setting the webhook URL, it should be DOMAIN name + '/api/webhook' to correspond to the path of the webhook. Once you have made the necessary modifications to the above content, open a blank page in your browser and paste it to submit.
Enter the following content in the terminal.
vercel
It will be automatically deployed to Vercel.
Go to Vercel DashboardDashboard – Vercel
After locating the recently deployed project, configure the environment variables.
Set your own bot token inside.
KEY | VALUE |
---|---|
TELEGRAM_BOT_TOKEN | <TELEGRAM_BOT_TOKEN> |
After completing the deployment, you will obtain a URL provided by Vercel. Simply reconfigure the webhook.
https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/setWebhook?url=<Webhook_URL>
<TELEGRAM_BOT_TOKEN> corresponds to the bot's TOKEN, and <Webhook_URL> corresponds to the Vercel URL + '/api/webhook'. Once you have made the necessary modifications to the above content, open a blank page in your browser, paste it, and submit.