Skip to content

Instantly share code, notes, and snippets.

@dideler

dideler/bot.rb

Last active Jan 14, 2021
Embed
What would you like to do?
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 | jq .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
@thilinapiy

This comment has been minimized.

Copy link

@thilinapiy thilinapiy commented Jun 14, 2019

Thanks for sharing this:
Also you can just use another curl to get the chat id instead of running this ruby code.

https://api.telegram.org/bot<YourBOTToken>/getUpdates
Eg:- 
https://api.telegram.org/bot123456789:jbd78sadvbdy63d37gda37bd8/getUpdates

Source: https://stackoverflow.com/a/32572159/516964

@dideler

This comment has been minimized.

Copy link
Owner Author

@dideler dideler commented Jun 16, 2019

Thanks for the tip @thilinapiy! I've updated the instructions.

@AzagraMac

This comment has been minimized.

Copy link

@AzagraMac AzagraMac commented Mar 26, 2020

TOKEN="YOUR_TOKEN" 
ID="YOUR_ID"
URL="https://api.telegram.org/bot$TOKEN/sendMessage"

curl -s -X POST $URL -d chat_id=$ID -d text="Hello World"

Example, copy script to /usr/local/bin/notify-on-ssh-login.sh:

#!/bin/bash

TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
ID="xxxxxxxxxxxxxx"
URL="https://api.telegram.org/bot$TOKEN/sendMessage"

if [ "$PAM_TYPE" != "open_session" ]
then
	exit 0
else
	curl -s -X POST $URL -d chat_id=$ID -d text="$(echo -e "Host: `hostname`\nUser: $PAM_USER\nHost: $PAM_RHOST")" > /dev/null 2>&1
	exit 0
fi

Add to end file /etc/pam.d/sshd

session    optional     pam_exec.so  /usr/local/bin/notify-on-ssh-login.sh
@zyadomer999

This comment has been minimized.

Copy link

@zyadomer999 zyadomer999 commented Nov 14, 2020

Is there anyone here please can explain how can you send a media file through HTTP request in telegram bot?

@dideler

This comment has been minimized.

Copy link
Owner Author

@dideler dideler commented Nov 16, 2020

@yvikt

This comment has been minimized.

Copy link

@yvikt yvikt commented Dec 12, 2020

👍

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