On a fine Saturday morning, I was sitting idly, staring at my screen, wondering when A.I.s would really become witty and user-friendly (or speaking in a more user-friendly tone, When will Jarvis arrive). I realised that humanity is at least a decade away from creating a general purpose A.I.

My hopes died, but my dream lived.

What we can do now, is lay the foundation for what is to come. We can establish a standard of communication between man and machine. Then we can test it, tweak it, and improve it. Again. And again. And again.

So here we are, building a chatbot, hoping that one day, our efforts will have ultimately payed off. There are quite a few platforms to build your bot on, and to me it seems like Telegram is the way to go. Of course, your choice could be different depending upon your requirements from the platform. This is just my take.

Okay… but, what’s a bot?

According to Wikipedia,

A chatbot is a computer program which conducts a conversation via auditory or textual methods. Such programs are often designed to convincingly simulate how a human would behave as a conversational partner …

So basically, it’s a computer application (or an app, if you will), that seems to converse like a normal human being would in the 21st century. It takes in text as input that looks like a normal conversation. It then processes that input and extracts crucial information from it, and finally provides you with a (hopefully?) satisfying answer (or it happily plays the role of your servant and does any task you want it to).

But, what about…

No, it won’t go out of control and take over the world just yet. We’re still a few decades away from that. But, we will help it achieve that in this post.

Yes, WE WILL.

Nah, just joking :) We’re just building a simple bot using the Telegraf Bot Framework.

Enough said, lets begin.

To create a bot on Telegram, there’s … a bot for that. Yes, really. For that, I hope you have the Telegram app installed on your desired OS. Open the Telegram app and search for @BotFather and begin the conversation with him. There’s a good short tutorial for that on the Telegram Bots Intro page. Save your authorisation token for further use.

Now that our bot exists (let’s assume its username is @example_bot), we can begin doing some real stuff.

Open up the terminal on your computer (I prefer using a UNIX/Linux based OS) and make a folder for example_bot and cd into it

mkdir example-bot

cd example-bot/

Now, you can install the telegraf node package using either Yarn or NPM. I personally prefer Yarn over NPM. (For all you ‘X vs Y’ folks out there, this Sitepoint article is here to help you.)

Now, initialise the project and install the telegraf package

yarn init

yarn add telegraf

Create a new file called index.js and open it in your favourite text editor. Once you’ve opened the empty file, type the following code into it. I repeat,

Type it. Don’t Copy-Paste code. Ever.

const Telegraf = require('telegraf') const app = new Telegraf('<YOUR TOKEN>') app.command('start', (ctx) => {

console.log('start', ctx.from)

ctx.reply("welcome")

}) app.hears(/hi/i, (ctx) => ctx.reply("Hey, there")) app.on('sticker', (ctx) => ctx.reply('👍')) app.startPolling()

Replace <YOUR TOKEN> with your own authorisation token.

I’ll come back and explain this code in a bit. For now, save the file and run it in your terminal using

node index.js

Now, go into your Telegram app, and start a conversation with the bot. Send the following separate messages to the bot:

/start

hi

Hi

hiii!

Hiiiiiiii!!

You can see that the bot responds for each of these messages. Now go to your terminal and press Ctrl+C to stop the script. Send the above messages again. Yes, that’s right, the bot doesn’t respond anymore.

The code

Now that we have established a working app. Let’s understand what’s happening in the code.

After requiring the telegraf package, we create a new instance of the Telegraf app while connecting it to our Telegram bot using the authentication token we provide here:

const app = new Telegraf('<YOUR TOKEN>')

In the following code block, i.e.

app.command('start', (ctx) => {

console.log('start', ctx.from)

ctx.reply("welcome")

})

we’re listening for the event when the user enters a Telegram command using the ‘/’ character. In case a user sends a command ‘/start’, this code block of ours will start executing since we have passed the argument ‘start’ in the listener.

But, how does it know what to execute?

Relax, I hear ya! here, these are Arrow functions.

Sorry, arrow functions :) these arrow functions are used as JavaScript (JS) callbacks in Telegraf. You can learn about JS callbacks over here and here. ctx (the framework’s preferred short form for context ) is just a parameter we pass to the callback. This ctx is a JS object containing all the information about the message recieved from the user. Namely, information about the sender, the message content, time and date of sending etc.

Now, when the user sends a ‘/start’ command, we get to know who sent it, in our terminal (provided the script is running). This can be easily understood from

console.log('start', ctx.from)

and then our bot replies with ‘welcome’. Similarly, when the bot gets the inputs :

hi

Hi

hiii!

Hiiiiiiii!!

It replies with “Hey, there”. /hi/i is Regular expression (RegExp) for the above commands. If you really want to get your hands on RegExp, go check out RegExr. I’ll wait here.

app.hears(/hi/i, (ctx) => ctx.reply("Hey, there"))

I’m pretty sure that if you’ve come this far in the tutorial, you can make out what this means:

app.on('sticker', (ctx) => ctx.reply('👍'))

Now, in the end app.startPolling() simply starts our app.

There you have it!

Your very own simple Telegram bot is done. If, like me, you’re really interested in getting into this stuff, you can go check out the official site of Telegraf