Telegram-Powered Bots. This article is a guest post by Eduardo… – Best Telegram

Telegram-Powered Bots

Building Bots with Node.js

This article is a guest post by Eduardo Freitas and Madan Bhintade, the authors of Building Bots with Node.js, which looks at building a Telegram-powered bot that will act like a virtual assistant! Telegram (https://telegram.org/) is a free, cloud-based mobile and desktop messaging app that takes you into a new era of messaging, which focuses primarily on security and the speed of the message delivery. So, let’s get started!

NorthStack™ Serverless Application Hosting

Skip to content NorthStack™ is managed application hosting built on a modern serverless AWS stack. Deploy vanilla or…

northstack.com

Telegram

Start by Setting up a Bot Account Using a Telegram Bot — @BotFather

A Telegram bot is a special account that does not require a phone number to be set up. To set up this account, you will use another Telegram bot named BotFather, a Telegram bot that rules all the other Telegram bots. This is an awesome technique that Telegram has specially provided for developers to create their own bots. Here, you can see the capability of one bot that helps you in creating other bots.

Technically, Telegram bots are third-party applications running inside Telegram. When a user sends a message to a Telegram bot, Telegram’s intermediary server takes care of the encryption and communication with the help of Telegram bot APIs.

The way to set up a bot account is as follows:

  1. Conduct a search for @Botfather and add it to your conversations, or you can directly open the URL https://telegram.me/botfather to start conversations with BotFather. BotFather will then introduce itself and will display a START button at the bottom for the user.

2. Once you click on the START button, BotFather will provide you with all the commands that can be used for creating a new bot, as shown in the following screenshot:

3. Now, click on the link /newbot from your conversation with BotFather. With this command, BotFather will ask you to choose a name for your bot.

4. For now, choose the name MadansNewTelegramBot. BotFather will internally validate whether the name is available. If it is available, BotFather asks for a username for the newly created bot.

5. At this point, BotFather has created your bot and has also provided a token for your bot. This token can be used whilst wiring up your bot with Telegram bot APIs.

6. Now you can use this bot for conversations using the URL telegram.me/MadansNewBot or by searching for the name of the bot in the search field, as shown in the following screenshot:

7. After searching once, you can select the bot for further conversations.

Your first Telegram bot will be a no-brain bot as there is no intelligence built within it. In the next section, you will actually build some basic intelligence with the help of Node.js. You will build a bot that will tell you the sentiments of your messages.

Creating a Basic Telegram Bot

You can use Node.js and Telegram bot APIs in order to create your basic Telegram bot.

Start by creating a folder in your local drive from the command prompt in order to store your bot:

mkdir telegrambotcd telegrambot

Assuming you have Node.js and npm installed, create and initialize your package.json, which will store your bot's dependencies and definitions:

npm init

Once you have gone through the npm init options (which are very easy to follow), you'll see something similar to this:

In your project folder, you’ll see the result, which is your package.json file.

Just like you did in your previous example, you will use Express ( http://expressjs.com ) as your REST Node.js framework. You can install it and save it to your package.json file as follows:

npm install express --save

Once Express has been installed, you should see something like this:

With Express setup, the next thing to do is to install the node-telegram-bot-api package. This can be located at https://www.npmjs.com/package/telegram-bot-api .

In order to install it, run this npm command:

npm install node-telegram-bot-api --save

You should then see something similar to this:

The next thing to do is to update your package.json in order to include the "engines"attribute. Open the package.json file with a text editor and update it as follows:

"engines": {"node": ">=5.6.0"}

Your package.json should then look like this:

Bot Conversation Logic

With your bot all wired up, you can now focus on creating the core logic for your conversations with the bot. Create your app.js file, which will be the entry point to your bot.

Your app.js should like this:

var telegramBot = require('node-telegram-bot-api');var token ='267449059:AAGzHJFlDkzOG5SxyOJJT2yHsiC06Ut6ySE';var api = new telegramBot(token, {polling: true});api.onText(/\/help/, function(msg, match) {var fromId = msg.from.id;api.sendMessage(fromId, "I can help you in getting the sentiments of any text you send to me.");});api.onText(/\/start/, function(msg, match) {var fromId = msg.from.id;api.sendMessage(fromId, "They call me MadansFirstTelegramBot. " +"I can help you in getting the sentiments of any text you send to me."+"To help you i just have few commands.\n/help\n/start\n/sentiments");});console.log("MadansFirstTelegramBot has started. Start conversations in your Telegram.");

Now take a look at the code snippet line by line. The first thing you’ll see is the reference the node package you previously installed using npm.

var telegramBot = require('node-telegram-bot-api');

Once you now have set up your reference, you are connected to your bot. Remember, BotFather provided a token to your bot for accessing Telegram bot APIs; you will be referring to the same token here, as shown in the following screenshot:

var token ='267449059:AAGzHJFlDkzOG5SxyOJJT2yHsiC06Ut6ySE';var api = new telegramBot(token, {polling: true});});

You have a handle to interact with your bot, through the token and bot APIs, so you can start a conversation with your bot. To start a bot, Telegram bots use the command /start. On entering the start command, your bot should introduce itself and also ask you how he can help you. This is achieved using the following code snippet:

api.onText(/\/start/, function(msg, match) {var fromId = msg.from.id;api.sendMessage(fromId, "They call me MadansFirstTelegramBot. " +"I can help you in getting the sentiments of any text you send to me."+"To help you i just have few commands.\n/help\n/start\n/sentiments");});

This basically tells your bot that if a user sends the command /start, your bot will send message in response to that with the help of api.onText()method.

Now, run your Node.js program to start your conversation with the bot.

Conversations with Your Basic Telegram Bot

To start conversations with your basic bot you should:

  1. Search for your newly created and Node.js wired bot using its name, as shown in the following screenshot:

2. Click on the START button, and the /start command will be sent to your bot to start the conversation. Once you've done that, you'll see that your bot has responded to the /start command. Refer to the following screen:

3. Whatever you have written for the /start command in your app.js has executed and, through the Telegram bot APIs, the response is shown to you.

4. Click on the /help command or type in /help for your bot. Your bot will respond to the /help command with following response that you have wired into your Node.js program.

5. Since your Node.js program from app.js is running behind the scenes, your bot is responding to your commands based on what has been programmed in app.js.

Building Bots With Node.js

We hope you had an interesting experience learning how to build a Telegram bot. If you found this a thought provoking read, you can always explore Building Bots with Node.js by Eduardo Freitas and Madan Bhintade. This book shows you how to automate workflow, automate internal communication processes, and provide customer service without apps, using messaging and interactive bots.

An expert in data capture, extraction, big data, and business process automation, Eduardo Freitas delivers support and helps create world-class software solutions that automate critical business processes. Madan Bhintade is an independent solution architect. A C# Corner MVP, he is also a developer with a focus on cloud-based solutions. He has 16 years of experience building solutions for insurance, financial, and HR industries.

Ten articles before and after

How does Telegram members count impact the ICO investment returns? – Best Telegram

From Telegram to Twitter: Top Puerto Rican Officials Plotted Possible Information Operation – Best Telegram

Infomatix Telegram Sticker Contest – Best Telegram

Introducing The Unbound Telegram Ambassador Program – Best Telegram

O WhatsApp ficou mais seguro que o Telegram? – Best Telegram

Telegram Inline Keyboards using Google App Script – Best Telegram

Command and control server in social media (Twitter, Instagram, Youtube + Telegram) – Best Telegram

build Telegram Messenger iOS App. 最近 Telegram 愈來愈紅,彼得潘的很多朋友,像是溫蒂跟奇妙仙子都跑到… – Best Telegram

Telegram: The Next Generation Messaging Platform? – Best Telegram

Telegram, Viber, SendInBlue, HubSpot, Google Recaptcha: What’s New in Startup 3.3? – Best Telegram