Create Telegram API for notification

2021-03-30

Configure telegram as notification using API in group. In VMware solutions, these can be used as event based alerting for VEBA or vROPS or VRLI

Summary tasks:

  1. Configure Bot
  2. Telegram group for notification
  3. Sent message using API

Configure BotFather

  1. chat with botfather
  1. command newbot. For this lab, I use teddyalert1bot as botname. I purposely modify the API access.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    teddy setiawan, [29 Jan 2021 16.29.35]:
    /newbot

    BotFather, [29 Jan 2021 16.29.35]:
    Alright, a new bot. How are we going to call it? Please choose a name for your bot.

    teddy setiawan, [29 Jan 2021 16.30.08]:
    teddyaler1tbot

    BotFather, [29 Jan 2021 16.30.08]:
    Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.

    teddy setiawan, [29 Jan 2021 16.30.24]:
    teddyalert1bot

    BotFather, [29 Jan 2021 16.30.25]:
    Done! Congratulations on your new bot. You will find it at t.me/teddyalert1bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.

    Use this token to access the HTTP API:
    1605817578:AAFv7kfczLJ29hRmCQ0Jv1yA5J6RR4TEDDY
    Keep your token secure and store it safely, it can be used by anyone to control your bot.

    For a description of the Bot API, see this page: https://core.telegram.org/bots/api

Telegram group

  1. Create telegram group
  2. Include our newly create bot into this group.
  3. Use REST API to check group chat ID. The url to get update response from telegram: https://api.telegram.org/botTOKEN API/getUpdates
    If the response empty, try to sent chat message to the target group. Find the telegram group with property “title”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
curl -v https://api.telegram.org/bot1605817578:AAFv7kfczLJ29hRmCQ0Jv1yA5J6RR4TEDDY/getUpdates

{
"ok": true,
"result": [
{
"update_id": 285465504,
"message": {
"message_id": 2,
"from": {
"id": 793061140,
"is_bot": false,
"first_name": "teddy",
"last_name": "setiawan",
"username": "teddysetiawan"
},
"chat": {
"id": -587481951,
"title": "teddyaler1",
"type": "group",
"all_members_are_administrators": true
},
"date": 1611915038,
"text": "tedddy"
}
}
]
}

In above example, the group chat id = -587481951

Sent message using API

  1. to sent simple message, use URL: https://api.telegram.org/botTOKENAPI/sendMessage?chat_id=-GROUPID&text=chatmessage

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    curl -v https://api.telegram.org/bot1605817578:AAFv7kfczLJ29hRmCQ0Jv1yA5J6RR4TEDDY/sendMessage?chat_id=-587481951&text=hohoho

    {
    "ok": true,
    "result": {
    "message_id": 3,
    "from": {
    "id": 1605817578,
    "is_bot": true,
    "first_name": "teddyaler1tbot",
    "username": "teddyalert1bot"
    },
    "chat": {
    "id": -587481951,
    "title": "teddyaler1",
    "type": "group",
    "all_members_are_administrators": true
    },
    "date": 1611915159,
    "text": "hohoho"
    }
    }
  2. verify the message in group