forked from tilderadio/radiobot
Compare commits
1 Commits
master
...
email-noti
Author | SHA1 | Date |
---|---|---|
Ben Harris | b4dace41ea |
|
@ -1,9 +1,6 @@
|
|||
toot.json
|
||||
*-toot.json
|
||||
input.txt
|
||||
log.txt
|
||||
.bot.cfg
|
||||
|
||||
subscribers.txt
|
||||
email-subscribers.txt
|
||||
now_playing.txt
|
||||
account.ini
|
||||
|
|
|
@ -8,5 +8,3 @@ it:
|
|||
|
||||
drop webhook.php somewhere in a webroot and update `now_playing_file` in bot.properties to point to now_playing.txt in that dir.
|
||||
|
||||
this code is very bad and should probably be rewritten from scratch.
|
||||
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
account=bensbots
|
||||
password=my super secret password
|
||||
|
|
@ -2,8 +2,9 @@ server="localhost"
|
|||
port="6667"
|
||||
npfile="now_playing.txt"
|
||||
channel="tilderadio"
|
||||
notify_channels="meta team club $channel"
|
||||
notify_channels="meta team yourtilde $channel"
|
||||
link="https://tilderadio.org/listen"
|
||||
schedule="https://tilderadio.org/schedule/"
|
||||
source="https://tildegit.org/tilderadio/radiobot"
|
||||
source="https://tildegit.org/ben/radiobot"
|
||||
nick="radiobot"
|
||||
log="log.txt"
|
||||
|
|
303
bot.sh
303
bot.sh
|
@ -1,210 +1,173 @@
|
|||
#!/usr/bin/env bash
|
||||
#!/bin/bash
|
||||
|
||||
# read configs
|
||||
. bot.properties
|
||||
input=".bot.cfg"
|
||||
echo "Starting session: $(date "+[%y:%m:%d %T]")">$log
|
||||
echo "NICK $nick" > $input
|
||||
echo "USER $nick 0 * :$nick" >> $input
|
||||
echo "MODE $nick +B" >> $input
|
||||
|
||||
# not registered and identified
|
||||
reg=""
|
||||
|
||||
# reset input file
|
||||
rm input.txt && touch input.txt
|
||||
|
||||
printf "starting %s: %s\n" "$nick" "$(date "+[%y:%m:%d %T]")"
|
||||
|
||||
function send {
|
||||
printf "> %s: %s\n" "$(date "+[%y:%m:%d %T]")" "$1"
|
||||
printf "%b\r\n" "$1" >> input.txt
|
||||
}
|
||||
|
||||
function msg {
|
||||
send "PRIVMSG $1 :$2"
|
||||
function save_info {
|
||||
now_playing=$(<$npfile)
|
||||
}
|
||||
|
||||
function parse_dj {
|
||||
grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs
|
||||
grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs
|
||||
}
|
||||
|
||||
# save current dj info on startup
|
||||
now_playing=$(<$npfile)
|
||||
function msg {
|
||||
echo "PRIVMSG ${1} :${2}" >> $input
|
||||
}
|
||||
|
||||
# save info
|
||||
save_info
|
||||
dj=$(parse_dj)
|
||||
|
||||
# connect and register
|
||||
send "NICK $nick"
|
||||
send "USER $nick 0 * :$nick"
|
||||
send "MODE $nick +B"
|
||||
|
||||
# main loop
|
||||
tail -f input.txt | telnet $server $port | while read -r buf
|
||||
tail -f $input | telnet $server $port | while read res
|
||||
do
|
||||
# trim \r's
|
||||
line=$(printf %b "$buf" | tr -d $'\r')
|
||||
# log the session
|
||||
echo "$(date "+[%y:%m:%d %T]")$res" >> $log
|
||||
|
||||
# log the received line
|
||||
printf "< %s: %s\n" "$(date "+[%y:%m:%d %T]")" "$line"
|
||||
# new dj!
|
||||
if [[ $dj != $(parse_dj) ]]; then
|
||||
dj=$(parse_dj)
|
||||
if [[ ! -z "${dj}" ]]; then
|
||||
for chan in $notify_channels; do
|
||||
msg "#$chan" "$dj is now online! tune in now here: $link"
|
||||
done
|
||||
|
||||
# now playing
|
||||
if [ "$now_playing" != "$(<$npfile)" ]; then
|
||||
now_playing=$(<$npfile)
|
||||
while read -u 10 subscriber; do
|
||||
msg $subscriber "$dj is now online! tune in now here: $link"
|
||||
done 10< subscribers.txt
|
||||
|
||||
# print the new song if a dj is streaming
|
||||
if [ -n "$dj" ]; then
|
||||
msg "#$channel" "\x0303$now_playing"
|
||||
fi
|
||||
while read -u 11 subscriber email_addr; do
|
||||
sed -e "s/<dj>/$dj/g" -e "s/<link>/$link/g" dj-online-email.tmpl \
|
||||
| sendmail $email_addr
|
||||
done 11< email-subscribers.txt
|
||||
|
||||
for json in . ./yourtilde ./team; do
|
||||
TOOT_JSON_PATH=$json toot "dj $dj is now streaming live on https://tilderadio.org! tune in here: $link"
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
# new dj!
|
||||
if [ "$dj" != "$(parse_dj)" ]; then
|
||||
dj=$(parse_dj)
|
||||
if [ -n "$dj" ]; then
|
||||
for chan in $notify_channels; do
|
||||
msg "#$chan" "$dj is now online playing $now_playing! tune in now here: $link"
|
||||
done
|
||||
# now playing
|
||||
if [[ $now_playing != "$(<$npfile)" ]]; then
|
||||
save_info
|
||||
[[ ! -z "${dj}" ]] && echo -e "PRIVMSG #$channel :\x0303$now_playing" >> $input
|
||||
fi
|
||||
|
||||
while IFS= read -r -u 10 subscriber; do
|
||||
msg "$subscriber" "$dj is now online playing $now_playing! tune in now here: $link"
|
||||
done 10< subscribers.txt
|
||||
# do things when you see output
|
||||
case "$res" in
|
||||
# respond to ping requests from the server
|
||||
PING*)
|
||||
echo "$res" | sed "s/I/O/" >> $input
|
||||
;;
|
||||
|
||||
while read -r -u 11 subscriber email_addr; do
|
||||
sed -e "s/<dj>/$dj/g" \
|
||||
-e "s|<link>|$link|g" \
|
||||
-e "s/<now_playing>/$now_playing/g" \
|
||||
-e "s/<email_addr>/$subscriber <$email_addr>/g" dj-online-email.tmpl \
|
||||
| sendmail "$email_addr"
|
||||
done 11< email-subscribers.txt
|
||||
# make sure we're joined
|
||||
*376*|*404*)
|
||||
for chan in $notify_channels; do
|
||||
echo "JOIN #$chan" >> $input
|
||||
done
|
||||
;;
|
||||
|
||||
for json in ./*-toot.json; do
|
||||
toot --creds "$json" "dj $dj is now streaming live on #tilderadio! tune in here: $link"
|
||||
done
|
||||
fi
|
||||
fi
|
||||
# run when a message is seen
|
||||
*PRIVMSG*)
|
||||
echo "$res"
|
||||
who=$(echo "$res" | perl -pe "s/:(.*)\!.*@.*/\1/")
|
||||
from=$(echo "$res" | perl -pe "s/.*PRIVMSG (.*[#]?([a-zA-Z]|\-)*) :.*/\1/")
|
||||
echo $who $from
|
||||
|
||||
# do things when you see output
|
||||
case "$line" in
|
||||
# respond to ping requests from the server
|
||||
PING*)
|
||||
send "$(printf %s "$line" | sed "s/I/O/")"
|
||||
;;
|
||||
# "#" would mean it's a channel
|
||||
if [ "$(echo "$from" | grep '^#')" ]; then
|
||||
test "$(echo "$res" | grep ":$nick:")" || continue
|
||||
will=$(echo "$res" | perl -pe "s/.*:$nick:(.*)/\1/")
|
||||
else
|
||||
will=$(echo "$res" | perl -pe "s/.*$nick :(.*)/\1/")
|
||||
from=$who
|
||||
fi
|
||||
will=$(echo "$will" | perl -pe "s/^ //")
|
||||
com=$(echo "$will" | cut -d " " -f1)
|
||||
|
||||
# log in to services
|
||||
*001*) # RPL_WELCOME
|
||||
if [ -z "$reg" ] && [ -f account.ini ]; then
|
||||
. account.ini
|
||||
send "SQUERY NickServ identify $account $password"
|
||||
fi
|
||||
;;
|
||||
case "$com" in
|
||||
subscribe)
|
||||
if grep -q $who subscribers.txt; then
|
||||
msg $from "you're already subscribed! :)"
|
||||
else
|
||||
echo $who >> subscribers.txt
|
||||
msg $from "i'll send you a direct message when a dj starts streaming!"
|
||||
fi
|
||||
;;
|
||||
|
||||
# grab account name if authentication successful
|
||||
*900*) # RPL_LOGGEDIN
|
||||
reg=$(printf %s "$line" | sed -E "s/.* (.*) :.*/\1/")
|
||||
printf "successfully logged in as '%s'\n" "$reg"
|
||||
;;
|
||||
unsubscribe)
|
||||
sed -i "/$who/d" subscribers.txt
|
||||
msg $from "i'll stop sending you updates."
|
||||
;;
|
||||
|
||||
# make sure we're joined
|
||||
*376*) # RPL_ENDOFMOTD
|
||||
for chan in $notify_channels; do
|
||||
send "JOIN #$chan"
|
||||
done
|
||||
;;
|
||||
email-subscribe)
|
||||
if grep -q $who email-subscribers.txt; then
|
||||
msg $from "you're already subscribed! :)"
|
||||
else
|
||||
email_addr=$(echo "$will" | cut -d " " -f2)
|
||||
echo "$who $email_addr" >> email-subscribers.txt
|
||||
msg $from "i'll send you an email when a dj starts streaming!"
|
||||
fi
|
||||
;;
|
||||
|
||||
# run when a message is seen
|
||||
*PRIVMSG*)
|
||||
who=$(printf "%s" "$line" | sed -E "s/:(.*)\!.*@.*/\1/")
|
||||
from=$(printf "%s" "$line" | sed -E "s/.*PRIVMSG (.*[#]?([a-zA-Z]|\-)*) :.*/\1/")
|
||||
email-unsubscribe)
|
||||
sed -i "/$who/d" email-subscribers.txt
|
||||
msg $from "i'll stop sending you email updates."
|
||||
;;
|
||||
|
||||
# "#" would mean it's a channel
|
||||
if printf %s "$from" | grep -q "^#"; then
|
||||
# channel messages need to contain our nick
|
||||
printf %s "$line" | grep -q ":$nick:" || continue
|
||||
trailing=$(printf %s "$line" | sed -E "s/.*:$nick:(.*)/\1/")
|
||||
else
|
||||
trailing=$(printf %s "$line" | sed -E "s/.*$nick :(.*)/\1/")
|
||||
from="$who"
|
||||
fi
|
||||
time)
|
||||
msg $from "$(TZ=UTC date)"
|
||||
;;
|
||||
|
||||
# trim leading and split args on space
|
||||
IFS=" " read -r -a args <<< "$(printf %s "$trailing" | sed -E "s/^ //")"
|
||||
np)
|
||||
msg $from "$now_playing"
|
||||
;;
|
||||
|
||||
case "${args[0]}" in
|
||||
subscribe)
|
||||
if grep -q "$who" subscribers.txt; then
|
||||
msg "$from" "you're already subscribed! :)"
|
||||
else
|
||||
printf "%s\n" "$who" >> subscribers.txt
|
||||
msg "$from" "i'll send you a direct message when a dj starts streaming!"
|
||||
fi
|
||||
;;
|
||||
dj)
|
||||
msg $from "${dj:-"no one"} is on the air now"
|
||||
;;
|
||||
|
||||
unsubscribe)
|
||||
sed -i"" "/$who/d" subscribers.txt
|
||||
msg "$from" "i'll stop sending you updates."
|
||||
;;
|
||||
link)
|
||||
msg $from "$link"
|
||||
;;
|
||||
|
||||
email-subscribe)
|
||||
if grep -q "$who" email-subscribers.txt; then
|
||||
msg "$from" "you're already subscribed! :)"
|
||||
else
|
||||
if (( "${#args[1]}" > 0 )); then
|
||||
printf "%s %s\n" "$who" "${args[1]}" >> email-subscribers.txt
|
||||
msg "$from" "i'll send you an email when a dj starts streaming!"
|
||||
else
|
||||
msg "$from" "please provide an address to email when a dj starts streaming"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
source)
|
||||
msg $from "$source"
|
||||
;;
|
||||
|
||||
email-unsubscribe)
|
||||
sed -i"" "/$who/d" email-subscribers.txt
|
||||
msg "$from" "i'll stop sending you email updates."
|
||||
;;
|
||||
schedule)
|
||||
msg $from "$schedule"
|
||||
;;
|
||||
|
||||
time|date|now)
|
||||
msg "$from" "UTC time is currently $(LANG=C TZ=UTC date)"
|
||||
;;
|
||||
help)
|
||||
msg $from "hey hi, my commands are subscribe, unsubscribe, np, dj, link, slogan, and schedule. if you prefer email notifications, you can use radiobot: email-subscribe you@example.com"
|
||||
;;
|
||||
|
||||
np)
|
||||
msg "$from" "$now_playing"
|
||||
;;
|
||||
radio|slogan)
|
||||
msg $from "$(curl -s https://ben.ttm.sh/slogans.txt | shuf -n1)"
|
||||
;;
|
||||
|
||||
dj)
|
||||
msg "$from" "${dj:-"no one"} is on the air now"
|
||||
;;
|
||||
paymybills)
|
||||
msg $from "whaddya mean?! i'm broker than you!"
|
||||
;;
|
||||
|
||||
link)
|
||||
msg "$from" "$link"
|
||||
;;
|
||||
*)
|
||||
echo "no command: $res"
|
||||
;;
|
||||
|
||||
source)
|
||||
msg "$from" "$source"
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
|
||||
schedule|upnext|un)
|
||||
# get next scheduled streaming time
|
||||
json=$(curl -s "https://radio.tildeverse.org/api/station/1/schedule?rows=1" | jq ".[]")
|
||||
nextdj=$(jq -r .name <<< "$json")
|
||||
nexttime=$(LANG=C TZ=UTC date -d "$(jq -r .start <<< "$json")")
|
||||
timediff=$(TZ=UTC awk -v d="$nexttime" 'BEGIN { then=mktime(d); now=systime(); print strftime("%Hh %Mm", then - now); }')
|
||||
msg "$from" "$schedule - $nextdj will be on at $nexttime (in $timediff)"
|
||||
;;
|
||||
|
||||
help)
|
||||
msg "$from" "hey hi, my commands are np, dj, link, slogan, schedule, time, [un]subscribe, email-[un]subscribe. please provide an address when subscribing via email."
|
||||
;;
|
||||
|
||||
radio|slogan)
|
||||
msg "$from" "$(shuf -n1 slogans.txt)"
|
||||
;;
|
||||
|
||||
paymybills)
|
||||
msg "$from" "whaddya mean?! i'm broker than you!"
|
||||
;;
|
||||
|
||||
*) ;;
|
||||
|
||||
esac
|
||||
;;
|
||||
|
||||
# else
|
||||
*) ;;
|
||||
esac
|
||||
# else
|
||||
*)
|
||||
echo "$res"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
From: radiobot <radiobot@tilde.chat>
|
||||
To: <email_addr>
|
||||
Subject: tilderadio: dj <dj> is now streaming!
|
||||
|
||||
<dj> just came on the air on tilderadio.org!
|
||||
|
||||
now playing: <now_playing>
|
||||
|
||||
have a listen here: <link>
|
||||
|
||||
~radiobot
|
||||
|
|
31
slogans.txt
31
slogans.txt
|
@ -1,31 +0,0 @@
|
|||
The soundtrack in your cat's head
|
||||
Your cat says she likes us better
|
||||
radio for the people
|
||||
radio by bots, for bots
|
||||
On the Internet, no one knows you're a weakly superhuman AGI with a really good speech synth module.
|
||||
We found your cat
|
||||
cats.
|
||||
a hacker's excuse for radio
|
||||
0::1 is where the heart is
|
||||
now with the longest slogan in human history
|
||||
reticulating splines
|
||||
Hold the shift
|
||||
a very serious place for very serious happenings
|
||||
sorry, this extension is invalid
|
||||
This is the radio you were looking for
|
||||
Not The Illuminati, We Promise
|
||||
the radio is coming from INSIDE the house
|
||||
Dont Sue Us!
|
||||
it's radio minus radio
|
||||
where your passwords are stored in plain text
|
||||
Like radio. Only better.
|
||||
tomasino 0wnz y0u!
|
||||
that escalated quickly
|
||||
music for your brain meats
|
||||
003 Days Since Last Workplace Accident
|
||||
*heavy breathing*
|
||||
Dizzy? We stop the world from spinning
|
||||
Where Disney Princesses Go Slumming
|
||||
any slogan but "eat the poop or die"
|
||||
that's not what she said!
|
||||
not product placement, we promise! (tildestore.com)
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
$json = json_decode(file_get_contents('php://input'));
|
||||
|
||||
$msg = "";
|
||||
|
||||
if ($json->live->is_live)
|
||||
$msg .= "({$json->live->streamer_name}) ";
|
||||
$msg .= "now playing: ";
|
||||
$msg .= $json->now_playing->song->text . " ";
|
||||
$msg .= "~~ {$json->listeners->current} listeners";
|
||||
|
||||
file_put_contents("now_playing.txt", $msg);
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
<?php
|
||||
$debug = false;
|
||||
$input = file_get_contents("php://input");
|
||||
$tilderadio = json_decode($input, true);
|
||||
|
||||
if ($debug) {
|
||||
$timestamp = date("c");
|
||||
file_put_contents("$timestamp.json", $input);
|
||||
print_r($tilderadio);
|
||||
}
|
||||
|
||||
$msg = "";
|
||||
|
||||
if ($tilderadio["live"]["is_live"])
|
||||
$msg .= "({$tilderadio["live"]["streamer_name"]}) ";
|
||||
$msg .= "now playing: ";
|
||||
$msg .= $tilderadio["now_playing"]["song"]["text"] . " ";
|
||||
$msg .= "~~ {$tilderadio["listeners"]["current"]} listeners";
|
||||
|
||||
echo $msg;
|
||||
file_put_contents(__DIR__."/../now_playing.txt", $msg);
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
<?php
|
||||
$slogans = file("../slogans.txt");
|
||||
echo $slogans[array_rand($slogans)];
|
||||
|
Loading…
Reference in New Issue