如何使用 Bash 编写的 IRC 机器人从 IRC 频道读取消息?
我找不到太多关于用 Bash 编写的 IRC 机器人的文档,所以这是我的问题。 我有一个简单的机器人,可以加入频道并将消息写入频道。
但是,如何读取频道中的消息,即用户的消息?
最终,我希望我的机器人能够识别一个关键字,该关键字将使机器人启动并返回一些内容。作为我的机器人的基础,我使用了 http://www.blog.tdobson.net 中的脚本/节点/174。向我指出一些关于如何在 Bash 中编写 IRC 机器人的有用文档也很棒。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让 IRC 连接协议问题由 ii 来解决。
它是一个 IRC 客户端,生成文本文件中的所有输出(作为日志),但如果您向这些文件写入(追加行),您实际上是在向 IRC 发送命令。所以这很简单。
我建议使用
awk
进行文本解析。它是一种旨在实现这一目标的语言,如果您已经编写过复杂的 bash 脚本,那么它很容易学习。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.您向我们指出的基本流是这个
tail -f file |远程登录 foo |虽然真实;废话;
此方法将写入文件的数据获取到 telnet 命令中,但不会从 telnet 命令中获取数据并将其通过管道传输到脚本中。
修改循环以支持 tail -f file |远程登录 foo |当读 f 时;执行 echo "我收到消息 $f"; done 为您提供从 telnet 会话发送给您的数据,然后您可以解析这些数据。此策略的问题在于您无法自发地执行任何操作,只能响应来自 telnet 会话的流量。
你可以通过请求超时来解决这个问题:
在超时下你会得到一个空的$f,如果你收到一条消息,你会得到一个完整的$f。解析 irc 协议的 PRIVMSG 输出作为读者的练习。
尾巴 |远程登录|当读 f 时;做 ; done 循环不是完成此任务的传统方法。传统上,您可以将 telnet 设置为协进程 (coproc)。但无论哪种方式都可能有效。
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:
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.