43 lines
1.1 KiB
Bash
43 lines
1.1 KiB
Bash
|
#!/bin/bash
|
||
|
TOKENFILE=${HOME}/.telegram/bottoken
|
||
|
CHATFILE=${HOME}/.telegram/chatid
|
||
|
|
||
|
function die() {
|
||
|
echo "FATAL: $*" >&2
|
||
|
exit 1
|
||
|
}
|
||
|
|
||
|
function usage() {
|
||
|
echo "usage: $0 \"text to send\""
|
||
|
echo
|
||
|
echo "Instructs telegram bot to send the supplied message to a chat."
|
||
|
echo
|
||
|
echo "Needs these files to work:"
|
||
|
echo "File with telegram bot token: ${TOKENFILE}"
|
||
|
echo "File with telegram chat id: ${CHATFILE}"
|
||
|
}
|
||
|
|
||
|
if [[ $1 == "-h" || $# -ne 1 ]]; then
|
||
|
usage
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
[[ ! -e ${TOKENFILE} ]] && die "bot token file '${TOKENFILE}' doesn't exist";
|
||
|
[[ ! -e ${CHATFILE} ]] && die "chat id file '${CHATFILE}' doesn't exist";
|
||
|
|
||
|
TOKEN=$(egrep -v '^#' "$TOKENFILE")
|
||
|
CHAT=$(egrep -v '^#' "$CHATFILE")
|
||
|
|
||
|
[[ -z ${TOKEN} ]] && die "bot token file '${TOKENFILE}' appears empty";
|
||
|
[[ -z ${CHAT} ]] && die "chat id file '${CHATFILE}' appears empty";
|
||
|
|
||
|
msg="$*"
|
||
|
|
||
|
res=$(curl -s --data "text=$msg" --data "chat_id=$CHAT" 'https://api.telegram.org/bot'$TOKEN'/sendMessage' 2>&1)
|
||
|
rv=$?
|
||
|
[[ $rv -ne 0 ]] && fatal "curl failed"
|
||
|
status=$(jq -r .ok <<<"${res}")
|
||
|
[[ $status != "true" ]] && die "telegram API returned failure: ${res}"
|
||
|
exit 0
|
||
|
|