* fixes with md parser

This commit is contained in:
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 # 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 #!/bin/bash
source tg.sh source tg.sh
TELEGRAM_TOKEN="" TELEGRAM_TOKEN="<token>"
function _on_msg() { function _on_msg() {
echo "${event[from_username]}: ${event[text]}" echo "${event[from_username]}: ${event[text]}"
if [[ "${event[text]}" == "o/" ]]; then if [[ "${event[text]}" == "o/" ]]; then
tg_send ${event[chat_id]} "\o" tg_send ${event[chat_id]} "$(_escape '\o')"
fi fi
} }
tg_start tg_start
``` ```
The above example is all you need to create a basic telegram bot. Please note how you need to *escape* the text if there are any characters
The library is currently very WiP. Please proceed with caution. 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 #!/bin/bash
TELEGRAM_TOKEN="" TELEGRAM_TOKEN=""
PARSE_MODE="MarkdownV2" PARSE_MODE="MarkdownV2"
ESCAPE_ALL=false
# _escape_text(input_text) # _escape_text(input_text)
function _escape_text() { function _escape() {
sed 's/[][`~!@#$%^&*():;<>.,?\|{}=+-]/\\&/g' <<< "$1" sed 's/[`~!@#$%^&*():;<>.,?\|{}=+-_]/\\&/g' <<< "$1"
} }
# _unescape_text(input_text) # _unescape_text(input_text)
function _unescape_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]) # tg_send(chat, text, file, type, [id])
function tg_send() { function tg_send() {
text="$(_escape_text "$2")" if [[ $ESCAPE_ALL == true ]]; then
text="$(_escape <<< "$2")"
else
text="$2"
fi
if [[ $3 != '' ]]; then if [[ $3 != '' ]]; then
local type_snake=$4 local type_snake=$4
[[ $4 == '' ]] && local type_snake='document' [[ $4 == '' ]] && local type_snake='document'
@ -58,7 +63,7 @@ function tg_get_me() {
# tg_get_messages(offset, timeout) # tg_get_messages(offset, timeout)
function _tg_get_updates() { 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() { function _on_msg() {
@ -184,7 +189,7 @@ function tg_start() {
unset _tmp unset _tmp
if [[ "$_msg_type" == "forwarded_message" ]]; then if [[ "$_msg_type" == forwarded_message ]]; then
_jq_to_array '.message | _jq_to_array '.message |
[.forward_from.id, [.forward_from.id,
.forward_from.is_bot, .forward_from.is_bot,