aboutsummaryrefslogtreecommitdiff
path: root/handlers/callback.zsh
blob: 10a8250adce2a2f46d44e6a34227f933bd72639f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Copyright (C) 2024-2026 Maria Lisina
# Copyright (C) 2024-2026 Danil Lisin
# SPDX-License-Identifier: Apache-2.0

callback_query=($(jq -r '.callback_query.data' <<< "${update}"))

if [[ "${callback_query}" == "null" ]]
then
    exit 0
fi

user_id="$(jq -r '.callback_query.from.id' <<< "${update}")"
query_id="$(jq -r '.callback_query.id' <<< "${update}")"

source "${units}/user.zsh"
set -- ${callback_query[@]}

command="${1}"
shift

case "${command}" in
    ("delete")
        source "${operators}/callback_delete.zsh"
    ;;
    ("help")
        source "${operators}/callback_help.zsh"
    ;;
    ("post")
        source "${operators}/callback_post.zsh"
    ;;
    ("reset")
        source "${operators}/callback_reset.zsh"
    ;;
    ("short")
        source "${operators}/callback_short.zsh"
    ;;
    ("stop")
        source "${operators}/callback_stop.zsh"
    ;;
    ("tags")
        source "${operators}/callback_tags.zsh"
    ;;
    (*)
        exit 0
    ;;
esac

if [[ -z "${notification_text}" && -n "${output_text}" ]]
then
    chat_id="$(jq -r '.callback_query.message.chat.id' <<< "${update}")"
    message_id="$(jq -r '.callback_query.message.message_id' <<< "${update}")"
    inline_message_id="$(jq -r '.callback_query.inline_message_id' <<< "${update}")"

    if [[ "${inline_message_id}" != "null" ]]
    then
        unset chat_id
        unset message_id
    fi

    if ! output_data="$(
        curl --connect-timeout ${connrefused_timeout} \
            --data-urlencode "chat_id=${chat_id}" \
            --data-urlencode "message_id=${message_id}" \
            --data-urlencode "inline_message_id=${inline_message_id}" \
            --data-urlencode "text=${output_text}" \
            --data-urlencode "parse_mode=HTML" \
            --data-urlencode "link_preview_options=${link_preview_options}" \
            --data-urlencode "reply_markup=${reply_markup}" \
            --get \
            --max-time ${internal_timeout} \
            --proxy "${internal_proxy}" \
            --retry 1 \
            --retry-connrefused \
            --retry-max-time $((internal_timeout - connrefused_timeout)) \
            --silent \
            --user-agent "${useragent}" \
            "${api_address}/bot${api_token}/editMessageText"
    )"
    then
        notification_text="Failed to update message"

        log_text="editMessageText (${update_id}): Failed to access Telegram Bot API"
        source "${units}/log.zsh"
    fi

    if [[ -z "${notification_text}" ]] && ! jq -e '.' <<< "${output_data}" > /dev/null
    then
        notification_text="An unknown error occurred"

        log_text="editMessageText (${update_id}): An unknown error occurred"
        source "${units}/log.zsh"
    fi

    if [[ -z "${notification_text}" && "$(jq -r '.ok' <<< "${output_data}")" != "true" ]]
    then
        notification_text="Failed to update message"
        error_description="$(jq -r '.description' <<< "${output_data}")"

        if [[ "${error_description}" != "null" ]]
        then
            log_text="editMessageText (${update_id}): ${error_description}"
        else
            log_text="editMessageText (${update_id}): An unknown error occurred"
        fi

        source "${units}/log.zsh"
    fi
fi

if ! output_data="$(
    curl --connect-timeout ${connrefused_timeout} \
        --data-urlencode "callback_query_id=${query_id}" \
        --data-urlencode "text=${notification_text}" \
        --data-urlencode "cache_time=0" \
        --get \
        --max-time ${internal_timeout} \
        --proxy "${internal_proxy}" \
        --retry 1 \
        --retry-connrefused \
        --retry-max-time $((internal_timeout - connrefused_timeout)) \
        --silent \
        --user-agent "${useragent}" \
        "${api_address}/bot${api_token}/answerCallbackQuery"
)"
then
    log_text="answerCallbackQuery (${update_id}): Failed to access Telegram Bot API"
    source "${units}/log.zsh"

    exit 0
fi

if ! jq -e '.' <<< "${output_data}" > /dev/null
then
    log_text="answerCallbackQuery (${update_id}): An unknown error occurred"
    source "${units}/log.zsh"

    exit 0
fi

if [[ "$(jq -r '.ok' <<< "${output_data}")" != "true" ]]
then
    error_description="$(jq -r '.description' <<< "${output_data}")"

    if [[ "${error_description}" != "null" ]]
    then
        log_text="answerCallbackQuery (${update_id}): ${error_description}"
    else
        log_text="answerCallbackQuery (${update_id}): An unknown error occurred"
    fi

    source "${units}/log.zsh"
fi