-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.sh
executable file
·214 lines (184 loc) · 4.31 KB
/
bot.sh
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env dash
#
# Copyright (C) 2024 Maria Lisina
# Copyright (C) 2024 Danil Lisin
# SPDX-License-Identifier: Apache-2.0
#
# Run this software with `env -i` to avoid variable conflict
set -e
umask 77
version="2.0-inadev"
dir="${0%/*}"
cache="${dir}/cache/${$}"
config="${dir}/config"
modules="${dir}/modules"
submodules="${dir}/submodules"
units="${dir}/units"
offset=-1
if [ -n "${1}" ]
then
while getopts hla:s:i:e: options
do
case "${options}" in
(h)
help=0
;;
(l)
local=0
;;
(a)
address="${OPTARG}"
;;
(s)
size=${OPTARG}
;;
(i)
internal="${OPTARG}"
;;
(e)
external="${OPTARG}"
;;
(*)
echo "See '${0} -h'"
exit 1
;;
esac
done
shift $((OPTIND - 1))
else
help=0
fi
if [ -n "${help}" ]
then
echo "Sekoohaka Bot v${version}" \
"\n\nUsage: ${0} [options] [token]" \
"\n\nOptions:" \
"\n -h\t\tShow help information" \
"\n -l\t\tSame as -a localhost:8081 -s 20971520" \
"\n -a <addr>\tTelegram Bot API address, default: api.telegram.org" \
"\n -s <size>\tMax file size allowed to send with URL, default: 10485760" \
"\n -i <addr>\tInternal proxy address to interact with Telegram Bot API" \
"\n -e <addr>\tExternal proxy address to interact with anything else"
exit 0
fi
for required in busybox curl jq recode
do
if ! command -v ${required} > /dev/null
then
missing="${missing} ${required}"
fi
done
if [ -n "${missing}" ]
then
echo "Missing dependencies:${missing}" \
"\nFor more information follow: https://command-not-found.com/"
exit 1
fi
for function in base64 bc cat cut date find grep ls mkdir rm sed seq sha1sum sleep stat tr
do
if busybox ${function} --help > /dev/null 2>&1
then
alias ${function}="busybox ${function}"
else
missing="${missing} ${function}"
fi
done
if [ -n "${missing}" ]
then
echo "Missing BusyBox functions:${missing}" \
"\nUpdate your BusyBox or get a version with all the required functions"
exit 1
fi
if [ -n "${local}" ]
then
address="${address:-127.0.0.1:8081}"
size=${size:-20971520}
fi
if [ -z "${address}" ]
then
address="https://api.telegram.org"
fi
if [ -n "${size}" ]
then
if ! test ${size} -gt 0 > /dev/null 2>&1
then
echo "Illegal file size"
exit 1
fi
else
size=10485760
fi
if [ -n "${1}" ]
then
token="${1}"
shift
fi
if [ -n "${1}" ]
then
echo "Unrecognized action ${1}. See '${0} -h'"
exit 1
fi
until [ -n "${token}" ]
do
read -p "Telegram Bot API Token: " -r token
done
alias parameter="cut -d ' ' -f"
alias enhash="sha1sum | parameter 1"
alias htmlescape="sed -e 's/</\</g' -e 's/>/\>/g'"
alias urlencode="jq -Rr @uri"
for ascii in $(seq 33 126)
do
ascii_table="${ascii_table} $(printf "%b" "\0$(printf "%o" ${ascii})")"
done
rm -fr "${cache}"
mkdir -p "${cache}"
mkdir -p "${config}"
echo "PID: ${$}"
curl --get \
--max-time 10 \
--output "${cache}/getMe.json" \
--proxy "${internal}" \
--show-error \
--silent \
"${address}/bot${token}/getMe"
if ! jq -e '.' "${cache}/getMe.json" > /dev/null
then
cat "${cache}/getMe.json"
exit 1
fi
username="$(jq -r '.result.username' "${cache}/getMe.json")"
if [ "${username}" = "null" ]
then
echo "Failed to authorize the bot"
exit 1
fi
echo "Bot: ${username}"
while trap 'wait && exit 0' INT TERM
do
if ! curl --data "offset=${offset}" \
--get \
--output "${cache}/getUpdates.json" \
--proxy "${internal}" \
--silent \
"${address}/bot${token}/getUpdates"
then
continue
fi
if ! update_id="$(jq -r '.result.[0].update_id' "${cache}/getUpdates.json")"
then
continue
fi
if [ "${update_id}" != "null" ]
then
update="${cache}/${update_id}.json"
if ! jq '.result.[0]' "${cache}/getUpdates.json" > "${update}"
then
continue
fi
for module in "${modules}"/*
do
. "${module}" &
done
offset=$((update_id + 1))
fi
done