commit 314fc060d315cf35383980761c22a0c4c89e54b4 Author: Rob Pearce Date: Mon Nov 27 09:44:24 2023 +1100 Initial checkin diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..940e942 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/README.md b/README.md new file mode 100755 index 0000000..3a9dd8a --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# Overview + +Super simple curl wrapper to send Telegram messages. + +# Requirements + + - [jq](https://jqlang.github.io/jq/) + - curl + +# Usage + +```text +usage: ./telegram-send.sh "text to send" + +Instructs telegram bot to send the supplied message to a chat. + +Needs these files to work: +File with telegram bot token: /Users/rob/.telegram/bottoken +File with telegram chat id: /Users/rob/.telegram/chatid +``` + +# Example + +## CLI command + +```text +rob@crom:telegram-send$ ./telegram-send.sh "this is a test" +rob@crom:telegram-send$ +``` + +## Telegram output + +![telegramoutput](img/this_is_a_test.png "telegram output") + + + diff --git a/img/this_is_a_test.png b/img/this_is_a_test.png new file mode 100644 index 0000000..6ed4f5f Binary files /dev/null and b/img/this_is_a_test.png differ diff --git a/telegram-send.sh b/telegram-send.sh new file mode 100755 index 0000000..60e4399 --- /dev/null +++ b/telegram-send.sh @@ -0,0 +1,42 @@ +#!/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 +