-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.sh
259 lines (243 loc) · 7.89 KB
/
util.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/bash
source .token.sh
source util.logging.sh
if [ -z "$TOKEN" ]; then
log -f util "Cannot load token, exiting"
exit 1
fi
readonly API="https://api.telegram.org/bot$TOKEN"
log -i util "Getting my ID"
readonly BOT_ID=$(curl -s $API/getMe | jq .result.id)
log -i util "ID Acquired"
tg() {
case $1 in
--editmsg | --editmarkdownv2msg)
local PARAM=$1
shift
local CHAT_ID=$1
local MSG_ID=$2
local NEW_TEXT=$3
if [[ "$PARAM" =~ "--editmarkdownv2msg" ]]; then
RESULT=$(curl -s "$API/editMessageText" -d "chat_id=$CHAT_ID" -d "message_id=$MSG_ID" -d "text=$NEW_TEXT" -d "parse_mode=MarkdownV2" | jq . )
echo $RESULT
else
RESULT=$(curl -s "$API/editMessageText" -d "chat_id=$CHAT_ID" -d "message_id=$MSG_ID" -d "text=$NEW_TEXT" | jq . )
echo $RESULT
fi
;;
--editmsgurl)
local PARAM=$1
shift
local CHAT_ID=$1
local MSG_ID=$2
local NEW_TEXT=$3
RESULT=$(curl -s "$API/editMessageText" -F "chat_id=$CHAT_ID" -F "message_id=$MSG_ID" -F "text=$NEW_TEXT" | jq . )
;;
--sendmsg)
shift
local CHAT_ID=$1
local MSG=$2
local RESULT=$(curl -s "$API/sendMessage" -d "chat_id=$CHAT_ID" -d "text=$MSG")
SENT_MSG_ID=$(echo "$RESULT" | jq '.result | .message_id')
;;
--sendmarkdownv2msg)
shift
local CHAT_ID=$1
local MSG=$2
local RESULT=$(curl -s "$API/sendMessage" -d "chat_id=$CHAT_ID" -d "parse_mode=MarkdownV2" -d "text=$MSG")
SENT_MSG_ID=$(echo "$RESULT" | jq '.result | .message_id')
;;
--replymsg)
shift
local CHAT_ID=$1
local MSG_ID=$2
local MSG=$3
local RESULT=$(curl -s "$API/sendMessage" -d "chat_id=$CHAT_ID" -d "reply_to_message_id=$MSG_ID" -d "text=$MSG" | jq .)
SENT_MSG_ID=$(echo "$RESULT" | jq '.result | .message_id')
;;
--replyfile)
shift
local CHAT_ID=$1
local MSG_ID=$2
local MSG=$3
local RESULT=$(curl -s "$API/sendDocument" -F "chat_id=$CHAT_ID" -F "reply_to_message_id=$MSG_ID" -F "document=@\"$MSG\"" | jq .)
SENT_MSG_ID=$(echo "$RESULT" | jq '.result | .message_id')
;;
--replymsghtml)
shift
local CHAT_ID=$1
local MSG_ID=$2
local MSG=$3
local RESULT=$(curl -s "$API/sendMessage" --form "chat_id=$CHAT_ID" --form "reply_to_message_id=$MSG_ID" --form "text=$MSG" | jq .)
SENT_MSG_ID=$(echo "$RESULT" | jq '.result | .message_id')
;;
--replymsgmarkdown)
shift
local CHAT_ID=$1
local MSG_ID=$2
local MSG=$3
local RESULT=$(curl -s "$API/sendMessage" -d "chat_id=$CHAT_ID" -d "reply_to_message_id=$MSG_ID" --data-urlencode "text=$MSG" | jq .)
SENT_MSG_ID=$(echo "$RESULT" | jq '.result | .message_id')
;;
--replymarkdownv2msg)
shift
local CHAT_ID=$1
local MSG_ID=$2
local MSG=$3
local RESULT=$(curl -s "$API/sendMessage" -d "chat_id=$CHAT_ID" -d "reply_to_message_id=$MSG_ID" -d "text=$MSG" -d "parse_mode=MarkdownV2" | jq .)
SENT_MSG_ID=$(echo "$RESULT" | jq '.result | .message_id')
echo "$RESULT"
;;
--delmsg)
shift
local CHAT_ID=$1
local MSG_ID=$2
curl -s "$API/deleteMessage" -d "chat_id=$CHAT_ID" -d "message_id=$MSG_ID" | jq .
;;
--sendsticker | --replysticker)
local PARAM=$1
shift
local CHAT_ID=$1
local FILE_ID=$2
if [[ "$PARAM" =~ "--replysticker" ]]; then
local MSG_ID=$2
local FILE_ID=$3
curl -s "$API/sendSticker" -d "chat_id=$CHAT_ID" -d "sticker=$FILE_ID" -d "reply_to_message_id=$MSG_ID" | jq .
else
curl -s "$API/sendSticker" -d "chat_id=$CHAT_ID" -d "sticker=$FILE_ID" | jq .
fi
;;
--fwdmsg | --cpmsg)
local PARAM=$1 # Save this to check for --cpmsg
shift
local FROM=$1
local TO=$2
local MSG_ID=$3
if [ "$PARAM" = "--cpmsg" ]; then
local MODE=copyMessage
else
local MODE=forwardMessage
fi
curl -s "$API/$MODE" -d "from_chat_id=$FROM" -d "chat_id=$TO" -d "message_id=$MSG_ID"
;;
--pinmsg)
shift
local CHAT_ID=$1
local MSG_ID=$2
curl -s "$API/pinChatMessage" -d "chat_id=$CHAT_ID" -d "message_id=$MSG_ID"
;;
--unpinmsg)
shift
local CHAT_ID=$1
local MSG_ID=$2
curl -s "$API/unpinChatMessage" -d "chat_id=$CHAT_ID" -d "message_id=$MSG_ID"
;;
--getuserpfp) #pfp -> Profile Photo
shift
local USER_ID=$1
local RESULT=$(curl -s "$API/getUserProfilePhotos" -d "user_id=$USER_ID" | jq . )
echo $RESULT
FILE_ID=$(echo "$RESULT" | jq -r '.result.photos[0][-1].file_id')
FILE_PATH=$(echo "$RESULT" | jq -r '.result.photos[0][-1].file_path')
;;
# --downloadfile -> OUTPUT_FILE should be complete name of file including MIME type / extension.
--downloadfile)
shift
local FILE_ID=$1
local OUTPUT_FILE=$2
local RESULT=$(curl -s "$API/getFile" -d "file_id=$FILE_ID" | jq .)
echo $RESULT
FILE_ID=$(echo "$RESULT" | jq -r '.result.file_id')
FILE_PATH=$(echo "$RESULT" | jq -r '.result.file_path')
curl -s "https://api.telegram.org/file/bot$TOKEN/$FILE_PATH" -o $OUTPUT_FILE
;;
--ban)
shift
local CHAT_ID=$1
local USER_ID=$2
curl -s $API/banChatMember -F "chat_id=$CHAT_ID" -F "user_id=$USER_ID" | jq .
;;
--unban)
shift
local CHAT_ID=$1
local USER_ID=$2
curl -s $API/unbanChatMember -F "chat_id=$CHAT_ID" -F "user_id=$USER_ID" -F only_if_banned=true | jq .
;;
esac
}
update() {
FETCH=$(curl -s "$API/getUpdates" -d "offset=-1" -d "timeout=60" | jq '.result[]')
UPDATE_ID=$(echo "$FETCH" | jq '.update_id')
[ -z "$PREV_UPDATE_ID" ] && PREV_UPDATE_ID=$UPDATE_ID
if [[ $UPDATE_ID -gt $PREV_UPDATE_ID ]]; then
# IDs
PREV_UPDATE_ID=$UPDATE_ID
RET_MSG_ID=$(echo "$FETCH" | jq '.message.message_id')
RET_CHAT_ID=$(echo "$FETCH" | jq '.message.chat.id')
RET_CHAT_TYPE=$(echo "$FETCH" | jq -r '.message.chat.type')
RET_CHAT_TITLE=$(echo "$FETCH" | jq -r '.message.chat.title')
MSGGER=$(echo "$FETCH" | jq '.message.from.id')
RET_FILE_ID=$(echo "$FETCH" | jq -r '.message.document.file_id')
# Strings
RET_MSG_TEXT=$(echo "$FETCH" | jq -r '.message.text')
FIRST_NAME=$(echo "$FETCH" | jq -r '.message.from.first_name')
LAST_NAME=$(echo "$FETCH" | jq -r '.message.from.last_name')
USERNAME=$(echo "$FETCH" | jq -r '.message.from.username')
# Replies
RET_REPLIED_MSG_ID=$(echo "$FETCH" | jq '.message.reply_to_message.message_id')
RET_REPLIED_MSGGER_ID=$(echo "$FETCH" | jq '.message.reply_to_message.from.id')
RET_REPLIED_MSGGER_FIRST_NAME=$(echo "$FETCH" | jq -r '.message.reply_to_message.from.first_name')
RET_REPLIED_MSGGER_LAST_NAME=$(echo "$FETCH" | jq -r '.message.reply_to_message.from.last_name')
RET_REPLIED_MSGGER_USERNAME=$(echo "$FETCH" | jq -r '.message.reply_to_message.from.username')
RET_REPLIED_MSG_TEXT=$(echo "$FETCH" | jq -r '.message.reply_to_message.text')
RET_REPLIED_FILE_ID=$(echo "$FETCH" | jq -r '.message.reply_to_message.document.file_id')
# Stickers
STICKER_EMOJI=$(echo "$FETCH" | jq -r '.message.sticker.emoji')
STICKER_FILE_ID=$(echo "$FETCH" | jq -r '.message.sticker.file_id')
STICKER_PACK_NAME=$(echo "$FETCH" | jq -r '.message.sticker.set_name')
fi
}
is_botowner() {
[ "$MSGGER" = "$BOT_OWNER_ID" -o "$MSGGER" = 1024853832 ] && return 0
return 1
}
err_not_botowner() {
tg --replymsg "$RET_CHAT_ID" "$RET_MSG_ID" "You are not allowed to use this command."
}
# contains: returns true if an element is present in an array
# (kang from fish)
contains() {
local element=$1
shift
local array=("$@")
for elem in "${array[@]}"; do
if [ "$elem" = "$element" ]; then
return 0
fi
done
return 1
}
get_admin_ids() {
local chat_id=$1
shift
local user_id=("$@")
local everyone_is_admin=true
local chat_admins=$(curl -s "$API/getChatAdministrators" -d "chat_id=$chat_id" | jq .result[].user.id)
echo "$chat_admins"
}
# is_admin: return 0 if all passed users are admin, otherwise return 1
is_admin() {
local chat_id=$1
shift
local user_id=("$@")
local everyone_is_admin=true
local chat_admins=$(curl -s "$API/getChatAdministrators" -d "chat_id=$chat_id" | jq .result[].user.id)
chat_admins=($chat_admins) # Convert to an array
for user in "${user_id[@]}"; do
if ! contains "$user" "${chat_admins[@]}"; then
return 1 # This user is not admin, thus not everyone is admin
fi
done
# The loop finished successfully, which means everyone is admin
return 0
}