A simple Node.js boilerplate for developing Telegram bots with Telegraf.js and easily deploying to Heroku.
- Runs your bot in webhook mode in your local environment (using Ngrok)
- Ready for deployment to Heroku
- Node.js installed
By cloning it:
$ git clone https://github.com/robcss/telegraf-bot-boilerplate.git
Or by creating a new repository from the template:
See the github docs for more information on using templates.
Cd to the repo and install dependencies
$ npm install
Create a .env file in your directory and set up the following environment variables:
BOT_TOKEN=yourbottoken
PORT=yourport
NODE_ENV=development
NODE_ENV must be set to "development" in order to run the bot locally using Ngrok.
Write all your bot logic inside bot-app.js.
Run the app:
$ node index.js
Console should log this:
Environment: development
Webhook: https://yourpublicurl.ngrok.io
Port: yourport
Your bot should now be up and ready to get updates from Telegram!
You can check the status of the webhook with a GET request of
https://api.telegram.org/bot<yourbottoken>/getWebhookInfo
Telegram API should reply with:
{
"ok":true,
"result":
{
"url":"https://yourpublicurl.ngrok.io",
"has_custom_certificate":false,
"pending_update_count":0,
"max_connections":40.
"ip_address":"12.345.67.89"
}
}
- Sign up to Heroku
- Download the Heroku Command Line Interface (CLI)
In your personal dashboard click on New > Create new app
Give your app a name and create it
In your app page go to Settings, down to Config Vars and click on Reveal Config Vars
Set HOOK_URL to your app url, which you can find down on the same page under the Domains section.
Set BOT_TOKEN to your bot token.
PORT will be set automatically by Heroku.
NODE_ENV will be set to "production" by Heroku.
Cd to your repository and login
$ heroku login
$ heroku git:remote -a your-app-name123
$ git add .
$ git commit -m "first deploy"
$ git push heroku master
Your bot should now be up and live on Telegram!
Rembember you can check the status of the webhook with a GET request of
https://api.telegram.org/bot<yourbottoken>/getWebhookInfo
- Node.js
- Telegraf.js
- Ngrok (Node.js wrapper)
- micro-bot for inspiration