commit 71a175150d1842c1a8d7e755f9063838f333b0b3 Author: Dominika Liberda Date: Tue Aug 31 00:16:15 2021 +0200 + Initial commit diff --git a/tg.sh b/tg.sh new file mode 100755 index 0000000..658bc31 --- /dev/null +++ b/tg.sh @@ -0,0 +1,146 @@ +#!/bin/bash +TELEGRAM_TOKEN="" +PARSE_MODE="MarkdownV2" + +# tg_send(chat, text, file, type) +function tg_send_message() { + if [[ $3 != '' ]]; then + local type_snake=$4 + [[ $4 == '' ]] && local type_snake='document' + [[ $4 == '' || $4 == 'document' ]] && local type="Document" + [[ $4 == 'photo' ]] && local type="Photo" + [[ $4 == 'audio' ]] && local type="Audio" + [[ $4 == 'video' ]] && local type="Video" + [[ $4 == 'animation' ]] && local type="Animation" + [[ $4 == 'voice' ]] && local type="Voice" + [[ $4 == 'video_note' ]] && local type="VideoNote" + + curl -s + -F chat_id=$1 \ + -F caption="$2" \ + -F parse_mode=$PARSE_MODE \ + -F $type_snake=@$3 \ + "https://api.telegram.org/bot${TELEGRAM_TOKEN}/send${type}" + else + curl -s + -F chat_id=$1 \ + -F text="$2" \ + -F parse_mode=$PARSE_MODE \ + "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage" + fi +} + +# tg_get_me() +function tg_get_me() { + curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/getMe" +} + +# 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[]' +} + +function _on_msg() { + jq -rc 'message.user.first_name' <<< "${_events[$i]}" + get $1 user.first_name + ${event[text]} + + echo "[msg] " +} + +function _on_edit() { + echo "[edit] ${event[json]}" +} + +function _on_animation() { + echo "[animation] ${event[json]}" +} + +function _on_audio() { + echo "[audio] ${event[json]}" +} + +function _on_document() { + echo "[document] ${event[json]}" +} + +function _on_photo() { + echo "[photo] ${event[json]}" +} + +function _on_sticker() { + echo "[sticker] ${event[json]}" +} + +function _on_video() { + echo "[video] ${event[json]}" +} + +function _on_video_note() { + echo "[video_note] ${event[json]}" +} + +function _on_voice() { + echo "[voice] ${event[json]}" +} + + +_offset=0 +while true; do + IFS=$'\n' + _events=($(_tg_get_updates $_offset 10)) + unset IFS + + for (( i=0; i<${#_events[@]}; i++ )); do + _msg_type="$(jq -r '(select(.message) | "message"), (select(.edited_message) | "edited_message")' <<< "${_events[$i]}")" + _event_type="$(jq -r '(select(.message) | .message), (select(.edited_message) | .edited_message) | ( + (select(.animation) | "animation"), + (select(.audio) | "audio"), + (select(.document) | "document"), + (select(.photo) | "photo"), + (select(.sticker) | "sticker"), + (select(.video) | "video"), + (select(.video_note) | "video_note"), + (select(.voice) | "voice"))' <<< "${_events[$i]}")" + + declare -A event + event[type]="$_msg_type" + if [[ "$_msg_type" == edited_message ]]; then + event[json]="${_events[$i]}" + _on_edit + elif [[ "$_event_type" == animation ]]; then + event[json]="${_events[$i]}" + _on_animation + elif [[ "$_event_type" == audio ]]; then + event[json]="${_events[$i]}" + _on_audio + elif [[ "$_event_type" == document ]]; then + event[json]="${_events[$i]}" + _on_document + elif [[ "$_event_type" == photo ]]; then + event[json]="${_events[$i]}" + _on_photo + elif [[ "$_event_type" == sticker ]]; then + event[json]="${_events[$i]}" + _on_sticker + elif [[ "$_event_type" == video ]]; then + event[json]="${_events[$i]}" + _on_video + elif [[ "$_event_type" == video_note ]]; then + event[json]="${_events[$i]}" + _on_video_note + elif [[ "$_event_type" == voice ]]; then + event[json]="${_events[$i]}" + _on_voice + else + event[json]="${_events[$i]}" + event[message_id]="$(jq '.message.message_id' <<< "${_events[$i]}")" + event[text]="$(jq '.message.text' <<< "${_events[$i]}")" + _on_msg + fi + unset event + done + [[ ${#_events[@]} -gt 0 ]] && _update_id=$(jq 'select(.update_id != null) | .update_id' <<< "${_events[-1]}") + [[ $_update_id != '' ]] && _offset=$((_update_id+1)) + echo $_offset +done