Sircbot: Difference between revisions

From Alpine Linux
m (category added)
(Add sending message with lua library)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Sircbot is a simple irc bot based in lua script.
Sircbot is a Minimalistic IRC bot based in Lua script.


== Install sircbot ==
== Install sircbot ==


{{Cmd|apk add sircbot lua-sircbot}}
{{Cmd|# apk add {{Pkg|sircbot}} {{Pkg|lua-sircbot}}}}


== Configue sircbot ==
== Configure sircbot daemon ==


{{Cmd| vi  /etc/conf.d/sircbot}}
Edit the configuration file located at {{Path|/etc/conf.d/sircbot}} and change the name, server, and channel:


Change name, server and channel


sircbot_opts="-n name -s server"
{{Cat|/etc/conf.d/sircbot|...
sircbot_channels="#channel"
sircbot_opts{{=}}"-n NICK -s SERVER"
sircbot_channels{{=}}"#channel"
...}}
 
It is the same to run sirbot directly.
 
{{Cmd|$ sircbot -s SERVER -n NICK '#channel'}}
 
Sircbot would act as "daemon" joining server and channels thus do the following response.
 
== Sending messages ==
 
There are two ways to send messages, channel need be specified first in sirbot daemon.
 
=== sircbot-send ===
 
{{Cmd|<nowiki>$ echo "$MESSAGE" | sircbot-send '#channel'</nowiki>}}
 
=== sircbot lua library ===
 
{{Cat|lua_script|<nowiki>local sircbot = require('sircbot')
local client = sircbot.connect('#channel')
 
client:send("hello")</nowiki>}}


== Create sircbot scripts folders and files ==
== Create sircbot scripts folders and files ==


Sircbot will use an folder for the channel where is logged in. Lets say we will use it at the "test" channel.
Sircbot will use an folder for the channel where is logged in. The folder path is: {{Path|/etc/sircbot.d/#channel}}
The folder path is: /etc/sircbot.d/#channel
 
For every message that goes to the channel, sircbot will run all scripts in /etc/sircbot.d/#channel/ with sender, message and channel name as arguments.[https://git-old.alpinelinux.org/hosted/sircbot/about/]


== Example ==
== Example ==
Line 23: Line 46:
Lets see an example:
Lets see an example:


Our bot will be named kumquat and we will join at the #test channel in the irc.ddd.ddd server.
Our bot will be named ''kumquat'' and we will join at the #test channel in the irc.ddd.ddd server.


{{Cmd| vi  /etc/conf.d/sircbot}}


Change name, server and channel
{{Cat|/etc/conf.d/sircbot|sircbot_opts{{=}}"-n kumquat -s irc.ddd.ddd"
sircbot_channels{{=}}"#test"}}


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


Create folder bot.  
{{Cmd|# mkdir /etc/sircbot.d/#test -p}}
 
Create the Lua script file for our bot.  
 
 
{{Cat|/etc/sircbot.d/#test/commands|<nowiki>#!/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))


{{Cmd| mkdir /etc/sircbot.d/#test -p}}
-- 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


Create the lua scrip file for our bot.  
-- 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


{{Cmd| vi /etc/sircbot.d/#test/commands}}
-- 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


#!/usr/bin/lua
-- 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
   
   
-- Scripts for kumquat in Lua
-- This command will resolve a host name and ip
local kumquatresult = string.find(message, "host:[^%d]")
args={...}
if kumquatresult ~= nil then
  local strng = (string.sub(message, 6))
sender=args[1]  -- is the nickname of running the command on IRC
  command="host "..strng.." | sircbot-send '"..channel.."'"     
message=args[2] -- is the message or command typed
  io.popen(command)
channel=args[3] -- is the channel name
end
 
os.execute("sleep " .. tonumber(1))
-- This command will do a 3 times ping to a server or ip address
local kumquatresult = string.find(message, "ping:[^%d]")
-- This command will show: What can i do for you "nickname"?
if kumquatresult ~= nil then
local kumquatresult = string.find(message, "kumquat")
  local strng = (string.sub(message, 6))
  if kumquatresult ~= nil then
  command="ping -c 3 "..strng.." | sircbot-send '"..channel.."'"     
  command="echo 'What can i do for you '"..sender.."'?' | sircbot-send '"..channel.."'"
  io.popen(command)
  io.popen(command)
end</nowiki>}}
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


Make the script to be executable
Make the script to be executable


{{Cmd| chmod +x /etc/sircbot.d/#test/commands}}
{{Cmd|# chmod +x /etc/sircbot.d/#test/commands}}


== Giving life to the bot ==
== Giving life to the bot ==


Starting bot service and adding to boot.
Starting sircbot daemon and adding service to boot.


{{Cmd|/etc/init.d/sircbot start && rc-update add sircbot default}}
{{Cmd|# rc-service sircbot start && rc-update add sircbot default}}


== Troubleshooting ==
== Troubleshooting ==
Line 111: Line 130:
If your bot is not responding as expected, an easy way to troubleshoot is to manually execute the script in /etc/sircbot.d/<#channelname>/<scriptname> with the three agruments.
If your bot is not responding as expected, an easy way to troubleshoot is to manually execute the script in /etc/sircbot.d/<#channelname>/<scriptname> with the three agruments.


{{Cmd|/etc/sircbot.d/<#channelname>/<scriptname> username hi '<#channelname>'}}
{{Cmd|$ /etc/sircbot.d/<#channelname>/<scriptname> username hi '<#channelname>'}}


If your bot responds as expected, then you know that your script works.  The problem is that sircbot is not executing the script.  Here are two likely causes:
If your bot responds as expected, then you know that your script works.  The problem is that sircbot is not executing the script.  Here are two likely causes:


# Sircbot is case-sensetive when looking for the channel directory for a script to execute.  For example, if you have a channel named "#Alpine", sircbot will look for /etc/sircbot.d/#Alpine/*.  If that pathway isn't exactly right, then sircbot will skip that folder.  In our example, if you have /etc/sircbot.d/#alpine/script, sircbot will not execute anything in that folder.  To fix, simply rename the folder to /etc/sircbot.d/#Alpine/
# Sircbot is case-sensitive when looking for the channel directory for a script to execute.  For example, if you have a channel named "#Alpine", sircbot will look for /etc/sircbot.d/#Alpine/*.  If that pathway isn't exactly right, then sircbot will skip that folder.  In our example, if you have /etc/sircbot.d/#alpine/script, sircbot will not execute anything in that folder.  To fix, simply rename the folder to /etc/sircbot.d/#Alpine/
# Sircbot doesn't execute scripts that have a '.' in the name.  For example a script named 'listen.lua' will not be executed.  Simply rename the script to 'listen'.
# Sircbot doesn't execute scripts that have a '.' in the name.  For example a script named 'listen.lua' will not be executed.  Simply rename the script to 'listen'.


[[Category:Networking]]
[[Category:Networking]]

Latest revision as of 02:29, 8 March 2024

Sircbot is a Minimalistic IRC bot based in Lua script.

Install sircbot

# apk add sircbot lua-sircbot

Configure sircbot daemon

Edit the configuration file located at /etc/conf.d/sircbot and change the name, server, and channel:


Contents of /etc/conf.d/sircbot

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

It is the same to run sirbot directly.

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

Sircbot would act as "daemon" joining server and channels thus do the following response.

Sending messages

There are two ways to send messages, channel need be specified first in sirbot daemon.

sircbot-send

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

sircbot lua library

Contents of lua_script

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

Create sircbot scripts folders and files

Sircbot will use an folder for the channel where is logged in. The folder path is: /etc/sircbot.d/#channel

For every message that goes to the channel, sircbot will run all scripts in /etc/sircbot.d/#channel/ with sender, message and channel name as arguments.[1]

Example

Lets see an example:

Our bot will be named kumquat and we will join at the #test channel in the irc.ddd.ddd server.


Contents of /etc/conf.d/sircbot

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

Create folder bot.

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

Create the Lua script file for our bot.


Contents of /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

Make the script to be executable

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

Giving life to the bot

Starting sircbot daemon and adding service to boot.

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

Troubleshooting

If your bot is not responding as expected, an easy way to troubleshoot is to manually execute the script in /etc/sircbot.d/<#channelname>/<scriptname> with the three agruments.

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

If your bot responds as expected, then you know that your script works. The problem is that sircbot is not executing the script. Here are two likely causes:

  1. Sircbot is case-sensitive when looking for the channel directory for a script to execute. For example, if you have a channel named "#Alpine", sircbot will look for /etc/sircbot.d/#Alpine/*. If that pathway isn't exactly right, then sircbot will skip that folder. In our example, if you have /etc/sircbot.d/#alpine/script, sircbot will not execute anything in that folder. To fix, simply rename the folder to /etc/sircbot.d/#Alpine/
  2. Sircbot doesn't execute scripts that have a '.' in the name. For example a script named 'listen.lua' will not be executed. Simply rename the script to 'listen'.