Reputation: 9
I could not find much documentation on IRC bots written in Bash so here is my question. I have a simple bot that can join a channel and write messages into the channel.
However, how do I read messages from the channel, i.e. messages from users?
Ultimately, I'd like my bot to recognize a keyword that will spring the bot in action and return something. As the base of my bot, I used the script from http://www.blog.tdobson.net/node/174. Pointing me to some useful documentation on how to write IRC bots in Bash would be great as well.
Upvotes: 0
Views: 2637
Reputation: 21
Leave the IRC conectivity protocol problem to be solved by ii.
It is an IRC client that generates all the output in text files (as logs) but if you write (append lines) to these files, you are actually sending commands to IRC. So it is very simple.
I recommend using awk
for text parsing. It is a language aimed for that and easy to learn if you already do complicated bash scripts.
Upvotes: 2
Reputation: 31451
The basic stream you pointed us to is this tail -f file | telnet foo | while true; do blah; done
This method gets the data written to file into the telnet command, but nothing takes the data from the telnet command and pipes it into the script.
Modifying the loop to support tail -f file | telnet foo | while read f; do echo "I got message $f"; done
provides you the data being sent to you from the telnet session which you can then parse. The problem with this strategy is that you cannot do anything spontaneously, only in response to traffic from the telnet session.
You can take care of that problem by requesting a timeout:
tail -f pingbot.input | telnet irc.freenode.net 6667 | while read -t 2 f || true; do
echo I got message $f;
done
You will get an empty $f under timeout, a full $f if you got a message. Parsing the PRIVMSG output from the irc protocol is left as an exercise for the reader.
The tail | telnet | while read f; do ; done
loop is not the traditional way of accomplishing this task. Traditionally you would set up telnet as a coprocess (coproc) instead. But either way will probably work.
Upvotes: 1