Skip to content

Latest commit

 

History

History
132 lines (101 loc) · 3.7 KB

readme.md

File metadata and controls

132 lines (101 loc) · 3.7 KB

telegraf-bot-boilerplate

A simple Node.js boilerplate for developing Telegram bots with Telegraf.js and easily deploying to Heroku.

Features

  • Runs your bot in webhook mode in your local environment (using Ngrok)
  • Ready for deployment to Heroku

Installation

Prerequisites

Get the repository

By cloning it:

$ git clone https://github.com/robcss/telegraf-bot-boilerplate.git

Or by creating a new repository from the template:

template.png

See the github docs for more information on using templates.

Install dependencies

Cd to the repo and install dependencies

$ npm install

Run the bot locally

Set up development config variables

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.

Run the app

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!

How to confirm webhook status

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"
 }
}

Deploy to Heroku

Prerequisites

Create app

In your personal dashboard click on New > Create new app

createapp.png

Give your app a name and create it

createapp2.png

Set production config variables

In your app page go to Settings, down to Config Vars and click on Reveal Config Vars

config.png

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.

Login to Heroku via CLI

Cd to your repository and login

$ heroku login

Add Heroku remote to your local repository

$ heroku git:remote -a your-app-name123

Deploy with Git

$ 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

Built with:

Acknowledgments