/
telegram-bot-daemon.sh
136 lines (111 loc) · 4.25 KB
/
telegram-bot-daemon.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
#!/bin/sh
. /system/sdcard/scripts/common_functions.sh
CURL="/system/sdcard/bin/curl"
LASTUPDATEFILE="/tmp/last_update_id"
TELEGRAM="/system/sdcard/bin/telegram"
JQ="/system/sdcard/bin/jq"
. /system/sdcard/config/telegram.conf
[ -z $apiToken ] && echo "api token not configured yet" && exit 1
[ -z $userChatId ] && echo "chat id not configured yet" && exit 1
status() {
$TELEGRAM m "Motion detection `motion_detection status`\nNight mode `night_mode status`\nAlert type `get_config /system/sdcard/config/motion.conf telegram_alert_type`"
}
sendShot() {
/system/sdcard/bin/getimage > "/tmp/telegram_image.jpg" &&\
$TELEGRAM p "/tmp/telegram_image.jpg"
rm "/tmp/telegram_image.jpg"
}
sendMem() {
$TELEGRAM m $(free -k | awk '/^Mem/ {print "Mem: used "$3" free "$4} /^Swap/ {print "Swap: used "$3}')
}
nightOn() {
night_mode on && $TELEGRAM m "Night mode active"
}
nightOff() {
night_mode off && $TELEGRAM m "Night mode inactive"
}
detectionOn() {
motion_detection on && $TELEGRAM m "Motion detection started"
}
detectionOff() {
motion_detection off && $TELEGRAM m "Motion detection stopped"
}
textAlerts() {
rewrite_config /system/sdcard/config/motion.conf telegram_alert_type "text"
$TELEGRAM m "Text alerts on motion detection enabled"
}
imageAlerts() {
rewrite_config /system/sdcard/config/motion.conf telegram_alert_type "image"
$TELEGRAM m "Image alerts on motion detection enabled"
}
videoAlerts() {
rewrite_config /system/sdcard/config/motion.conf telegram_alert_type "video"
$TELEGRAM m "Video alerts on motion detection enabled"
}
imageThenVideoAlerts() {
rewrite_config /system/sdcard/config/motion.conf telegram_alert_type "video+image"
$TELEGRAM m "Image then video alerts on motion detection enabled"
}
respond() {
cmd=$1
[ $chatId -lt 0 ] && cmd=${1%%@*}
case $cmd in
/status) status;;
/mem) sendMem;;
/shot) sendShot;;
/on) detectionOn;;
/off) detectionOff;;
/nighton) nightOn;;
/nightoff) nightOff;;
/textalerts) textAlerts;;
/imagealerts) imageAlerts;;
/videoalerts) videoAlerts;;
/dualalerts) imageThenVideoAlerts;;
/help | /start) $TELEGRAM m "######### Bot commands #########\n# /mem - show memory information\n# /status - show current camera status\n# /shot - take a snapshot\n# /on - motion detection on\n# /off - motion detection off\n# /nighton - night mode on\n# /nightoff - night mode off\n# /textalerts - Text alerts on motion detection\n# /imagealerts - Image alerts on motion detection\n# /videoalerts - Video alerts on motion detection\n# /dualalerts - Image snapshot then video alerts on motion detection";;
/*) $TELEGRAM m "I can't respond to '$cmd' command"
esac
}
readNext() {
lastUpdateId=$(cat $LASTUPDATEFILE || echo "0")
json=$($CURL -s -X GET "https://api.telegram.org/bot$apiToken/getUpdates?offset=$lastUpdateId&limit=1&allowed_updates=message")
echo $json
}
markAsRead() {
nextId=$(($1 + 1))
echo "$nextId" > $LASTUPDATEFILE
}
main() {
json=$(readNext)
[ -z "$json" ] && return 0
if [ "$(echo "$json" | $JQ -r '.ok')" != "true" ]; then
echo "$(date '+%F %T') Bot error: $json" >> /tmp/telegram.log
[ "$(echo "$json" | $JQ -r '.error_code')" == "401" ] && return 1
return 0
fi;
messageAttr="message"
messageVal=$(echo "$json" | $JQ -r '.result[0].message // ""')
[ -z "$messageVal" ] && messageAttr="edited_message" && messageVal=$(echo "$json" | $JQ -r '.result[0].edited_message // ""')
[ -z "$messageVal" ] && messageAttr="channel_post"
chatId=$(echo "$json" | $JQ -r ".result[0].$messageAttr.chat.id // \"\"")
updateId=$(echo "$json" | $JQ -r '.result[0].update_id // ""')
if [ "$updateId" != "" ] && [ -z "$chatId" ]; then
markAsRead $updateId
return 0
fi;
[ -z "$chatId" ] && return 0 # no new messages
cmd=$(echo "$json" | $JQ -r ".result[0].$messageAttr.text // \"\"")
if [ "$chatId" != "$userChatId" ]; then
username=$(echo "$json" | $JQ -r ".result[0].$messageAttr.from.username // \"\"")
firstName=$(echo "$json" | $JQ -r ".result[0].$messageAttr.from.first_name // \"\"")
# Uncomment to get notified of attempted chat spam
# $TELEGRAM m "Received message from unauthorized chat id: $chatId\nUser: $username($firstName)\nMessage: $cmd"
else
respond $cmd
fi;
markAsRead $updateId
}
while true; do
main >/dev/null 2>&1
[ $? -gt 0 ] && exit 1
sleep 2
done;