Sircbot

来自 阿尔派 Linux

Sircbot 是一个基于 Lua 脚本的极简 IRC 机器人。

安装 sircbot

# apk add sircbot lua-sircbot

配置 sircbot 守护进程

编辑位于 /etc/conf.d/sircbot 的配置文件,并更改名称、服务器和频道


/etc/conf.d/sircbot 的内容

... sircbot_opts="-n NICK -s SERVER" sircbot_channels="#channel" ...

直接运行 sirbot 也是一样的。

$ sircbot -s SERVER -n NICK '#channel'

Sircbot 将充当 “守护进程” 加入服务器和频道,从而做出以下响应。

发送消息

有两种发送消息的方式,频道需要首先在 sirbot 守护进程中指定。

sircbot-send

$ echo "$MESSAGE" | sircbot-send '#channel'

sircbot lua 库

lua_script 的内容

local sircbot = require('sircbot') local client = sircbot.connect('#channel') client:send("hello")

创建 sircbot 脚本文件夹和文件

Sircbot 将为登录的频道使用一个文件夹。文件夹路径是:/etc/sircbot.d/#channel

对于发送到频道的每条消息,sircbot 将使用发送者、消息和频道名称作为参数运行 /etc/sircbot.d/#channel/ 中的所有脚本。[1]

示例

让我们看一个例子

我们的机器人将被命名为 kumquat,我们将加入 irc.ddd.ddd 服务器上的 #test 频道。


/etc/conf.d/sircbot 的内容

sircbot_opts="-n kumquat -s irc.ddd.ddd" sircbot_channels="#test"

创建文件夹 bot。

# mkdir /etc/sircbot.d/#test -p

为我们的机器人创建 Lua 脚本文件。


/etc/sircbot.d/#test/commands 的内容

#!/usr/bin/lua -- Scripts for kumquat in Lua args={...} sender=args[1] -- is the nickname of running the command on IRC message=args[2] -- is the message or command typed channel=args[3] -- is the channel name os.execute("sleep " .. tonumber(1)) -- This command will show: What can i do for you "nickname"? local kumquatresult = string.find(message, "kumquat") if kumquatresult ~= nil then command="echo 'What can i do for you '"..sender.."'?' | sircbot-send '"..channel.."'" io.popen(command) end -- This command will show: Hello there, amigo "nickname" local kumquatresult = string.find(message, "hi") if kumquatresult ~= nil then command="echo 'Hello there, amigo '"..sender.."'' | sircbot-send '"..channel.."'" io.popen(command) end -- This command will show the current UTC date and time local kumquatresult = string.find(message, "date") if kumquatresult ~= nil then command="date -u | sircbot-send '"..channel.."'" io.popen(command) end -- This command will do a search for a alpine linux package local kumquatresult = string.find(message, "apk search:[^%d]") if kumquatresult ~= nil then local strng = (string.sub(message, 12)) command="apk search"..strng.." | sircbot-send '"..channel.."'" io.popen(command) end -- This command will resolve a host name and ip local kumquatresult = string.find(message, "host:[^%d]") if kumquatresult ~= nil then local strng = (string.sub(message, 6)) command="host "..strng.." | sircbot-send '"..channel.."'" io.popen(command) end -- This command will do a 3 times ping to a server or ip address local kumquatresult = string.find(message, "ping:[^%d]") if kumquatresult ~= nil then local strng = (string.sub(message, 6)) command="ping -c 3 "..strng.." | sircbot-send '"..channel.."'" io.popen(command) end

使脚本可执行

# chmod +x /etc/sircbot.d/#test/commands

赋予 bot 生命

启动 sircbot 守护进程并将服务添加到启动项。

# rc-service sircbot start && rc-update add sircbot default

故障排除

如果你的机器人没有按预期响应,一个简单的故障排除方法是使用三个参数手动执行 /etc/sircbot.d/<#channelname>/<scriptname> 中的脚本。

$ /etc/sircbot.d/<#channelname>/<scriptname> username hi '<#channelname>'

如果你的机器人按预期响应,那么你就知道你的脚本工作正常。问题是 sircbot 没有执行该脚本。这里有两个可能的原因

  1. Sircbot 在查找要执行脚本的频道目录时区分大小写。例如,如果你有一个名为 “#Alpine” 的频道,sircbot 将查找 /etc/sircbot.d/#Alpine/*。如果该路径不完全正确,则 sircbot 将跳过该文件夹。在我们的示例中,如果你有 /etc/sircbot.d/#alpine/script,sircbot 将不会执行该文件夹中的任何内容。要修复此问题,只需将文件夹重命名为 /etc/sircbot.d/#Alpine/
  2. Sircbot 不会执行名称中带有 “.” 的脚本。例如,名为 “listen.lua” 的脚本将不会被执行。只需将脚本重命名为 “listen”。