Sending Message using Telegram Bot

With the help of this post we already created a telegram bot. Now we will use this bot to send messages to any user, group etc.

Telegram has an pre-formated URL by which we can send messages to any intended recipient. We are going to extract following information, by which we will form the URL.

  • Recipient ID
    Telegram has another Bot which will dissect any message you forward to it and provide you Several Information like below we will need only the recipient id from here.

    {
        "update_id": xxxxx,
        "message": {
            "message_id": xxxxx,
            "from": {
                "id": xxxxxx,
                "is_bot": false,
                "first_name": "First Name",
                "last_name": "Last Name",
                "username": "username",
                "language_code": "en"
            },
            "chat": {
                "id": xxxxxxx,
                "first_name": "First Name",
                "last_name": "Last Name",
                "username": "username",
                "type": "private"
            },
            "date": 1569506990,
            "forward_from": {
                "id": xxxxxxx,
                "is_bot": false,            
                "first_name": "First Name",
                "username": "username",
            },
            "forward_date": 1569506981,
            "text": "hi"
        }
    }
  • Text to Send
  • And obviously, the API Token. We found it while creating the Bot .

Now The URL is Like as below

https://api.telegram.org/botAPI Token/sendMessage?chat_id=Recipient ID&text=Text to send

With the data the URL will be something like below

https://api.telegram.org/bot123456789:ACX-SNqIyElLatJIOJyOmL_iNtqUickoqHZ/sendMessage?chat_id=509015653&text=Techie_Tips

After Executing this URL and if everything is on track then we will receive this json Data

{
  "ok": true,
  "result": {
    "message_id": 7,
    "from": {
      "id": 7619623434,
      "is_bot": true,
      "first_name": "Bot First",
      "username": "botfirst_bot"
    },
    "chat": {
      "id": 630444541,
      "first_name": "Recipient First",
      "last_name": "Recipient Last",
      "username": "recipient",
      "type": "private"
    },
    "date": 1569508483,
    "text": "Techie_Tips"
  }
}