While still working on PhotoMasters KE, the technical lead and I often had long and detailed conversations about new technologies to leverage in the development of a simple photograhper booking platform. Builting a photographer booking Telegram bot was always a recurring favorite for us.

Our preference for building a bot on Telegram as opposed to developing a custom bot was a consideration of 3 factors: bot performance, cost and time intensiveness. The Telegram bot platform offers a wide range of efficiency boosting features including:

  • Availability: Bots run in the cloud and are not prone to downtimes
  • Editable: Bot commands are easily and readily editable
  • Command Importing: With Google Table file, you can easily make a bot
  • Multi-Device: Bots can be used both as a web or mobile app.

In this post, we will cover a simple beginners guide to doing just that - building a simple telegram bot. You'll learn how the Telegram Bot API works and you'll get a list of basic functionalities you can add to the bot.

Creating a Telegram Bot

In this write up, its assumed you already have a Telegram account. If not you will need to create an account. You will also need to create a bot using the @BotFather and following a few simple steps.

To begin start by sending the following command to BotFather:

/newbot

The BotFather will ask you for a name and username, then generate an authorization token for your new bot. You have to send then the name you want for the bot, for example:

realchainlife

You're selected bot username on the otherhand has to end in bot, and be globally unique. Once chosen send your username to BotFather as follows:

realchainlife-bot

BotFather provides a stable of several more commands like /setdescription, /setabouttext, /setuserpic and even /deletebot. For now you will then receive a congratulations message with an access token like this:

123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11

It's as simple as that, you have created a Telegram Bot. In the next sections will cover how to interact with the Bot API and look at a sample code. Take note of the access token you have received. You will need it for the code we are about to write. If your existing token is compromised or you lost it for some reason, use the /token command to generate a new one.

Using The Telegram Bot API

The simplest way to interact with your Bot is through a web browser. By sending HTTPS requests to Telegram we will issue different commands through the various URLs available. To get information about your bot for example, a simple command visit the following URL in your browser (substituting the TOKEN that you got before):

https://api.telegram.org/bot(your-bot-token)/getMe

The specific command we've sent here /getMe which returns basic information about our Bot using JSON. You can also retrieve messages sent to your Bot through the getUpdates call:

https://api.telegram.org/bot(your-bot-token)/getUpdates

Expect the output for this command to be a JSON response of all the new messages sent to your Bot. Test this out by sending a message to your Bot and revisiting the URL.

Adding Basic Functionalities

So far, your new bot is set to only respond to the /start command. In this section we'll see how to add some more functions to your bot. To use the Telegram Bot API to send a message from your bot we can send a call from the browser using the command:

https://api.telegram.org/bot(your-bot-token)/sendMessage?chat_id=(chat-id)&text=TestReply

If you're using the telegram.bot package, you can send a response by using the command:

chat_id <- "CHAT_ID" # you can retrieve it from bot$getUpdates() after sending a message to the bot
                                    bot$sendMessage(chat_id = chat_id, text = "TestReply")
                                

You can also get a full list of all the available API methods. Another functionality we can add to your bot is unknown command handling. To do this from a browser URL send the following:

https://api.telegram.org/bot(your-bot-token)/sendMessage?(chat_id)=chat_id&text=SorryIdidntunderstandthatcommand

This command works for some some confused users who might try to send commands to the bot that it doesn't understand, to use the telegrram.bot MessageHandler with a command filter to reply to all commands that were not recognized by the previous handlers write add this to your code:

    unknown <- function(bot, update){
        bot$sendMessage(chat_id = update$message$chat_id,
            text = "Sorry, I didn't understand that command.")
                                    }
                                
    updater <- updater + MessageHandler(unknown, MessageFilters$command)

In the next write-up we can go through several advanced functionalities that we can add to the bot to increase its efficiency for your business.

From our experience, bots can benefit agencies. Businesses can even automate certain routine tasks and facilitate client access to a knowledge database. This is an excellent way to cut costs and free up time for staff to accomplish more valuable tasks.