Last active
June 24, 2024 05:05
-
-
Save KyonLi/ff2f3c34ff757890162deea6dae5f115 to your computer and use it in GitHub Desktop.
Shell command output to telegram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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/&/\&/g; s/</\</g; s/>/\>/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