+ README, minor fixes
This commit is contained in:
parent
26520cf094
commit
0970259080
22
README.md
Normal file
22
README.md
Normal file
|
@ -0,0 +1,22 @@
|
|||
# telegram.sh, a telegram bot api library in bash
|
||||
|
||||
This library aims to make creating quick-and-dirty bots in Bash as easy as possible.
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
source tg.sh
|
||||
TELEGRAM_TOKEN=""
|
||||
|
||||
function _on_msg() {
|
||||
echo "${event[from_username]}: ${event[text]}"
|
||||
|
||||
if [[ "${event[text]}" == "o/" ]]; then
|
||||
tg_send ${event[chat_id]} "\o"
|
||||
fi
|
||||
}
|
||||
|
||||
tg_start
|
||||
```
|
||||
|
||||
The above example is all you need to create a basic telegram bot.
|
||||
The library is currently very WiP. Please proceed with caution.
|
20
tg.sh
20
tg.sh
|
@ -2,8 +2,13 @@
|
|||
TELEGRAM_TOKEN=""
|
||||
PARSE_MODE="MarkdownV2"
|
||||
|
||||
function _escape_text() {
|
||||
sed 's/[][`~!@#$%^&*():;<>.,?\|{}=+-]/\\&/g' <<< "$1"
|
||||
}
|
||||
|
||||
# tg_send(chat, text, file, type)
|
||||
function tg_send_message() {
|
||||
function tg_send() {
|
||||
text="$(_escape_text "$2")"
|
||||
if [[ $3 != '' ]]; then
|
||||
local type_snake=$4
|
||||
[[ $4 == '' ]] && local type_snake='document'
|
||||
|
@ -15,16 +20,16 @@ function tg_send_message() {
|
|||
[[ $4 == 'voice' ]] && local type="Voice"
|
||||
[[ $4 == 'video_note' ]] && local type="VideoNote"
|
||||
|
||||
curl -s
|
||||
curl -s \
|
||||
-F chat_id=$1 \
|
||||
-F caption="$2" \
|
||||
-F caption="$text" \
|
||||
-F parse_mode=$PARSE_MODE \
|
||||
-F $type_snake=@$3 \
|
||||
"https://api.telegram.org/bot${TELEGRAM_TOKEN}/send${type}"
|
||||
else
|
||||
curl -s
|
||||
curl -s \
|
||||
-F chat_id=$1 \
|
||||
-F text="$2" \
|
||||
-F text="$text" \
|
||||
-F parse_mode=$PARSE_MODE \
|
||||
"https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage"
|
||||
fi
|
||||
|
@ -82,10 +87,10 @@ function _on_voice() {
|
|||
|
||||
#jq_to_array(jq_expression)
|
||||
function _jq_to_array() {
|
||||
declare -ga "_tmp=($(jq -r "$1" | sed 's/[][`~!@#$%^&*():;<>.,?/\|{}=+-]/\\&/g' ))"
|
||||
declare -ga "_tmp=($(jq -r "$1" | sed 's/[][`~!@#$%^&*():;<>.,?\|{}=+-]/\\&/g;'"s@\\\'@\\'@g" ))"
|
||||
}
|
||||
|
||||
|
||||
function tg_start() {
|
||||
_offset=0
|
||||
while true; do
|
||||
IFS=$'\n'
|
||||
|
@ -189,3 +194,4 @@ while true; do
|
|||
[[ $_update_id != '' ]] && _offset=$((_update_id+1))
|
||||
echo $_offset
|
||||
done
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue