Compare commits

...

33 Commits

Author SHA1 Message Date
Ben Harris dbb70f0727 Update 'README.md'
continuous-integration/drone/push Build is failing Details
2020-07-16 18:27:50 -04:00
Ben Harris c9a86e5317 tidy up systemd service file
continuous-integration/drone/push Build is failing Details
2020-06-28 14:45:03 -04:00
Ben Harris 8bc8d4a694 add shellcheck on deploy
continuous-integration/drone/push Build is failing Details
2020-06-28 14:34:51 -04:00
James Tomasino c78d6eb575 awk mktime expected different date format. replace with date in seconds 2020-06-28 16:45:07 +00:00
Ben Harris ac94486c80 show date diff on schedule command 2020-06-28 11:42:33 -04:00
Ben Harris df6e17bcbd testing out webhook 2020-06-27 19:40:10 -04:00
Ben Harris c91c02278a show time as 24h instead of 12h 2020-06-27 19:35:15 -04:00
Ben Harris fe4b8f137c add aliases for schedule 2020-06-27 19:27:36 -04:00
Ben Harris e2ee8d4c97 fix sending email 2020-06-27 15:21:16 -04:00
Ben Harris 744a206c07 improve formatting of schedule command 2020-06-25 10:58:37 -04:00
Ben Harris 7c7db1b785 show scheduled date in same format 2020-06-22 20:51:57 -04:00
Ben Harris 59a2da03eb add date alias to time command 2020-06-22 20:46:21 -04:00
Ben Harris 135e47fc07 show next scheduled dj 2020-06-22 20:32:42 -04:00
Ben Harris 161220bfeb expose random slogan 2020-06-21 15:00:31 -04:00
Ben Harris 617c85c904 fix restart bug 2020-06-20 23:09:19 -04:00
Ben Harris 2fc07b53ae update toot message 2020-06-19 10:10:49 -04:00
Ben Harris c6859e2513 fix color 2020-06-18 02:03:47 -04:00
Ben Harris 7e492f165f fix sending updates if a dj is on 2020-06-18 00:51:39 -04:00
Ben Harris b04e5ac662 update source url 2020-06-18 00:15:33 -04:00
Ben Harris 639c7eece0 add support for nickserv auth 2020-06-17 15:31:24 -04:00
Ben Harris 543dd48e01 fix shellcheck warning SC2207 2020-06-17 13:57:36 -04:00
Ben Harris 53e64bb1eb tidy up webhook 2020-06-17 13:45:52 -04:00
Ben Harris 4a80f45e1e update .gitignore 2020-06-17 13:39:57 -04:00
Ben Harris b257f25f9d just print to stdout 2020-06-17 13:39:05 -04:00
Ben Harris 7d449deb38 fix PONG and update notify_channels 2020-06-17 11:49:34 -04:00
Ben Harris 7ae1f26cb6 remove unneeded quotes 2020-06-17 11:40:06 -04:00
Ben Harris 118ec4e189 shellcheck: quote variables and create send() fn 2020-06-17 11:37:15 -04:00
Ben Harris 45364a3ad0 update .gitignore 2020-01-16 23:57:56 -05:00
Ben Harris 928da93e8a printf refactor 2020-01-16 23:52:15 -05:00
Ben Harris ffa0c279bf Merge branch 'master' of BrendanTCC/radiobot into master 2019-12-04 22:12:08 -05:00
Brendan Webb abb0646c02 Merge branch 'brendantcc-patch-1' of BrendanTCC/radiobot into master 2019-12-04 18:16:15 -05:00
Brendan Webb 7e7deb5574 Update 'slogans.txt' 2019-12-04 18:15:45 -05:00
Ben Harris 74ef150c21 change yourtilde to club 2019-12-01 20:20:26 -05:00
10 changed files with 215 additions and 147 deletions

10
.drone.yml Normal file
View File

@ -0,0 +1,10 @@
---
kind: pipeline
name: shellcheck
steps:
- name: shellcheck
image: koalaman/shellcheck-alpine:stable
commands:
- shellcheck -x bot.sh

4
.gitignore vendored
View File

@ -1,7 +1,9 @@
toot.json
log.txt
*-toot.json
input.txt
.bot.cfg
subscribers.txt
email-subscribers.txt
now_playing.txt
account.ini

View File

@ -1,4 +1,4 @@
# radiobot
# radiobot (legacy)
super simple bot
@ -8,3 +8,8 @@ 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.
UPDATE: it has been rewritten from scratch as a module for [bitbot](https://bitbot.dev)
the module code can be found [here](https://tildegit.org/ben/bitbot-modules/src/branch/master/tilderadio.py)

3
account.ini.sample Normal file
View File

@ -0,0 +1,3 @@
account=bensbots
password=my super secret password

View File

@ -2,9 +2,8 @@ server="localhost"
port="6667"
npfile="now_playing.txt"
channel="tilderadio"
notify_channels="meta team yourtilde club tildetel envs $channel"
notify_channels="meta team club $channel"
link="https://tilderadio.org/listen"
schedule="https://tilderadio.org/schedule/"
source="https://tildegit.org/ben/radiobot"
source="https://tildegit.org/tilderadio/radiobot"
nick="radiobot"
log="log.txt"

307
bot.sh
View File

@ -1,176 +1,211 @@
#!/bin/bash
#!/usr/bin/env 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=""
function save_info {
now_playing=$(<$npfile)
}
# reset input file
rm input.txt && touch input.txt
function parse_dj {
grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs
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 {
echo "PRIVMSG ${1} :${2}" >> $input
send "PRIVMSG $1 :$2"
}
# save info on startup
save_info
function parse_dj {
grep -Eo '^\([^)]*\)' $npfile | sed 's/[()]//g' | xargs
}
# save current dj info on startup
now_playing=$(<$npfile)
dj=$(parse_dj)
# connect and register
send "NICK $nick"
send "USER $nick 0 * :$nick"
send "MODE $nick +B"
# main loop
tail -f $input | telnet $server $port | while read res
tail -f input.txt | telnet $server $port | while read -r buf
do
# log the session
echo "$(date "+[%y:%m:%d %T]")$res" >> $log
# trim \r's
line=$(printf %b "$buf" | tr -d $'\r')
# now playing
if [[ $now_playing != "$(<$npfile)" ]]; then
save_info
[[ ! -z "${dj}" ]] && echo -e "PRIVMSG #$channel :\x0303$now_playing" >> $input
fi
# 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 playing $now_playing! 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 playing $now_playing! tune in now here: $link"
done 10< subscribers.txt
while read -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>/$email_addr/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
# print the new song if a dj is streaming
if [ -n "$dj" ]; then
msg "#$channel" "\x0303$now_playing"
fi
fi
fi
# do things when you see output
case "$res" in
# respond to ping requests from the server
PING*)
echo "$res" | sed "s/I/O/" >> $input
;;
# 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
# make sure we're joined
*376*|*404*)
for chan in $notify_channels; do
echo "JOIN #$chan" >> $input
done
;;
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
# 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
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
# "#" 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)
for json in ./*-toot.json; do
toot --creds "$json" "dj $dj is now streaming live on #tilderadio! tune in here: $link"
done
fi
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
;;
# 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/")"
;;
unsubscribe)
sed -i "/$who/d" subscribers.txt
msg $from "i'll stop sending you updates."
;;
# log in to services
*001*) # RPL_WELCOME
if [ -z "$reg" ] && [ -f account.ini ]; then
. account.ini
send "SQUERY NickServ identify $account $password"
fi
;;
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
;;
# 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"
;;
email-unsubscribe)
sed -i "/$who/d" email-subscribers.txt
msg $from "i'll stop sending you email updates."
;;
# make sure we're joined
*376*) # RPL_ENDOFMOTD
for chan in $notify_channels; do
send "JOIN #$chan"
done
;;
time)
msg $from "$(TZ=UTC date)"
;;
# 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/")
np)
msg $from "$now_playing"
;;
# "#" 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
dj)
msg $from "${dj:-"no one"} is on the air now"
;;
# trim leading and split args on space
IFS=" " read -r -a args <<< "$(printf %s "$trailing" | sed -E "s/^ //")"
link)
msg $from "$link"
;;
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
;;
source)
msg $from "$source"
;;
unsubscribe)
sed -i"" "/$who/d" subscribers.txt
msg "$from" "i'll stop sending you updates."
;;
schedule)
msg $from "$schedule"
;;
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
;;
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"
;;
email-unsubscribe)
sed -i"" "/$who/d" email-subscribers.txt
msg "$from" "i'll stop sending you email updates."
;;
radio|slogan)
msg $from "$(shuf -n1 slogans.txt)"
;;
time|date|now)
msg "$from" "UTC time is currently $(LANG=C TZ=UTC date)"
;;
paymybills)
msg $from "whaddya mean?! i'm broker than you!"
;;
np)
msg "$from" "$now_playing"
;;
*)
echo "no command: $res"
;;
dj)
msg "$from" "${dj:-"no one"} is on the air now"
;;
esac
;;
link)
msg "$from" "$link"
;;
# else
*)
echo "$res"
;;
esac
source)
msg "$from" "$source"
;;
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")")
nexttimesecs=$(LANG=C TZ=UTC date -d "$nexttime" +%s)
timediff=$(TZ=UTC awk -v d="$nexttimesecs" 'BEGIN { then=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
done

View File

@ -5,7 +5,7 @@ After=radiobot.service
[Service]
Type=simple
WorkingDirectory=/home/ben/workspace/radiobot
ExecStart=/bin/bash /home/ben/workspace/radiobot/bot.sh
ExecStart=/bin/bash bot.sh
Restart=always
RestartSec=5
StartLimitInterval=60s

View File

@ -27,3 +27,5 @@ music for your brain meats
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)

View File

@ -1,14 +1,22 @@
<?php
$debug = false;
$input = file_get_contents("php://input");
$tilderadio = json_decode($input, true);
$json = json_decode(file_get_contents('php://input'));
if ($debug) {
$timestamp = date("c");
file_put_contents("$timestamp.json", $input);
print_r($tilderadio);
}
$msg = "";
if ($json->live->is_live)
$msg .= "({$json->live->streamer_name}) ";
if ($tilderadio["live"]["is_live"])
$msg .= "({$tilderadio["live"]["streamer_name"]}) ";
$msg .= "now playing: ";
$msg .= $json->now_playing->song->text . " ";
$msg .= "~~ {$json->listeners->current} listeners";
$msg .= $tilderadio["now_playing"]["song"]["text"] . " ";
$msg .= "~~ {$tilderadio["listeners"]["current"]} listeners";
echo $msg;
file_put_contents(__DIR__."/../now_playing.txt", $msg);

4
webhook/slogan.php Normal file
View File

@ -0,0 +1,4 @@
<?php
$slogans = file("../slogans.txt");
echo $slogans[array_rand($slogans)];