Skip to content

Instantly share code, notes, and snippets.

@steevehook
Forked from dideler/bot.rb
Created August 11, 2023 18:31
Show Gist options
  • Save steevehook/adc828cf207182b1948eec6aa0730916 to your computer and use it in GitHub Desktop.
Save steevehook/adc828cf207182b1948eec6aa0730916 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. Gets its API token (via @BotFather)
  3. Get the ID of the channel
  4. Run bot.rb
  5. Ping your bot in the chat that you want the id for
  6. If it's a group chat, ensure bot has access to messages: @BotFather Bot Settings >> Group Privacy >> Turn off

API Docs for sending a message: 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 'telegram_bot'
TELEGRAM_BOT_TOKEN = "YOUR_BOT_API_TOKEN"
bot = TelegramBot.new(token: TELEGRAM_BOT_TOKEN)
bot.get_updates(fail_silently: true) do |message|
puts "@#{message.from.username}: #{message.text}"
puts "Chat-ID: #{message.chat.id}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment