mirror of
https://github.com/anten-ka/go_warp_pro.git
synced 2026-05-20 17:56:20 +00:00
Optimize bot_daemon: 2 jq calls via herestring instead of 8+ echo|jq forks, separate poll_cycle for local cleanup
Made-with: Cursor
This commit is contained in:
77
warp.sh
77
warp.sh
@@ -1553,42 +1553,53 @@ bot_daemon() {
|
|||||||
if has_awg_mode && [ -n "$CONTAINER" ]; then awg_load_container_data 2>/dev/null; fi
|
if has_awg_mode && [ -n "$CONTAINER" ]; then awg_load_container_data 2>/dev/null; fi
|
||||||
local offset=0
|
local offset=0
|
||||||
while true; do
|
while true; do
|
||||||
local response
|
bot_poll_cycle
|
||||||
response=$(curl -s --max-time 35 \
|
|
||||||
"https://api.telegram.org/bot${BOT_TOKEN}/getUpdates?offset=${offset}&timeout=30" 2>/dev/null)
|
|
||||||
[ -z "$response" ] && sleep 2 && continue
|
|
||||||
local ok; ok=$(echo "$response" | jq -r '.ok // "false"')
|
|
||||||
[ "$ok" != "true" ] && sleep 5 && continue
|
|
||||||
local cnt; cnt=$(echo "$response" | jq '.result | length')
|
|
||||||
for (( i=0; i<cnt; i++ )); do
|
|
||||||
local upd; upd=$(echo "$response" | jq ".result[$i]")
|
|
||||||
local uid; uid=$(echo "$upd" | jq -r '.update_id')
|
|
||||||
offset=$((uid + 1))
|
|
||||||
local cbd; cbd=$(echo "$upd" | jq -r '.callback_query.data // empty')
|
|
||||||
if [ -n "$cbd" ]; then
|
|
||||||
local cbi cci cmi
|
|
||||||
cbi=$(echo "$upd" | jq -r '.callback_query.id')
|
|
||||||
cci=$(echo "$upd" | jq -r '.callback_query.message.chat.id')
|
|
||||||
cmi=$(echo "$upd" | jq -r '.callback_query.message.message_id')
|
|
||||||
if ! is_bot_admin "$cci"; then tg_answer_cb "$cbi" "Нет доступа" > /dev/null; continue; fi
|
|
||||||
bot_handle_callback "$cci" "$cmi" "$cbi" "$cbd"
|
|
||||||
else
|
|
||||||
local mci mtx
|
|
||||||
mci=$(echo "$upd" | jq -r '.message.chat.id // empty')
|
|
||||||
mtx=$(echo "$upd" | jq -r '.message.text // empty')
|
|
||||||
if [ -n "$mci" ] && [ -n "$mtx" ]; then
|
|
||||||
if ! is_bot_admin "$mci"; then tg_send "$mci" "⛔ Нет доступа.\nChat ID: <code>$mci</code>" "" > /dev/null; continue; fi
|
|
||||||
if [[ "$mtx" == "/start" || "$mtx" == "/menu" ]]; then
|
|
||||||
bot_main_menu "$mci"
|
|
||||||
else
|
|
||||||
tg_send "$mci" "Используйте /start или /menu" "" > /dev/null
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bot_poll_cycle() {
|
||||||
|
local response
|
||||||
|
response=$(curl -s --max-time 35 \
|
||||||
|
"https://api.telegram.org/bot${BOT_TOKEN}/getUpdates?offset=${offset}&timeout=30" 2>/dev/null)
|
||||||
|
[ -z "$response" ] && sleep 2 && return
|
||||||
|
|
||||||
|
local header
|
||||||
|
header=$(jq -r '[.ok // "false", (.result | length)] | @tsv' <<< "$response" 2>/dev/null)
|
||||||
|
local ok cnt
|
||||||
|
IFS=$'\t' read -r ok cnt <<< "$header"
|
||||||
|
[ "$ok" != "true" ] && sleep 5 && return
|
||||||
|
[ "$cnt" -eq 0 ] 2>/dev/null && return
|
||||||
|
|
||||||
|
local updates_tsv
|
||||||
|
updates_tsv=$(jq -r '
|
||||||
|
.result[] | [
|
||||||
|
.update_id,
|
||||||
|
(.callback_query.data // ""),
|
||||||
|
(.callback_query.id // ""),
|
||||||
|
(.callback_query.message.chat.id // ""),
|
||||||
|
(.callback_query.message.message_id // ""),
|
||||||
|
(.message.chat.id // ""),
|
||||||
|
(.message.text // "")
|
||||||
|
] | @tsv
|
||||||
|
' <<< "$response" 2>/dev/null)
|
||||||
|
|
||||||
|
while IFS=$'\t' read -r uid cbd cbi cci cmi mci mtx; do
|
||||||
|
[ -z "$uid" ] && continue
|
||||||
|
offset=$((uid + 1))
|
||||||
|
if [ -n "$cbd" ]; then
|
||||||
|
if ! is_bot_admin "$cci"; then tg_answer_cb "$cbi" "Нет доступа" > /dev/null; continue; fi
|
||||||
|
bot_handle_callback "$cci" "$cmi" "$cbi" "$cbd"
|
||||||
|
elif [ -n "$mci" ] && [ -n "$mtx" ]; then
|
||||||
|
if ! is_bot_admin "$mci"; then tg_send "$mci" "⛔ Нет доступа.\nChat ID: <code>$mci</code>" "" > /dev/null; continue; fi
|
||||||
|
if [[ "$mtx" == "/start" || "$mtx" == "/menu" ]]; then
|
||||||
|
bot_main_menu "$mci"
|
||||||
|
else
|
||||||
|
tg_send "$mci" "Используйте /start или /menu" "" > /dev/null
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done <<< "$updates_tsv"
|
||||||
|
}
|
||||||
|
|
||||||
# ─── Bot menu (SSH) ──────────────────────────────────────────
|
# ─── Bot menu (SSH) ──────────────────────────────────────────
|
||||||
|
|
||||||
start_bot() {
|
start_bot() {
|
||||||
|
|||||||
Reference in New Issue
Block a user