Skip to content

Commit

Permalink
Merge branch 'master' of github.com:EliasKotlyar/Xiaomi-Dafang-Hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jmtatsch committed Nov 4, 2018
2 parents a1c8ff0 + 12d0c89 commit bb5be01
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
53 changes: 53 additions & 0 deletions firmware_mod/controlscripts/telegram-bot
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh

######### Bot commands #########
# /mem - show memory information
# /shot - take a shot
# /on - motion detect on
# /off - motion detect off

PIDFILE="/run/telegram-bot.pid"

if [ ! -f /system/sdcard/config/telegram.conf ]; then
echo "You have to configure telegram first. Please see /system/sdcard/config/telegram.conf.dist for further instructions"
fi

status()
{
pid="$(cat "$PIDFILE" 2>/dev/null)"
if [ "$pid" ]; then
kill -0 "$pid" >/dev/null && echo "PID: $pid" || return 1
fi
}

start()
{
if [ -f $PIDFILE ]; then
echo "Bot already running";
else
echo "Starting bot"
/system/sdcard/bin/busybox nohup /system/sdcard/scripts/telegram-bot-daemon.sh >/dev/null 2>&1 &
echo "$!" > "$PIDFILE"
fi
}

stop()
{
pid="$(cat "$PIDFILE" 2>/dev/null)"
if [ "$pid" ]; then
kill -9 "$pid"
rm "$PIDFILE"
echo "Bot stopped"
else
echo "Could not find a bot to stop."
fi
}

if [ $# -eq 0 ]; then
start
else
case $1 in start|stop|status)
$1
;;
esac
fi
78 changes: 78 additions & 0 deletions firmware_mod/scripts/telegram-bot-daemon.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/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

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}')
}

detectionOn() {
. /system/sdcard/scripts/common_functions.sh
motion_detection on && $TELEGRAM m "Motion detection started"
}

detectionOff() {
. /system/sdcard/scripts/common_functions.sh
motion_detection off && $TELEGRAM m "Motion detection stopped"
}

respond() {
case $1 in
/mem) sendMem;;
/shot) sendShot;;
/on) detectionOn;;
/off) detectionOff;;
*) $TELEGRAM m "I can't respond to '$1' 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)

[ "$(echo "$json" | $JQ -r '.ok')" != "true" ] && return 1

chatId=$(echo "$json" | $JQ -r '.result[0].message.chat.id // ""')
[ -z "$chatId" ] && return 0 # no new messages

cmd=$(echo "$json" | $JQ -r '.result[0].message.text // ""')
updateId=$(echo "$json" | $JQ -r '.result[0].update_id // ""')

if [ "$chatId" != "$userChatId" ]; then
username=$(echo "$json" | $JQ -r '.result[0].message.from.username // ""')
firstName=$(echo "$json" | $JQ -r '.result[0].message.from.first_name // ""')
$TELEGRAM m "Received message from not authrized chat: $chatId\nUser: $username($firstName)\nMessage: $cmd"
else
respond $cmd
fi;

markAsRead $updateId
}

while true; do
main >/dev/null 2>&1
[ $? -gt 0 ] && exit 1
done;

0 comments on commit bb5be01

Please sign in to comment.