Sircbot: Difference between revisions

From Alpine Linux
(lua IRC bot)
 
Line 38: Line 38:
Create the lua scrip file for our bot.  
Create the lua scrip file for our bot.  


{{Cmd| vi /etc/sircbot.d/#test/commands.lua}}
{{Cmd| vi /etc/sircbot.d/#test/commands}}


  #!/usr/bin/lua
  #!/usr/bin/lua
Line 55: Line 55:
  local kumquatresult = string.find(message, "kumquat")  
  local kumquatresult = string.find(message, "kumquat")  
   if kumquatresult ~= nil then
   if kumquatresult ~= nil then
   command="echo 'What can i do for you '"..sender.."'?' | sircbot-send '#hed'"
   command="echo 'What can i do for you '"..sender.."'?' | sircbot-send '"..channel.."'"
   io.popen(command)
   io.popen(command)
  end
  end
Line 62: Line 62:
  local kumquatresult = string.find(message, "hi")
  local kumquatresult = string.find(message, "hi")
   if kumquatresult ~= nil then
   if kumquatresult ~= nil then
   command="echo 'Hello there, amigo '"..sender.."'' | sircbot-send '#hed'"
   command="echo 'Hello there, amigo '"..sender.."'' | sircbot-send '"..channel.."'"
   io.popen(command)
   io.popen(command)
  end
  end
Line 69: Line 69:
  local kumquatresult = string.find(message, "date")
  local kumquatresult = string.find(message, "date")
   if kumquatresult ~= nil then
   if kumquatresult ~= nil then
   command="date -u | sircbot-send '#hed'"
   command="date -u | sircbot-send '"..channel.."'"
   io.popen(command)
   io.popen(command)
  end
  end
Line 77: Line 77:
  if kumquatresult ~= nil then
  if kumquatresult ~= nil then
   local strng = (string.sub(message, 12))
   local strng = (string.sub(message, 12))
   command="apk search"..strng.." | sircbot-send '#hed'"     
   command="apk search"..strng.." | sircbot-send '"..channel.."'"     
   io.popen(command)
   io.popen(command)
  end
  end
Line 85: Line 85:
  if kumquatresult ~= nil then
  if kumquatresult ~= nil then
   local strng = (string.sub(message, 6))
   local strng = (string.sub(message, 6))
   command="host "..strng.." | sircbot-send '#hed'"     
   command="host "..strng.." | sircbot-send '"..channel.."'"     
   io.popen(command)
   io.popen(command)
  end
  end
Line 93: Line 93:
  if kumquatresult ~= nil then
  if kumquatresult ~= nil then
   local strng = (string.sub(message, 6))
   local strng = (string.sub(message, 6))
   command="ping -c 3 "..strng.." | sircbot-send '#hed'"     
   command="ping -c 3 "..strng.." | sircbot-send '"..channel.."'"     
   io.popen(command)
   io.popen(command)
  end
  end

Revision as of 09:55, 20 April 2012

Sircbot is a simple irc bot based in lua script.

Install sircbot

apk add sircbot lua-sircbot

Configue sircbot

vi /etc/conf.d/sircbot

Change name, server and channel

sircbot_opts="-n name -s server"
sircbot_channels="#channel"

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. The folder path is: /etc/sircbot.d/#channel

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.

vi /etc/conf.d/sircbot

Change name, server and channel

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

Create folder bot.

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

Create the lua scrip file for our bot.

vi /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

Giving life to the bot

Starting bot service and adding to boot.

/etc/init.d/sircbot start && rc-update add sircbot default