Skip to content

Instantly share code, notes, and snippets.

@myfingerhurt
Created October 2, 2021 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myfingerhurt/dd9d2f230c10d1e29f3162fb31b0d54d to your computer and use it in GitHub Desktop.
Save myfingerhurt/dd9d2f230c10d1e29f3162fb31b0d54d to your computer and use it in GitHub Desktop.
WAN instant message notification based on Matrix-bot much better than E-mail notification for AC68U AC86U AX86U (Asus Merlinwrt)
#!/bin/sh
#-----------Server1 matrix.org-----------------
#ACCESS_TOKEN="Very-Long-Matrix-Token-1"
#HOME_SERVER="matrix.org"
#CURL_EX_PARAM=" --resolve matrix.org:80:1.1.1.1 --resolve matrix.org:443:1.1.1.1"
#-----------Server2 tchncs.de-----------------
ACCESS_TOKEN="Very-Long-Matrix-Token-2"
HOME_SERVER="tchncs.de"
#-----------Fixed TOKEN-----------------
ROOMID="!YourRoomID:matrix.org"
SENDER_NAME="Matrix Online Notification"
#----------------------------------------------
LOGS_IPV4=$(curl -s -S --max-time 10 "https://api4.my-ip.io/ip")
LOGS_IPV6=$(curl -s -S --max-time 10 "https://api6.my-ip.io/ip")
#-----------Message to Riot.im-----------------
echo -n "{\"msgtype\":\"m.text\", \"body\":\"" >/tmp/msg.json
echo -n "\\n\\n\\n\\n=========\\n" >>/tmp/msg.json
echo -n "Matrix: "$HOME_SERVER"\\n" >>/tmp/msg.json
echo -n "From "$SENDER_NAME".\\n" >>/tmp/msg.json
echo -n "I just got connected to the internet.\\n" >>/tmp/msg.json
echo -n "My WAN IP is: `nvram get wan0_ipaddr`\\n" >>/tmp/msg.json
echo -n "My EXT IPv4 is: "$LOGS_IPV4"\\n" >>/tmp/msg.json
echo -n "My EXT IPv6 is: "$LOGS_IPV6"\\n" >>/tmp/msg.json
echo -n "Uptime is: `uptime | cut -d ',' -f1 | sed 's/^.\{12\}//g' | tr -d ','`\\n">>/tmp/msg.json
echo -n "Called By: "$1" \\n" >>/tmp/msg.json
echo -n "---- \\n" >>/tmp/msg.json
echo -n "Your friendly neighborhood." >>/tmp/msg.json
echo -n "\"}" >>/tmp/msg.json
#curl -s --max-time 5 -XPOST -H "Content-Type: application/json" -d @"/tmp/msg.json" "https://matrix.org/_matrix/client/r0/rooms/$ROOMID/send/m.room.message?access_token=$ACCESS_TOKEN"\
# 2>&1 | tee /tmp/matrixbot.log
LOGS1=$(curl $CURL_EX_PARAM -s -S --max-time 10 -XPOST -H "Content-Type: application/json" -d @"/tmp/msg.json" "https://$HOME_SERVER/_matrix/client/r0/rooms/$ROOMID/send/m.room.message?access_token=$ACCESS_TOKEN")
#"event_id":"$1562237636108192tixgZ:matrix.org"
#success=$(cat /tmp/matrixbot.log | grep 'event_id'| tr -d '\t'|tr -d ' '|tr -d '\n' )
SUCCESS=$(echo $LOGS1| awk '/event_id.+/i'|tr -d '\"\t \n\r')
#is of nonzero length
if [ -n "$SUCCESS" ]
then
cru d matrixbot;
echo "Success! "$SUCCESS"" | tee /dev/tty | logger -t "matrixbot" -p user.notice;
else
error=$(echo $LOGS1 | grep -e 'errcode' -e 'curl'| tr -d '\t\n\r');
echo "Failed! "$error"" | tee /dev/tty | logger -t "matrixbot" -p user.notice;
echo "Raw Log "$LOGS1""
# if token failed, give up
if [ `echo $error | grep -c "TOKEN" ` -gt 0 ]; then
cru d matrixbot;
fi
fi
rm /tmp/msg.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment