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

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:
      shell 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.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      
         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
    1
    2
    3
    4
    
    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
    

comments powered by Disqus