Build a Serverless Telegram Bot with Firebase Functions
I have got to warn you though, although the code is simple, the setup is kinda long.
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).
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.
- This tutorial assumes you have Node.js (and NPM, which comes together with Node.js) installed
- Remember to first create a Firebase project, set the pricing plan to the Blaze plan, and note down the project name used
1) Install the Firebase CLI by running:
npm install -g firebase-tools
2) Sign into Firebase using your Google account by running:
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:
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>
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
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:
If you are new to this npm business and are intimidated by the amount of files in your directory, just know that the
index.jsfile is the only file you will ever need to edit for this tutorial (and for your bot to work for that matter).
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:
Refer to the Firebase official docs if you want to learn more about environment variables.
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:
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
firebase deploy --only functions and everything should work as expected.