Formatting code for SimpleRSS2XMPPConfBot


show source only

======Simple RSS 2 XMPP Conference Bot - howto======

Note: this is not a complete script, it's just a set of instructions on how to setup one by yourself.

====What do you need====
~- a GNU/Linux machine - duh ;-)
~- rsstail - to read the feed, works great for identi.ca
~- sed - to modify the output of rsstail, optional
~- sendxmpp - to send the message
~- a dedicated xmpp account on any server
~- tail - for sending multiple feeds to 1 chatroom
~- grep - for filtering

##Sed## is usually installed by default, ##rsstail## and ##sendxmpp## are usually found in the distros repository. Obviously you'll need a text editor of some sort. The xmpp account will be used by the bot, I recommend to create a new account for this, do not use your own.

====Get to work====

Open your text editor and paste the first line:

%%(bash)
#!/bin/bash
%%

This will allow to execute this script ;-).
OK, ok, here's the meat:

%%(bash)
rsstail -n 0 -i 120 -z -P -u <feed> | sed -ue 's/Title: //g' | sendxmpp -u <bot-account-name> -j <bot-account-server> -p <bot-account-password> -r <botname> --message-type=chat -t -i -c "<chatroom>" 2>&1 &
%%

I recommend you check the relevant manpages on what these all parameters mean. This single line will post the titles from a single rss feed to a single xmpp chatroom. It will run in the background, to turn it off you need to kill the rsstail process, the rest will terminate automatically.

Now here comes the tricky part... How to send multiple rss feeds to a single chatroom. You could just copy the line for each feed (remember to keep the <botname> unique), but this would keep multiple bots in the chatroom all the time and it wouldn't look too nice. Here is what I've come up with:

%%(bash)
rm /tmp/rss2xmpp-bot.pids
touch /tmp/rss2xmpp-bot.pids
rm /tmp/mychatroom-stream
touch /tmp/mychatroom-stream
(rsstail -n 0 -i 120 -z -P -u <feed1> & echo $! >&3 ) 3>>/tmp/rss2xmpp-bot.pids | sed -ue 's/Title: //g' >> /tmp/mychatroom-stream 2>&1 &
(rsstail -n 0 -i 120 -z -P -u <feed2> & echo $! >&3 ) 3>>/tmp/rss2xmpp-bot.pids | sed -ue 's/Title: //g' >> /tmp/mychatroom-stream 2>&1 &
(tail -f /tmp/mychatroom-stream & echo $! >&3 ) 3>>/tmp/rss2xmpp-bot.pids | sendxmpp -u <bot-account-name> -j <bot-account-server> -p <bot-account-password> -r <botname> --message-type=chat -t -i -c "<chatroom>" 2>&1 &
%%

This appends the rss items to a temp file and then tails this file and sends it to the chatroom. Also this makes it a little easier to keep track of the spawned processes - the pids are put into another temp file. You can kill them all easily using a script like this one:

%%(bash)
#!/bin/bash

while read line
do
kill $line
done </tmp/rss2xmpp-bot.pids
%%

This is it! ;-) It works rather stably, although some lag is known to exist (especially visible if you use a rss feed from some microblogging service like identi.ca).

One more note: if you are using the variant with the temp file for multiple feeds, it's a good idea to kill the bot and start it over once in a while to purge the temporary file.

====Filtering out some lines====

I'm using this script to send my identi.ca stream to a chatroom and some people said that @replies are out of context and unnecessary, so I figured out a small addition to filter them out:

%%(bash)
grep --line-buffered -v -P -e '^\S+:\s@\S+.+'
%%

should be added as another pipe after sed, so the "bot-string" becomes:

%%(bash)
rsstail -n 0 -i 120 -z -P -u <feed> | sed -ue 's/Title: //g' | grep --line-buffered -v -P -e '^\S+:\s@\S+.+' | sendxmpp -u <bot-account-name> -j <bot-account-server> -p <bot-account-password> -r <botname> --message-type=chat -t -i -c "<chatroom>" 2>&1 &
%%

====Support====

If you liked this howto please consider flattring: (button on top of the page)

----
CategoryProjekty
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki