Skip to content

Instantly share code, notes, and snippets.

@kepocnhh
Last active May 2, 2026 12:18
Show Gist options
  • Select an option

  • Save kepocnhh/bca10f0bf6b2e2037b72f197c0339ee2 to your computer and use it in GitHub Desktop.

Select an option

Save kepocnhh/bca10f0bf6b2e2037b72f197c0339ee2 to your computer and use it in GitHub Desktop.
Telegram bot send message
#!/usr/local/bin/bash
if test $# -ne 4; then
echo 'Wrong arguments!'; exit 1; fi
TG_BOT_ID="$1"
TG_BOT_TOKEN="$2"
TG_CHAT_ID="$3"
TG_MESSAGE="$4"
ARGUMENTS=(TG_BOT_ID TG_BOT_TOKEN TG_CHAT_ID TG_MESSAGE)
for (( INDEX=0; INDEX<${#ARGUMENTS[@]}; INDEX++ )); do
ARGUMENT="${ARGUMENTS[INDEX]}"
if test -z "${!ARGUMENT}"; then
echo "Argument \"$ARGUMENT\" is empty!"; exit $((100+INDEX)); fi
done
REQUEST_BODY="{
\"parse_mode\": \"markdown\",
\"link_preview_options\": {
\"is_disabled\": true
},
\"chat_id\": ${TG_CHAT_ID}
}"
REQUEST_BODY="$(echo "${REQUEST_BODY}" | TG_MESSAGE="${TG_MESSAGE}" yq -M -o=json '.text=strenv(TG_MESSAGE)')"
if test $? -ne 0; then echo 'Request body error!'; exit 1; fi
# https://core.telegram.org/bots/api#sendmessage
CODE=$(curl -m 8 -w %{http_code} -o /dev/null \
"https://api.telegram.org/bot${TG_BOT_ID}:${TG_BOT_TOKEN}/sendMessage" \
-H 'Content-Type: application/json' \
--data "${REQUEST_BODY}")
if test $? -ne 0; then
echo 'Curl error!'; exit 1; fi
if [[ "${CODE}" != '200' ]]; then
echo 'Send tg message error!'; exit 1; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment