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


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:

#!/bin/bash


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

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:

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:

#!/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:

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


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

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

There are no comments on this page. [Add comment]

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