aboutsummaryrefslogtreecommitdiff
path: root/handlers/inline.zsh
blob: 8fa0552c91a6a15df5c56a47e905ea23b69c049b (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
# Copyright (C) 2024-2026 Maria Lisina
# Copyright (C) 2024-2026 Danil Lisin
# SPDX-License-Identifier: Apache-2.0

inline_query=($(jq -r '.inline_query.query' <<< "${update}"))

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

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

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

command="${1}"

if [[ -n "${command}" ]]
then
    shift
else
    source "${operators}/inline_none.zsh"
fi

case "${command}" in
    ("help")
        source "${operators}/inline_help.zsh"
    ;;
    ("original")
        source "${operators}/inline_original.zsh"
    ;;
    ("post")
        source "${operators}/inline_post.zsh"
    ;;
    ("short")
        source "${operators}/inline_short.zsh"
    ;;
    ("shorts" | s)
        source "${operators}/inline_shorts.zsh"
    ;;
    (*)
        source "${operators}/inline_search.zsh"
    ;;
esac

if ! output_data="$(
    curl --connect-timeout ${connrefused_timeout} \
        --data-urlencode "inline_query_id=${query_id}" \
        --data-urlencode "results=${results}" \
        --data-urlencode "cache_time=0" \
        --data-urlencode "next_offset=${next_offset}" \
        --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}/answerInlineQuery"
)"
then
    log_text="answerInlineQuery (${update_id}): Failed to access Telegram Bot API"
    source "${units}/log.zsh"

    exit
fi

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

    exit
fi

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

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

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