Initial checkin
This commit is contained in:
commit
314fc060d3
|
@ -0,0 +1 @@
|
|||
.DS_Store
|
|
@ -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")
|
||||
|
||||
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 7.3 KiB |
|
@ -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
|
||||
|
Loading…
Reference in New Issue