Skip to main content
Version: 1.3.0

Telegram Setup


☝️By Bottender, you can begin your first Telegram Bot in 3 mins!!

Enable Telegram Channel

Create a New Telegram Bottender App

Create Bottender App is the best way to start building a new application in Bottender.

To create a project, run:

npx create-bottender-app my-app

Make sure that the telegram option is checked:

After finishing the Create Bottender App process, you will see the config file bottender.config.js generated automatically for further channel settings.

Enable Telegram Channel for Existing Bottender Apps

First, you need to have a bottender.config.js file which sets channels.telegram.enabled as true:

module.exports = {
channels: {
telegram: {
enabled: true,
path: '/webhooks/telegram',
accessToken: process.env.TELEGRAM_ACCESS_TOKEN,
},
},
};

The default webhook path is /webhooks/telegram. You can set your webhook path in the path field.

Complete Telegram Channel Settings

To make Telegram bots work, you have to set up:

  • Telegram Access Token
  • Webhook

Telegram Access Token

bottender.config.js looks up .env for Telegram access token, which is generated by sending /newbot to @BotFather on Telegram. Meanwhile, you get a Telegram Bot account from @Botfather.

Then, paste your Telegram bot token to the following field in .env.

TELEGRAM_ACCESS_TOKEN=

Note:

  • BotFather is a super elegant mechanism to create/edit/delete your bots. You don't even need to apply for a developer account or permission to send messages.
  • Keep your token secure and store it safely; it can be used by anyone to control your bot. When you have to generate a new access token, you can send /revoke to BotFather.
  • For more info, please refer to Telegram's official doc, BotFather.

Webhook

Set Up Webhook for Development

Before setting the webhook, please make sure you have set your access token correctly in .env.

By the following command, Bottender runs a bot server by ngrok, which makes your local bot server accessible from the Internet:

# in development
npm run dev

When you run bottender in development mode, Bottender automatically run up a Ngrok client, and then you can get the information of webhook URL from the console like this:

App has started
telegram webhook url: https://42bbf602.ngrok.io/webhooks/telegram
server is running on 5000 port...

Then, you can open a new tab in the terminal and finish the webhook setting by the below command:

npx bottender telegram webhook set

Finally, press Y to allow Bottender set ngrok temporary URL as the webhook. Now you are ready to interact with your bot on Telegram :)

Set Up Webhook for Production

Before setting the webhook, please make sure you have set your access token correctly as the environment variable.

Then, you can run Bottender on your hosting by the following command:

# in production
npm start

By the following command, you can finish the Telegram webhook setting. (If you deployed your bot with the default webhook setting, you webhook for Telegram bot supposed to be https://example.com/webhooks/telegram )

npx bottender telegram webhook set -w https://example.com/webhooks/telegram

Now you are ready to interact with your bot on Telegram :)