-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmahov2.api
executable file
·127 lines (110 loc) · 3.53 KB
/
mahov2.api
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
#!/bin/bash
# Bot Username (w/o @)
BOT_USERNAME=""
# Telegram APIs
TOKEN=''
# APIs
URL='https://api.telegram.org/bot'$TOKEN
# Log file
LOG='./bot.log'
# Default TZ and locale
export LC_ALL=zh_CN.utf-8
export TZ=Asia/Shanghai
# Logger
function log {
echo "[$(date)] $$ $*" >> $LOG
}
#### End of Configurable options ####
echo "Content-type: text/html" && echo
# Checks
[ -z "$REMOTE_ADDR" ] && {
echo "REMOTE_ADDR not set, are you running from cli?"
exit 0
}
[[ ! "$QUERY_STRING" == "bot$TOKEN" ]] && {
log "Invalid token from $REMOTE_ADDR."
exit 0
}
[ -z "$QUERY_STRING_POST" ] && {
log "Correct token from $REMOTE_ADDR but empty body. CGI misconfigured?"
exit 0
}
# Plugins settings
PLUGINS_CONF='./plugins.json'
# find_handler: Find plugins to handle command
function find_handler {
jq -cr 'to_entries [] | select(.value | index("'$1'")) | .key' "$PLUGINS_CONF"
}
# Message inspector: functions added by add_msg_inspector will be call everytime bot gets a message
function add_msg_inspector {
MSG_ISPC="$MSG_ISPC $*"
}
# msg_send: Send Message
function msg_send {
[ -z "$1" ] && {
log "didn't sent message, body is empty."
return 1
}
send=$(iconv -f utf-8 -t utf-8//IGNORE <<< "$1")
for arg in $msg_send_OPTIONS; do earg="$earg -F $arg"; done
[[ ! $c_type == "private" && -z "$NO_REPLY" ]] && earg="$earg -F reply_to_message_id=$mid"
log "API reply: $(curl -s "$URL/sendMessage" $earg -F "chat_id=$cid" -F "text=$send")"
}
# Load inspectors
for INSP in inspectors/*; do {
. $INSP
}; done
# Init bot environment
jq -cr '.message' <<< "$QUERY_STRING_POST" | {
read msg
[ "$msg" = null ] && {
log "No message was got from update. Maybe it is not a 'message'. Try set allowed_updates in setWebhook."
exit 0
}
jq -cr '.message_id, .text, .chat, .from' <<< "$msg" | {
read mid; read text; read chat; read from
jq -cr '.id, .first_name, .last_name, .username, .language_code' <<< "$from" | {
read uid; read u_fn; read u_ln; read u_un; read u_lang
jq -cr '.id, .type, .title, .username, .first_name, .last_name' <<< "$chat" | {
read cid; read c_type; read c_title; read c_un; read c_fn; read c_ln
# Init env done, message info stored as:
# $text : Message Text
# $mid : Message ID
# $uid : User ID
# $u_fn : User firstname
# $u_ln : User lastname
# $u_un : Username
# $u_lang : User language
# $cid : Chat ID
# $c_type : Chat type
# $c_title : Chat title
# $c_un : Chat username
# $c_fn : Chat firstname
# $c_ln : Chat Lastname
[[ -z "$text" || "$text" == "null" ]] && {
log "Empty Message? from $uid@$cid."
exit 0
}
log "Incoming message: uid $uid|cid $cid|mid $mid|\"$text\""
for ispc in $MSG_ISPC; do {
$ispc
}; done
[[ ! "$text" == '/'* ]] && exit 0
cmd="$(cut -d' ' -f1 <<< "$text")"
[[ "$cmd" == *"@"* ]] && {
to_bot="$(cut -d'@' -f2 <<< "$cmd")"
[[ ! "$to_bot" == "$BOT_USERNAME" ]] && exit 0
}
[ -z "$cmd_body" ] && cmd_body="$(awk -F"$cmd " '{print $2}' <<< "$text")"
cmd="$(sed -e 's/@'$BOT_USERNAME'//; s/\///;' <<< "$cmd")"
handler_cmd="$(find_handler "$cmd")"
[ -z $handler_cmd ] && {
log "No handler for $cmd, exiting"
exit 0
}
log "Procressing: cmd $cmd|body \"$cmd_body\"|handler $handler_cmd"
. $handler_cmd
}
}
}
}