- Create a bot
- Get the bot's API token from @BotFather
- Add your bot to the chat you'll be sending messages to
- Get the ID of the chat
a. Fetch bot updates and look for the chat id:b. OR, run bot.rb and @-mention your bot in the chat. The chat id will appear incurl https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getUpdates | -r '.result[].message.chat.id'
bot.rb
's output.
The bot may need temporary message access:@BotFather > Bot Settings > Group Privacy > Turn off
- 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
-
Star
(214)
You must be signed in to star a gist -
Fork
(54)
You must be signed in to fork a gist
-
-
Save dideler/85de4d64f66c1966788c1b2304b9caf1 to your computer and use it in GitHub Desktop.
# 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 |
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'
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.
Found myself in need to send an arbitrary text with unicode and quotes, the original example didn't work.
Here is updated one, forcing jq to do all of the string escaping:
#!/usr/bin/env bash
# noti-bot - send notification in telegram
# Usage:
# echo "your text" | noti-bot
# or
# noti-bot "your text" | noti-bot
API=yours
chat_id=yours
if [[ -n "$1" ]]; then
msg="$1"
else
msg="$(cat)"
fi
# to escape " and unicode, no, printf is not enough, neither is multiple -d for curl
data="$(echo '{"chat_id": "aaa", "text": "aaa", "disable_notification": false}' |
jq --arg msg "$msg" --arg chat "$chat_id" '{"chat_id": $ARGS.named.chat, "text": $ARGS.named.msg, "disable_notification": false}')"
# echo "$data"
ans="$(curl -s --tlsv1.2 -X POST \
-H 'Content-Type: application/json' \
-d "$data" \
https://api.telegram.org/bot$API/sendMessage)"
if [[ "$(echo "$ans" | jq -r '.ok')" != "true" ]]; then
echo "$ans" | jq -r '"error \(.error_code) - \(.description)"'
fi
Demo:
echo -e 'Nope!";$(pwd)"\nyes - приветこんにちは' | ./noti-bot
I know, I know, "at this point just use python or something". I needed it with curl.
If succesful - prints nothing.
If not - prints "error_code - error_description".
Is it possible to send a message to a telegram bot without the bot token? maybe by using username, since username is unique anyway.
Created a group, switching on themes\topics.
Got link for one topic (like this https://t.me/c/1234567890/1).
How to send message to this topic?
Instead of:
I think it should be: