Mohan pd.
Mohan pd. Author of The Coders Blog. Also a professional fullstack javascript developer working with various tech company and client around world.

Sending a notification message to Telegram via cURL.

Sep 08, 2019 · 1 min
Sending a notification message to Telegram via cURL.

Here is the simple way to send a notification to telegram using cURL. Recently, I need to implement this on project as deployment success notification. Here is, How I manage to do it.

Steps:

  • Create a bot
  • Get a token [API TOKEN] from help of url above.
  • Needs a id of a chat [group/channel on which the message will appear.]
    1. Add your bot to the chat
    2. Fetch bot updates and look for the chat id:
      curl https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getUpdates | jq .message.chat.id
    3. OR run ruby script below also your bot the chat. The chat id will appear in bot.rb’s output.

         require 'logger'
         require 'telegram_bot'
      
         TELEGRAM_BOT_TOKEN = "YOUR_BOT_API_TOKEN"
      
         bot = TelegramBot.new(token: TELEGRAM_BOT_TOKEN, logger: Logger.new(STDOUT))
      
         bot.get_updates(fail_silently: true) do |message|
           puts "@#{message.from.username}: #{message.text}"
           puts "Chat-ID: #{message.chat.id}"
         end
      
  • Send a message via their HTTP API: https://core.telegram.org/bots/api#sendmessage
    curl -X POST \
       -H 'Content-Type: application/json' \
       -d '{"chat_id": "123456789", "text": "This is a test from curl", "disable_notification": true}' \
       https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage