Build Serverless Telegram Bot via StdLib — hack server side! :)

Hello, in this article I will show you how easily, with very little effort, you can run your own Telegram Bot with help of StdLib (Serverless approach).

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

Enviroment Setup

We will install StdLib CLI utility.

Create our stdlib workspace for our bot.

Then, we will create our service.

Go to working directory.

To make requests, we will use lightweight library r2.

Register Bot

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:

Copy-Paste Snippet

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.

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.

Write Your Very Own Bot Now! :)

Conclusion

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

Matej Nemček ⚡ 孔子

Written by

★ crypto & crypto ★ hypersignal.xyz ∆ cypherpunk ± ¶sychotronic 🏄 kitesurfer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade