Serverless Telegram Bot with Firebase

Firebase + Telegram = pure love

1. Create your Bot

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

2. Create your Firebase project

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

3. The code

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 inside: npm init
  4. Init firebase in the same folder: firebase init
  5. Choose: Functions: Configure and deploy Cloud Functions
  6. Select a default Firebase project:
    6.1 — We have a firebase project id created on the point 2.3, if this is shown in the options choose it
    6.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
Firebase init your code

3.2 The actual code

  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
Firebase deploying router function

4. Bind the bot to Firebase

  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

Voilá

--

--

--

An web artisan too old for the hype. 📜 "I hear and I forget. I see and I remember. I do and I understand."

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building My First Ruby CLI

Secure Front end (React.js) and Back end (Node.js/Express Rest API) with Keycloak

Making WiFi work on Big Sur unsupported Macs with “failed with 66” error

React Native vs Flutter

A Simple REST API with Expressive Tea | Part Three

Open Sublime Text 3 from Terminal in macOS Linux

II: Engineered to Perfection

Test Automation 101: (1)Workspace Setup Guide — Simple and Easy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Francisco Gutiérrez

Francisco Gutiérrez

An web artisan too old for the hype. 📜 "I hear and I forget. I see and I remember. I do and I understand."

More from Medium

Why Flutter and Firebase ?

5 Reasons to Choose Node.js for Web Development

Building LinkedIn Chat with Smart Replies in Flutter

Firebase upgrade from v8 to v9