blob: d9eb46fbe26a0770ee722296ae11eb9b3ecdfcf9 (
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
|
# Copyright (C) 2024-2026 Maria Lisina
# Copyright (C) 2024-2026 Danil Lisin
# SPDX-License-Identifier: Apache-2.0
if [[ -z "${file_id}" ]]
then
message_id="$(jq -r '.message.reply_to_message.message_id' <<< "${update}")"
file_id="$(jq -r '.message.reply_to_message.photo.[1].file_id' <<< "${update}")"
if [[ "${message_id}" == "null" || "${message_id}" == "${message_thread_id}" ]]
then
output_text="You must use this command in reply to an image"
return
fi
if [[ "${file_id}" == "null" ]]
then
output_text="Could not find image in replied message"
return
fi
fi
if ! output_data="$(
curl --connect-timeout ${connrefused_timeout} \
--data-urlencode "file_id=${file_id}" \
--get \
--max-time ${internal_timeout} \
--proxy "${internal_proxy}" \
--retry 1 \
--retry-connrefused \
--retry-max-time $((external_timeout - connrefused_timeout)) \
--silent \
--user-agent "${useragent}" \
"${api_address}/bot${api_token}/getFile"
)"
then
output_text="Failed to get image file"
log_text="getFile (${update_id}): Failed to access Telegram Bot API"
source "${units}/log.zsh"
return
fi
if ! jq -e '.' <<< "${output_data}" > /dev/null
then
output_text="An unknown error occurred"
log_text="getFile (${update_id}): An unknown error occurred"
source "${units}/log.zsh"
return
fi
if [[ "$(jq -r '.ok' <<< "${output_data}")" != "true" ]]
then
output_text="Failed to get image file"
error_description="$(jq -r '.description' <<< "${output_data}")"
if [[ "${error_description}" != "null" ]]
then
log_text="getFile (${update_id}): ${error_description}"
else
log_text="getFile (${update_id}): An unknown error occurred"
fi
source "${units}/log.zsh"
return
fi
file_path="$(jq -r '.result.file_path' <<< "${output_data}")"
until mkdir "${cache}/${update_id}_file.lock"
do
sleep 1
done
if [[ "${api_address}" != "${local_address}" ]]
then
output_file="${cache}/${update_id}_file.jpg"
if ! curl --connect-timeout ${connrefused_timeout} \
--max-time ${internal_timeout} \
--output "${output_file}" \
--proxy "${internal_proxy}" \
--retry 1 \
--retry-connrefused \
--retry-max-time $((external_timeout - connrefused_timeout)) \
--silent \
--user-agent "${useragent}" \
"${api_address}/file/bot${api_token}/${file_path}"
then
output_text="Failed to download image file"
log_text="getFile (${update_id}): Failed to access Telegram Bot API"
source "${units}/log.zsh"
return
fi
if [[ "$(jq -r '.ok' "${output_file}")" == "false" ]]
then
output_text="Failed to download image file"
error_description="$(jq -r '.description' "${output_file}")"
if [[ "${error_description}" != "null" ]]
then
log_text="getFile (${update_id}): ${error_description}"
else
log_text="getFile (${update_id}): An unknown error occurred"
fi
source "${units}/log.zsh"
return
fi
file_path="${output_file}"
fi
sn_query="file=@${file_path}"
source "${units}/sn_search.zsh"
rmdir "${cache}/${update_id}_file.lock"
|