diff --git a/README.md b/README.md index 69bf8d7..8c144b5 100644 --- a/README.md +++ b/README.md @@ -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="" 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. diff --git a/tg.sh b/tg.sh index b0d1bea..b918912 100755 --- a/tg.sh +++ b/tg.sh @@ -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/\\/&�Meow�/g;s/\\�Meow�\\�Meow�\\�Meow�/\\/;s/\\�Meow�//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,