Table of Content
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
Cupu is an Indonesian slank language for LAME
This guidance is using terminal.
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
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.
Telegram Bot API
You should read the official document first:
Consider do this.
We are going to use this
$tele_url many times.
Now do our very first API call.
Telegram is using
JSON as their format.
We need a little helper here to make the json output looks tidy.
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.
This is the heart of the API.
You can do it with
Note that the result could be different
if you are already open your bot.
Now you can get the value in JSON with
jq is like walking in a DOM in HTML.
jq does not exist in your system,
you should install it first.
Start The Bot
It is okay if you are already start the bot.
I just want to show you how the
walk through JSON.
Consider say something in your bot,
And pipe the output to
You can see that we can isolate the output for certain JSON value.
Consider see the nice color output.
Note that getUpdates method is plural. It fetch array of messages.
A feedback to telegram, need certain variable, such as
We can easly get it using
Showing is not enough, we need to store it in variable,
using special bash command subtitution form
Now we can show the variable.
Don’t be silence, people would think that the bot is down.
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.
Before we are going to move from console to script,
we can empty the update by using
just add one (or bigger number) to the
What is Next ?
We are going to summarize the commands into BASH script.
Thank you for reading.
- February 2018
- Telegram Bot - Manual Page
- Telegram Bot - Makefile
- Telegram Bot - BASH Group Tools
- Telegram Bot - BASH Option Argument
- Telegram Bot - BASH Script
- Telegram Bot - Getting Token
- Telegram Bot - Overview
- dbus in Window Manager
- Distribution - Slackware Post Install Log
- Using Git for Working Between Computer
- Bootstrap CLI Webtools - Part Three
- Bootstrap CLI Webtools - Part Two
- Bootstrap CLI Webtools - Part One
- Bootstrap CLI Webtools - Summary
- Using Cloud for Daily Activity
- File System - Trapped in Snapper Rollback
- Jekyll Installation on openSUSE
- openSUSE Tumbleweed Tiling Experience