Skip to content

Telegram API

How to create a bot and use Telegram as a notification tool

Telegram Bots

Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to our Bot API.

Steps:

  1. Open telegram and talk to BotFather https://t.me/botfather. Just type /newbot and follow the instructions.

  2. Once created, you'll receive a token. SAVE IT!

    Let's suppose the token is 1133220044:ABDghabc123_p6k3rkp6abcdef1a2Taz_xz

  3. Now, open a chat with the bot you created and send some messages. We are doing that to find your chat_id.

  4. To discover that, we have to mount a URL with our token:

    https://api.telegram.org/bot{token}/getUpdates

    In our case, https://api.telegram.org/bot1133220044:ABDghabc123_p6k3rkp6abcdef1a2Taz_xz/getUpdates

    Just open this url and you will see something like this:

    {
       "ok":true,
       "result":[
          {
             "update_id":123456789,
             "message":{
                "message_id":"...",
                "from":{},
                "chat":{
                   "id":185270483,
                   "..."
                }
             }
          }
       ]
    }
    

    We want chat.id and in this case, it's equal to 185270483

  5. Now you just have to call some URLs to send messages.

Examples:

export TELEGRAM_TOKEN=1133220044:ABDghabc123_p6k3rkp6abcdef1a2Taz_xz
export TELEGRAM_CHAT_ID=185270483

# Text message
curl --silent --output /dev/null --show-error --fail \
    --data chat_id="$TELEGRAM_CHAT_ID" \
    --data text="hello!" \
    "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage"

# Text message (using Markdown)
curl --silent --output /dev/null --show-error --fail \
    --data chat_id="$TELEGRAM_CHAT_ID" \
    --data parse_mode="markdown" \
    --data text="*using markdown*" \
    "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage"    

# Photo, from a url
curl --silent --output /dev/null --show-error --fail \
    --data chat_id="$TELEGRAM_CHAT_ID" \
    --data photo="http://theoldreader.com/kittens/600/400" \
    --data caption='Hey, look this' \
    "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendPhoto"

# Photo, from your machine
curl --silent --output /dev/null --show-error --fail \
    --form "chat_id=$TELEGRAM_CHAT_ID" \
    --form "caption=Hey, look this" \
    --form "photo=@/home/path/to/picture.png" \
    "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendPhoto"

Source: https://core.telegram.org/bots/api#available-methods


Last update: July 15, 2020