Skip to content

Instantly share code, notes, and snippets.

@delfer
Last active September 20, 2022 10:35
Show Gist options
  • Save delfer/5b7bdba34d0238573c1b05d5b4ff7210 to your computer and use it in GitHub Desktop.
Save delfer/5b7bdba34d0238573c1b05d5b4ff7210 to your computer and use it in GitHub Desktop.
Flying Bear Ghost 5 Telegram Bot
#!/bin/bash
PRINTER_IP={IP}
API=https://api.telegram.org/bot{TOKEN}
CHAT={CHAT}
STATE_FILE=/run/3d-printer-sate.mon
MESSAGE_ID_FILE=/run/3d-printer-msg-id.mon
# Status table
STATE_OFFLINE=f
STATE_ONLINE=n
STATE_PRINTING=p
function send {
/usr/bin/curl -s -X POST $API/sendMessage -d chat_id="$CHAT" -d text="3D Printer%0A$1" | /usr/bin/jq '.result.message_id' > $MESSAGE_ID_FILE
}
function update {
MESSAGE_ID=$(cat $MESSAGE_ID_FILE)
echo "update $MESSAGE_ID"
if [[ $MESSAGE_ID == ?(-)+([0-9]) ]]; then
/usr/bin/curl -s -X POST $API/editMessageText -d chat_id="$CHAT" -d message_id="$MESSAGE_ID" -d text="3D Printer%0A$1"
fi
}
# Prev state
touch $STATE_FILE
PREV_STATE=$(cat $STATE_FILE)
PREV_STATE=${PREV_STATE:-$STATE_OFFLINE}
echo "Prev state $PREV_STATE"
# Cur state
NC_OUT=$(nc -d -I1 -w5 $PRINTER_IP 8080 | head -c 128 ; echo -e "\n${PIPESTATUS[0]}\n")
NC_CODE=$(echo "$NC_OUT" | tail -n 1)
NC_OUT=$(echo "$NC_OUT" | grep "SD printing" | tail -n 1)
echo "nc out $NC_OUT"
echo "nc code $NC_CODE"
if [ $NC_CODE -ne 0 ]; then
CUR_STATE=$STATE_OFFLINE
elif [[ $NC_OUT == *"Not"* ]]; then
CUR_STATE=$STATE_ONLINE
elif [[ ! -z "$NC_OUT" ]]; then
CUR_STATE=$STATE_PRINTING
PROGRESS_RAW=$(echo "$NC_OUT" | cut -d' ' -f4)
PROGRESS_PART1=$(echo "$PROGRESS_RAW" | cut -d'/' -f1 -s)
PROGRESS_PART2=$(echo "$PROGRESS_RAW" | cut -d'/' -f2 -s)
if [[ ! -z "$PROGRESS_RAW" && $PROGRESS_PART2 -gt 0 && $PROGRESS_PART2 -ge $PROGRESS_PART1 ]]; then
PROGRESS_PERCENT=$(python -c "print ('%.2f' % (100.0*$PROGRESS_RAW))")
fi
else
CUR_STATE=$PREV_STATE
fi
echo "$CUR_STATE" > $STATE_FILE
echo "Cur state $CUR_STATE"
echo "Prgrs raw $PROGRESS_RAW"
echo "Prgrs perc $PROGRESS_PERCENT"
# State machine
if [[ $PREV_STATE == $STATE_OFFLINE && $CUR_STATE == $STATE_OFFLINE ]]; then
true
elif [[ $PREV_STATE == $STATE_OFFLINE && $CUR_STATE == $STATE_ONLINE ]]; then
send "Now online!"
elif [[ $PREV_STATE == $STATE_OFFLINE && $CUR_STATE == $STATE_PRINTING ]]; then
send "Printing started!"
elif [[ $PREV_STATE == $STATE_ONLINE && $CUR_STATE == $STATE_OFFLINE ]]; then
send "Now offline!"
elif [[ $PREV_STATE == $STATE_ONLINE && $CUR_STATE == $STATE_ONLINE ]]; then
true
elif [[ $PREV_STATE == $STATE_ONLINE && $CUR_STATE == $STATE_PRINTING ]]; then
send "Printing started!"
elif [[ $PREV_STATE == $STATE_PRINTING && $CUR_STATE == $STATE_OFFLINE ]]; then
send "Now offline!"
elif [[ $PREV_STATE == $STATE_PRINTING && $CUR_STATE == $STATE_ONLINE ]]; then
send "Printing done!"
elif [[ $PREV_STATE == $STATE_PRINTING && $CUR_STATE == $STATE_PRINTING ]]; then
if [[ ! -z "$PROGRESS_PERCENT" ]]; then
update "printing $PROGRESS_PERCENT%"
fi
else
send "Shit happends [$PREV_STATE]->[$CUR_STATE]"
fi
  1. Add M27 S10 to your starting G-Code
  2. Install prerequirements apt-get install netcat curl python3 && snap install jq
  3. Fill up your printer {IP} PRINTER_IP=192.168.0.3
  4. Create bot and fill up {TOKEN} https://telegrambots.github.io/book/1/quickstart.html
  5. Send any mesage to your bot
  6. Execute https://api.telegram.org/botXXX:YYYY/getUpdates (replace XXX:YYYY with your bot token)
  7. Look for "chat":{"id":-zzzzzzzzzz, -zzzzzzzzzz is your chat id (with the negative sign)
  8. Fill up chat id CHAT=1234567
  9. Copy this script into /opt/ and make it executable chmod +x
  10. Log in as root sudo su
  11. Start crontab editor crontab -e
  12. Add new line * * * * * /opt/3d-printer-status-bot.sh > /run/3d-printer.last 2>&1 and make shure you have one empty line at the end of file
  13. Save

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment