Article Series

This is a multiple-parts article. There are few sections here.

Where to Discuss?

Local Group

Bashful Bot

Goal: Parsing JSON feed from Telegram API in BASH console

Telegram Bot is one of the interesting hype I met. Its API is simple and easy to use. Even a bashful coder can do it, in spare time.

At first I want to use telebot as a name of my project. But there are already too many project using telebot as a repository name. So I decide to use a unique name cupubot.

Cupu is an Indonesian slank language for LAME

This guidance is using terminal.

Preparation

Before You begin, you need to get a telegram bot token. It is already discussed in previous article.


Make a config

Since we are using terminal we can just write

% token='your_token_here'

But consider a more elegant way, use config. We should separate secret stuff, such as token and password, outside the console, and outside the script.

% mkdir ~/.config/cupubot
% touch ~/.config/cupubot/config.sh 
% echo "token='your_token_here'" > ~/.config/cupubot/config.sh 
% source ~/.config/cupubot/config.sh
% echo $token

BASH: Telegram Bot: config


Telegram Bot API

https://api.telegram.org/bot<token>/METHOD_NAME

You should read the official document first:

Consider do this.

% tele_url="https://api.telegram.org/bot${token}"

We are going to use this $tele_url many times.

Now do our very first API call. Telegram is using JSON as their format.

% curl -s "${tele_url}/getMe"
{"ok":true,"result":{"id":547870369,"is_bot":true,"first_name":"epsi_bot","username":"epsi_bot"}}

We need a little helper here to make the json output looks tidy. The json_reformat.

% curl -s "${tele_url}/getMe" | json_reformat
{
    "ok": true,
    "result": {
        "id": 547870369,
        "is_bot": true,
        "first_name": "epsi_bot",
        "username": "epsi_bot"
    }
}

BASH: Telegram Bot: getMe API with json_reformat

If the json_reformat does not exist in your system, you should install it first.


Save the JSON

Just like most cool API, Telegram also utilize JSON. Instead of calling the API over and over again, you may redirect the JSON output to a file, and analyze the result later on.

% curl -s "${tele_url}/getMe" > getMe.json

% cat getMe.json | json_reformat
{
    "ok": true,
    "result": {
        "id": 513194798,
        "is_bot": true,
        "first_name": "Cupu Bot",
        "username": "cupu_cupu_bot"
    }
}

BASH: Telegram Bot: redirect JSON output to file


Get Updates

This is the heart of the API.

You can do it with json_reformat. Note that the result could be different if you are already open your bot.

% curl -s "${tele_url}/getUpdates" | json_reformat
{
    "ok": true,
    "result": [

    ]
}

Now you can get the value in JSON with jq. jq is like walking in a DOM in HTML. If the jq does not exist in your system, you should install it first.

% curl -s "${tele_url}/getUpdates" | jq -r ".ok"
true

BASH: Telegram Bot: getupdates API with jq JSON


Start The Bot

It is okay if you are already start the bot. I just want to show you how the jq walk through JSON.

Consider say something in your bot, such as Hello World. And pipe the output to jq. You can see that we can isolate the output for certain JSON value.

% curl -s "${tele_url}/getUpdates" | jq -r ".result"
[
  {
    "update_id": 408173037,
    "message": {
      "message_id": 122,
      "from": {
        "id": 507894652,
        "is_bot": false,
        "first_name": "Epsi",
        "last_name": "Sayidina",
        "username": "RizqiSayidina",
        "language_code": "en-US"
      },
      "chat": {
        "id": 507894652,
        "first_name": "Epsi",
        "last_name": "Sayidina",
        "username": "RizqiSayidina",
        "type": "private"
      },
      "date": 1516742659,
      "text": "Hello World"
    }
  }
]

Consider see the nice color output.

BASH: Telegram Bot: JSON Result

Note that getUpdates method is plural. It fetch array of messages.


Chat ID

A feedback to telegram, need certain variable, such as chat_id. We can easly get it using jq.

% curl -s "${tele_url}/getUpdates" | jq -r ".result[].message.chat.id"
507894652

Showing is not enough, we need to store it in variable, using special bash command subtitution form $(…).

% chat_id=$(curl -s "${tele_url}/getUpdates" | jq -r ".result[0].message.chat.id")

Now we can show the variable.

% echo $chat_id
507894652

BASH: Telegram Bot: JSON Result


Sending Feedback

Don’t be silence, people would think that the bot is down.

% curl -s "${tele_url}/sendMessage?chat_id=${chat_id}" \
  --data-urlencode "text=Thank you for visiting" | json_reformat
{
    "ok": true,
    "result": {
        "message_id": 123,
        "from": {
            "id": 547870369,
            "is_bot": true,
            "first_name": "epsi_bot",
            "username": "epsi_bot"
        },
        "chat": {
            "id": 507894652,
            "first_name": "Epsi",
            "last_name": "Sayidina",
            "username": "RizqiSayidina",
            "type": "private"
        },
        "date": 1516743765,
        "text": "Thank you for visiting"
    }
}

BASH: Telegram Bot: Send Feedback Message

Note that sendMessage method is singular. It send one feedback at a time. We are going to do it in loop script later to reply all messages.

Now it is a good time to see the reply message, in your smartphone or any telegram client.

BASH: Telegram Bot: Feedback Message on Smartphone

Before we are going to move from console to script, we can empty the update by using offset, just add one (or bigger number) to the update_id value.

% curl -s "${tele_url}/getUpdates?offset=408173039"
{"ok":true,"result":[]}

What is Next ?

We are going to summarize the commands into BASH script.

Thank you for reading.