Skip to content

Instantly share code, notes, and snippets.

@emberian
Forked from anonymous/queuebot.lua
Last active December 14, 2015 14:59
Show Gist options
  • Save emberian/5105020 to your computer and use it in GitHub Desktop.
Save emberian/5105020 to your computer and use it in GitHub Desktop.
require "irc"
local lanes = require "lanes".configure()
local linda = lanes.linda()
local re = require "re"
local sleep = require "socket".sleep
local id = 0
local queued_requests = {}
local send_request = lanes.gen("*", function(command, account, item, id)
local http = require "socket.http"
local ltn12 = require "ltn12"
local req = ('{"command": "%s", "account": "%s", "item": "%s"}'):format(command, account, item)
local _, status = http.request{
url = "http://localhost:8080/api",
method = "POST",
headers = {
["Content-Length"] = #req,
["Content-Type"] = "application/json"
},
source = ltn12.source.string(req)
}
if status ~= 200 then
linda:set(id, false)
else
linda:set(id, true)
end
end)
local channels = { "#ai-dev", "#welcometomars" } -- todo: separate config, multinetwork
function target(message)
_, nick, command, item = re.find(message, "{[^:]*} ': ' {[^ ]*} ' ' {.*}")
if nick == nil or command == nil or item == nil then
return nil
end
return nick, command, item
end
commands = {
enqueue = true,
dequeue = true
}
local qb = irc.new { nick = "queuebot" }
qb:hook("OnChat", function(user, channel, message)
nick, command, item = target(message)
if nick == nil then
return
end
if commands[command] then
local id_ = tostring(id)
id = id + 1
send_request(command, nick, item, id_)
queued_requests[id_] = channel
qb:sendChat(channel, ("done [%s]"):format(id_))
end
end)
qb:connect("irc.coldfront.net")
for _, channel in ipairs(channels) do
qb:join(channel)
end
while true do
qb:think()
for k,v in pairs(queued_requests) do
if linda:get(k) == false then
queued_requests[k] = nil
qb:sendChat(v, ("%s failed!"):format(k))
end
end
sleep(0.3)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment