Telegram Bot written in TypeScript and deployed on Firebase Functions
-
Create a new bot using BotFather
/newbot
-
Copy your bot token and write it in functions folder .env
BOT_TOKEN=<your_bot_token>
Note: To load .env, you need to run
firebase deploycommand first. -
Install dependencies
npm install -g firebase-tools npm install -g firebase-functions npm install -g firebase-admin npm install -g typescript npm install -g cpy-cli npm install -g ngrok npm install dotenv npm install npm-run-all
-
Initialize Firebase Project
firebase init
-
Setup webhook
curl -F "url=https://europe-west1-<your_project_id>.firebaseapp.com/telegramBot" https://api.telegram.org/bot<your_bot_token>/setWebhook
For local testing, use ngrok to expose your local server to the internet:
ngrok http 5001
Then, use the ngrok url to set the webhook. Example:
https://api.telegram.org/bot<your_bot_token>/setWebhook?url=https://<ngrok_id>.ngrok.io/<your_project_id>/europe-west1/telegramBot
-
Build typescript
cd functions | tsc --watch
-
Test locally
firebase serve --only functions
-
Deploy to firebase
firebase deploy --only functions