#!/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() { echo "[msg] ${event[from_username]}: ${event[text]}" } 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" event[json]="${_events[$i]}" if [[ "$_msg_type" == edited_message ]]; then _on_edit elif [[ "$_event_type" == animation ]]; then _on_animation elif [[ "$_event_type" == audio ]]; then _on_audio elif [[ "$_event_type" == document ]]; then _on_document elif [[ "$_event_type" == photo ]]; then _on_photo elif [[ "$_event_type" == sticker ]]; then _on_sticker elif [[ "$_event_type" == video ]]; then _on_video elif [[ "$_event_type" == video_note ]]; then _on_video_note elif [[ "$_event_type" == voice ]]; then _on_voice else declare -a "_tmp=($(jq -r '.message | [.message_id, .from.id, .from.is_bot, .from.first_name, .from.last_name, .from.username, .chat.id, (if .chat.type == "private" then .chat.username else .chat.title end), .chat.type, .date, .text] | @sh' <<< "${_events[$i]}" | sed 's/[][`~!@#$%^&*():;<>.,?/\|{}=+-]/\\&/g' ))" event[message_id]="${_tmp[0]}" event[from_id]="${_tmp[1]}" event[from_is_bot]="${_tmp[2]}" event[from_first_name]="${_tmp[3]}" event[from_last_name]="${_tmp[4]}" event[from_username]="${_tmp[5]}" event[chat_id]="${_tmp[6]}" event[chat_title]="${_tmp[7]}" event[chat_type]="${_tmp[8]}" event[date]="${_tmp[9]}" event[text]="${_tmp[10]}" _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