Some of you will know the Telegram application. It is an instant messaging application with many features, including creating your own bots. This can be very useful on different occasions, which I may present in future posts, but for now I leave this to your imagination. ‌ The only thing we need to create a bot in Telegram is:

  • A way to make requests http.
  • A Telegram account.

Send messages on Telegram with a bot

Do not worry because both things are very easy to achieve, and the programming language does not matter. Therefore, to the point, this is how a message is sent by Telegram being a bot in the different languages:

Code to make an http request with Go, PHP, JS, Python y bash

Send a text message with Go:

func SendMessage(msg string) error {
 token := os.Getenv("TELEGRAM_TOKEN")
 chatID := os.Getenv("TELEGRAM_CHAT_ID")

 url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s", token, chatID, msg)
 resp, err := http.Get(url)
 if err != nil {
  return err
 }
 defer resp.Body.Close()

 return nil
}

Send a text message with PHP: (code reference)

function sendMessage($chatID, $messaggio, $token) {
    $url = "https://api.telegram.org/bot" . $token . "/sendMessage?chat_id=" . $chatID;
    $url = $url . "&text=" . urlencode($messaggio);
    $ch = curl_init();
    $optArray = array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true
    );
    curl_setopt_array($ch, $optArray);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

Send a text message with JS (si usas node recuerda activar el flag –experimental-fetch):

const sendMessage = async (message) => {
    const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/sendMessage`;
    const body = {
        chat_id: process.env.TELEGRAM_CHAT_ID,
        text: message,
    };
    const options = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(body),
    };
    const response = await fetch(url, options);

    return await response.json();
};

Send a text message with Python: (code reference)

import requests
TOKEN = "YOUR TELEGRAM BOT TOKEN"
chat_id = "YOUR CHAT ID"
message = "hello from your telegram bot"
url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"
response = requests.get(url)
print(response.json())

Send a text message with curl: (code reference)

curl -X POST \
     -H 'Content-Type: application/json' \
     -d '{"chat_id": "YOUR_CHAT_ID", "text": "This is a test from curl"}' \
     https://api.telegram.org/bot$YOUR_BOT_TOKEN/sendMessage

It is done in a similar way In all languages, however, to know all the options that Telegram allows us to use, I recommend reading its documentation.

Create a Telegram bot

Ok Arturo, we already know how to send a message, but what about the TOKEN and the CHAT_ID?

Surely you are wondering that right now, this is the second point I was talking about at the beginning. The next thing you need is to create your bot, and for that you have to talk to BotFather, he will guide you to create your first bot. At the end of everything, a message will appear where you will see a token, this is the TOKEN we were talking about all the time, copy it and keep it in a safe place.

Now the CHAT_ID is missing. This parameter already depends on how complete we want to make our bot, but initially we will want to know our CHAT_ID to start the tests, this is as simple as the previous steps. What we will have to do is make a GET request to this url: https://api.telegram.org/bot<token>/getUpdates. We can do this with the curl itself curl https://api.telegram.org/bot<token>/getUpdates or simply by accessing that url from our browser. It’s obvious but remember you have to change <token> to the TOKEN you just copied in the previous step.

Get the ChatID

There is nothing else to do, copy these two variables in your code, or save them as environment variables (recommended for security), or pass them as a parameter to the function as in the PHP example. Sending a message with a bot is that simple, the rest of the logic I leave to you, you can combine it with an arduino to notify you of the humidity in the house, to notify you when your team scores a goal… limit is in your imagination.

Telegram libraries to create a bot

Of course, if what you want is to make a more complex bot, that sends photos, responds to messages, or other functionalities, I do not recommend that you implement the code yourself. With a simple Google search like “Telegram bot library in…” you add your favorite language, you will find thousands. I also recommend looking directly at Telegram’s own recommended libraries.