Serverless Telegram Bot with Firebase

Using (free) Firebase Cloud Functions to start your first telegram bot, as always with Javascript.

1. Create your bot

Easy and fast:

  1. Search in telegram @BotFather or open this link
  2. Write the command “/newBot
  3. Follow all the steps
  4. Write down 2 things :
    1. bot name (finished in ‘bot’): In our example firebase_bot
    2. Secret Token (don’t miss it): in our example LONG_STRING_TOKEN
Telegram bot creation

2. Create your firebase project

Go to your firebase admin page

  1. Go to your firebase console https://console.firebase.google.com
  2. Add a new project
  3. Write down the final id of the project: in our example firebase-bot-560a7
Firebase project creation

3. The code

I am going to assume you have Node and NPM installed in your system and you know how to use it

3.1 Set the enviroment

  1. Install globally firebase-tools: npm install -g firebase-tools
  2. Login into firebase: firebase login
  3. Create a folder and run: npm init inside
  4. Init firebase in the same folder: firebase init
  5. Choose: Functions: Configure and deploy Cloud Functions
  6. Select a default Firebase project:
    1. We have a firebase project id created on the point 2.3, if this is shown in the options choose it
    2. If the project is not there choose [create a new project] (we will pick the project later)
  7. Javascript or Typescript: (IMHO) Typescript
  8. TSLint: Yes
  9. install npm dependencies: Yes
  10. If in the point 6 you didn’t choose your firebase project set it now with: firebase use firebase-bot-560a7 (this has to be the id of your firebase project, point 2.3)
  11. install functions dependencies:
    • cd functions folder
    • npm install –save express cors

3.2 The actual code

Our bot will reply to any telegram message with a “Hello message-user-first-name”

We are going to create a router function in order to be the single entry point for all telegram messages.

[codepen_embed height=”798″ theme_id=”0″ slug_hash=”ErqQLG” default_tab=”js” user=”pikilon”]See the Pen Simple Firebase Cloud Telegram Bot by Pikilon (@pikilon) on CodePen.[/codepen_embed]

3.3 Deploy your code

Now that you have create your bot code you need to upload it to firebase to be compiled and available

  1. In the root of your project run: firebase deploy only –functions
  2. Wait until the process finish and write down the router function url: in our case https://us-central1-firebase-bot.cloudfunctions.net/router

4. Bind the bot to Firebase

Now you have the bot, you have your firebase cloud function running, you only need to connect them.

  1. Go to your browser and put the bold text with your data, we have defined TOKEN in 1.4.2) and the url in 3.3.2 MAIN LAST “/”
    • https://api.telegram.org/botLONG_STRING_TOKEN/setWebhook?url=https://us-central1-firebase-bot.cloudfunctions.net/router/
  2. Check is set correctly going to https://api.telegram.org/botLONG_STRING_TOKEN/getWebhookInfo
Telegram bot and firebase binded

5. Check the results

Let see it in action!
Open your bot in telegram and say hello to your creation

Voilá