Kubectl command cheat sheet

Check kubectl version. “Kubectl command cheat sheet” is published by [DS]Canon.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




My learning experience in building a Telegram bot

The article is primarily aimed at developers who are intending to get started building their first Telegram Bot. It will also contain a decent amount of codes to better illustrate the process — Node.js with ES6 Syntax.

I gave myself a challenge to build a Telegram Bot over the weekend. The bot will show the users randomized wikipedia articles.

As I was developing the bot, I realized that there were many processes involved in building it — Registering the bot, setting up the local development environment, testing and deploying to a server.

Thus, in order to ease the difficulty in building a bot, I decided to write this article to highlight the development process.

Type /newbot to start the creation process. It will ask you to enter the bot name and username.

After creating a bot via BotFather, it will issue you with an access token which I have hidden in the image blow (Access Token is similar to your password. Never ever expose it for anyone to see!!).

We need a way for Telegram to inform us whenever any user has interacted with our bot. Interaction can be via typing or clicking a button in our bot channel. There are 2 ways to monitor the changes:

The next question is: how do we register a webhook URL with Telegram?

In Node.js, we can simply call the setWebHook(url) method. We also need to define the POST API route. This route will accept the HTTP POST payload sent by Telegram whenever an user event has occurred.

Example of a button in Telegram context.

2. For ‘message’ events, we will watch for the /start and /page text from the user.

The below codes create an event listener for the 2 events we are interested in:

Let us focus on the ‘message’ event first where the user type something to the bot:

The msg parameter object in the event callback has two keys that interests us, msg.text and msg.chat.id. They store the user input text and the user chat id respectively.

The if condition does a regex check for the following text, /start and /page. If it passes the check, then it will do the following:

This parseAndReply() method is responsible for parsing the JSON object returned from wikipedia and sending a message / photo to the user. Let me break it down to digestable pieces:

Let us take an in-depth look of the important variables:

The “If” condition below checks whether there is a featured image for the random page (Not all pages have a thumbnail, so we need an if condition for that).

What happens in the if and else statement?

After you have successfully developed and tested your bot on your local server, it is time to deploy it live!

One of the tools I recommend using for quick deployment is Heroku. They have a free plan which offers 1000 dymo hours per month, more than sufficient to run your server.

I hope that this article has served as a general guide on who you can build a Telegram Bot, from registering a bot to deploying it live for the world to see!

Thank you for reading and please kindly give me feedback on how can I improve my writing skill.

Add a comment

Related posts:

What are the top 10 brands of Vape Kits? Complete Guide

Vaping has become increasingly popular over the years, and as a result, the market is saturated with a wide range of vape kits from various brands. Choosing the right vape kit can be overwhelming…

Mastering the Art of Data Visualization with Small Multiples

Data visualization is a powerful tool for conveying complex information in a concise manner. As data becomes increasingly abundant and complex, effective visualization techniques become even more…