Skip to content

Instantly share code, notes, and snippets.

@KyonLi
Last active June 24, 2024 05:05
Show Gist options
  • Save KyonLi/ff2f3c34ff757890162deea6dae5f115 to your computer and use it in GitHub Desktop.
Save KyonLi/ff2f3c34ff757890162deea6dae5f115 to your computer and use it in GitHub Desktop.
Shell command output to telegram
#!/bin/bash
#
# Requirement: jq curl
# Only support input not parameters
#
API_HOST=api.telegram.org
TOKEN=114514:abc123
CHAT_ID=1919810
for c in jq curl; do
if ! command -v $c >/dev/null 2>&1; then
echo "Error: $c is not installed." >&2
exit 1
fi
done
IFS='' read -srd '' result
[ ! "$result" ] && result=Done || echo -n "$result"
text=$(echo -e "$(whoami)@$(uname -n):\n<pre>$(echo -n "$result" | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g')</pre>" | jq -Rs .)
curl -fsSL -o /dev/null -m 10 -X POST \
-H 'Content-Type: application/json' \
-d "{\"chat_id\": \"$CHAT_ID\", \"text\": $text, \"parse_mode\": \"HTML\", \"link_preview_options\": {\"is_disabled\": true}}" \
https://$API_HOST/bot$TOKEN/sendMessage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment