Skip to content

Instantly share code, notes, and snippets.

@dideler
Last active April 17, 2024 08:40
Show Gist options
  • Save dideler/85de4d64f66c1966788c1b2304b9caf1 to your computer and use it in GitHub Desktop.
Save dideler/85de4d64f66c1966788c1b2304b9caf1 to your computer and use it in GitHub Desktop.
Sending a notification message to Telegram using its HTTP API via cURL
  1. Create a bot
  2. Get the bot's API token from @BotFather
  3. Add your bot to the chat you'll be sending messages to
  4. Get the ID of the chat
    a. Fetch bot updates and look for the chat id:
    curl https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getUpdates | -r '.result[].message.chat.id'
    b. OR, run bot.rb and @-mention your bot in the chat. The chat id will appear in bot.rb's output.
    The bot may need temporary message access: @BotFather > Bot Settings > Group Privacy > Turn off
  5. Send a message using the 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
# Use this script to test that your Telegram bot works.
#
# Install the dependency
#
# $ gem install telegram_bot
#
# Run the bot
#
# $ ruby bot.rb
#
# Send a message to the bot to get the current chat's ID in the console output.
# If it's a group chat, invite them to the chat first.
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
@BANGAAN44N
Copy link

Instead of:

curl https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getUpdates | jq .message.chat.id

I think it should be:

curl https://api.telegram.org/bot$7040428535:AAH-RtS-yODyP-CIbZrEr433Y2xrLuP13vk/getUpdates | jq 'result[].message.chat.id'

@tas33n
Copy link

tas33n commented Apr 17, 2024

hi guys, is it possible to get the bot's API token from @Botfather NOT through telegram but using some api? I need to create bots automatically in my .net app

its possible with account api, instead of bot api.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment