* fixes with md parser

master
Dominika Liberda 2022-04-03 17:28:50 +02:00
parent 53cfd77b2f
commit 1cfc08999a
2 changed files with 25 additions and 11 deletions

View File

@ -1,22 +1,31 @@
# 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.
This library aims to make creating quick-and-dirty bots in Bash as easy
as possible. To create a basic bot, all you need is the following:
```
#!/bin/bash
source tg.sh
TELEGRAM_TOKEN=""
TELEGRAM_TOKEN="<token>"
function _on_msg() {
echo "${event[from_username]}: ${event[text]}"
if [[ "${event[text]}" == "o/" ]]; then
tg_send ${event[chat_id]} "\o"
tg_send ${event[chat_id]} "$(_escape '\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.
Please note how you need to *escape* the text if there are any characters
which would conflict with the MarkdownV2 standard. If you wish to use MD
styling, the `tg_send` call could look like that:
```
tg_send ${event[chat_id]} "[$(_escape "^_^")]($(_escape "https://example.org"))"
```
Alternatively, if you decide that you don't want to run `_escape` on every
string, set `ESCAPE_ALL` to `true` after loading the library.

17
tg.sh
View File

@ -1,20 +1,25 @@
#!/bin/bash
TELEGRAM_TOKEN=""
PARSE_MODE="MarkdownV2"
ESCAPE_ALL=false
# _escape_text(input_text)
function _escape_text() {
sed 's/[][`~!@#$%^&*():;<>.,?\|{}=+-]/\\&/g' <<< "$1"
function _escape() {
sed 's/[`~!@#$%^&*():;<>.,?\|{}=+-_]/\\&/g' <<< "$1"
}
# _unescape_text(input_text)
function _unescape_text() {
sed -E 's/\\/&lauraiscute/g;s/\\lauraiscute\\lauraiscute\\lauraiscute/\\/;s/\\lauraiscute//g' <<< "$1"
sed -E 's/\\/&<EFBFBD>Meow<EFBFBD>/g;s/\\<5C>Meow<6F>\\<5C>Meow<6F>\\<5C>Meow<6F>/\\/;s/\\<5C>Meow<6F>//g' <<< "$1"
}
# tg_send(chat, text, file, type, [id])
function tg_send() {
text="$(_escape_text "$2")"
if [[ $ESCAPE_ALL == true ]]; then
text="$(_escape <<< "$2")"
else
text="$2"
fi
if [[ $3 != '' ]]; then
local type_snake=$4
[[ $4 == '' ]] && local type_snake='document'
@ -58,7 +63,7 @@ function tg_get_me() {
# tg_get_messages(offset, timeout)
function _tg_get_updates() {
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/getUpdates?offset=$1&timeout=$2" | jq -c '.result[]'
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/getUpdates?offset=$1&timeout=p" | jq -c '.result[]'
}
function _on_msg() {
@ -184,7 +189,7 @@ function tg_start() {
unset _tmp
if [[ "$_msg_type" == "forwarded_message" ]]; then
if [[ "$_msg_type" == forwarded_message ]]; then
_jq_to_array '.message |
[.forward_from.id,
.forward_from.is_bot,