Skip to content

Instantly share code, notes, and snippets.

@lieuzhenghong
Last active September 12, 2017 16:04
Show Gist options
  • Save lieuzhenghong/f782948ac1617b94200a80d98600394e to your computer and use it in GitHub Desktop.
Save lieuzhenghong/f782948ac1617b94200a80d98600394e to your computer and use it in GitHub Desktop.
Telegram Bot Primer

So You Want to Build a Telegram Bot

What's a Telegram Bot and how does it work?

tl;dr

A Telegram Bot is a client that makes requests to the Telegram server and makes further requests depending on the Telegram server's response.

Harh?

Not to worry; read on!

Polling, long polling and push

To understand the mechanism by which a Telegram bot works it's important to understand how the Web works.

Imagine you require a document from The Central Office. The Central Office is this enormous office building nobody's ever seen the inside of. Everyone claims to know someome who works inside there but nobody can tell you what's inside.

To get a document from The Central Office, you knock on (one of many) doors and slip a paper form underneath the door stating what is it you want.

Sometimes, within seconds, The Central Office delivers exactly what is needed. Other times, The Central Office denies your request because you're not allowed to get that document, or they don't have that document on file, or you didn't fill up the form properly and made a mistake somewhere.

And sometimes, when the Central Office is too swamped, people stand outdoors for hours until they get fed up and leave!

Nonetheless, this works well enough for a time. People line up, knock on the door, slide the form underneath the door, wait some time, get the document(s), and go.

This is the Web 1.0 model, and how the vast majority of servers online work. The client (your computer) asks for a document (say, the front page of google.com) and it's delivered by the server (the computer at google.com).

Occasionally, a malformed request may cause the server to return a 404 error (document not found), 403 Forbidden, or a server timeout.

A sea change—polling and long polling

One day, the Central Office announces that they're going to become a central Post Office. Apart from asking for documents, you can also give them documents with the recipient's name on it, and they'll store it in their file cabinet for that person.

When the person next knocks on the the Central Office door, they will receive all the documents that were sent to them.

People love this feature, and use it all the time now. People used to check once every day, but as time goes by people discover the power of Instant Messaging and want to get quick updates from their friends.

So what they started doing is they keep knocking on the door of the Central Office every five seconds or so to ask if there are any new documents.

This works well enough but people got tired of knocking all the time. So one of them had the smart idea to knock on the door and pass the Office a document that said:

"I will be standing outside this door for the next three hours, starting now. If at any time there are new documents for me, please give them to me."

And this is exactly what a Telegram Bot does. It's a program that knocks on the door of the Telegram server, asks if there are any messages for <BOT_NAME>, and waits patiently for any new messages to come in. This is called long polling.

What does it do if it receives a message? Well, that's up to your program to handle, but most bots should do something with the message, then send a response back to the Telegram web server addressed to the user who sent it that message.

Parsing a response from the Telegram server

So imagine you're building a Telegram bot that makes coffee for you. Maybe the Telegram bot runs on a Raspberry Pi that's hooked up to a smart coffee maker.

Maybe you have three commands as follows:

/start
/help
/make_coffee
/make_tea

Then the logic of your bot will look something like this:

if (command is "/start") {
  send_response("Hello! Ready to go")
}
else if (command is "/help") {
  send_response("Type /make_coffee or /make_tea to make a drink")
}
else if (command is "make_coffee") {
  send_response("Making coffee!")
  ... talk to the coffee machine ...
}
else if (command is "make_tea") {
  send_response("Making tea!")
  ... talk to the coffee machine ...
}
else {
  send_response("Unrecognised command.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment