Build Serverless Telegram Bot via StdLib — hack server side! :)
Long story short, serverless is convenient if you don’t want to mind any server setup or think about scaling your service or you want to save time spent on server setups and its configurations.
Mostly you pay only for invocation of your function whenever is called and you don’t pay for server, when nobody is using your service e.g. during 🌃
What We Will Do
- environment setup
- register bot
- copy-paste snippet
- connect Telegram servers with your bot
- have fun! :)
Before We Start (prerequisites)
First of all, you will need to register at StdLib before you can continue. Actually, you can register StdLib account directly from CLI.
Afterwards, we will continue step by step from https://docs.stdlib.com/main/#/quickstart/from-scratch
We will install StdLib CLI utility.
npm install lib.cli -g
Create our stdlib workspace for our bot.
Then, we will create our service.
lib create replybot
Go to working directory.
To make requests, we will use lightweight library r2.
npm i r2
Now, you will need to have a token for your bot, we will create it at BotFather.
Lets create new Telegram Bot, using, of course, another bot for this. Go to https://web.telegram.org and write to this guy @BotFather. He will guide you through the process and you will end up with something like this:
Now you can copy paste this snippet code. Don’t forget to insert your token from BotFather at line 3.
After, you can put your bot up to accept requests from Internets.
lib up dev
In your terminal you will get something like is shown in the following code snippet.
— — — — — — — — —
Connect Telegram Servers With Your Bot
Now, we will set webhook destination to tell Telegram servers where we will receive new updates for our bot. I’m using httpie here for HTTP requests, it’s better replacement for curl
If you are struggling with something regarding stdlib, you can lookup for logs.
lib logs yangwao.replybot[@dev]
Write Your Very Own Bot Now! :)
Serverless approach is very convenient in some cases because it allows you to build very scalable solution. It also changes your thinking paradigm about managing and paying for servers.
StdLib gives you $5 credit upfront to play with and you can build your very first serverless function or deploy your chatbot.
Inspirated by article https://hackernoon.com/serverless-telegram-bot-on-aws-lambda-851204d4236c
Serverless framework gives you very simple tool to deploy your function with no need to know AWS or any other cloud provider.
Further reading https://github.com/anaibol/awesome-serverless