Build a Serverless Telegram Bot with Firebase Functions

With just 8 lines of code, build and deploy a serverless telegram bot using JavaScript, all without spending a single cent

Aaron Choo
Aug 14, 2019 · 4 min read

I have got to warn you though, although the code is simple, the setup is kinda long.

Firebase

If you have not heard about Firebase, it is a Backend as a Service (BaaS) that has its own integrated real-time database, web hosting, user authentication, and many more services. For this tutorial, the only service we will be using is Firebase Cloud Functions. Head on over to the Firebase Console to create your own account and start a new project.

Make sure your Firebase pricing plan is set to the pay-as-you-go Blaze plan to allow the Telegram servers to talk to your Firebase Cloud Functions server. Don’t worry, there is a free tier of 2,000,000 Functions invocations per month so there is no need to pay for anything unless you decide to send 2 million messages to your bot in a month (now that is an achievement).

Telegraf

This tutorial uses the Telegraf framework. This framework greatly simplifies the telegram bot writing process into just a few elegant lines of code that makes the creating of bots a much more enjoyable process. If you do ever get stuck beyond this tutorial, remember to read the Telegraf docs.

Prerequisites

  1. This tutorial assumes you have Node.js (and NPM, which comes together with Node.js) installed
  2. Remember to first create a Firebase project, set the pricing plan to the Blaze plan, and note down the project name used

Setup

1) Install the Firebase CLI by running:

npm install -g firebase-tools

2) Sign into Firebase using your Google account by running:

firebase login

3) Make a new project directory to store all the code and necessary files.

mkdir telegram-bot-firebasecd telegram-bot-firebase

4) Change into the project’s root directory and initialise a new Firebase project by running:

firebase init

When prompted with the Firebase CLI features needed, choose only Functions as that is the only feature we require. Here is the full list of my response if you just want to follow along:

Are you ready to proceed? Yes
Which Firebase CLI features do you want to set up for this folder? Functions: Configure and deploy Cloud Functions
Select a default Firebase project for this directory: <YOUR_PROJECT_NAME>
What language would you like to use to write Cloud Functions? JavaScript
Do you want to use ESLint to catch probable bugs and enforce style? Yes
Do you want to install dependencies with npm now? Yes

5) After the installation is done, we have even more dependencies to install.

Head on over to the /functions directory:

cd functions

Then install Telegraf:

npm install telegraf

6) It’s finally time for some code! Fire up your favourite IDE to edit the index.js file. Here is all the code required to get a simple bot working:

7) If your eyes are sharp, you might have noticed the functions().config.telegrambot.key environment variable in the code above. It is actually the API key of the telegram bot which we have yet to create.

So let’s head on over to @BotFather on Telegram to create a new bot. Follow the instructions on BotFather and note the HTTP API token that he sends you upon creating your bot.

8) Now, we can setup the Firebase environment variable to store your bot’s API key (you can just place your API key directly in the code, but it is good practice to never hard code API keys). Now, run the following, replacing <YOUR_BOT'S_API_KEY> with the actual API key from step 7.

firebase functions:config:set telegrambot.key="<YOUR_BOT'S_API_KEY>"

Verify the key-value pair was indeed set by running:

firebase functions:config:get

9) Now, we can deploy this function to Firebase Functions by running:

firebase deploy --only functions

10) But wait! We are not done yet. We have yet to configure our bot’s endpoint to talk to our Firebase functions.

Head over to your Firebase console and press on the Functions tab. You should see a table with a Function called bot (or whatever you decided to name it) together with an URL under the Trigger column:

Full URL censored

In your terminal, run the following, again replacing the <YOUR_FIREBASE_FUNCTIONS_URL> with the URL that we just found and <YOUR_BOT'S_API_KEY> with your bot’s actual API key:

curl -F "<YOUR_FIREBASE_FUNCTIONS_URL>" https://api.telegram.org/bot<YOUR_BOT'S_API_KEY>/setWebhook

11) And voila! Now, whenever you send “hi” to your telegram bot, he will respond with “Hey there”, just like what our code at line 6 says.

Now that might seem like a lot of initial setup. However, from now onward, all you have to do to make a change is to edit and save index.js, run firebase deploy --only functions and everything should work as expected.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade