fix
parent
e3c4aa16b2
commit
2b4267cca2
|
@ -5,11 +5,11 @@
|
|||
- Add : **secure.http_mods = lp_api**
|
||||
|
||||
### Add parameters if needed in to minetest.confg
|
||||
* **lp_api.url** - LongPoll server url (Default: **http://mtdbot.pp.ua:8003**)
|
||||
* **lp_api.url** - LongPoll server url (Default: **http://localhost:8003**)
|
||||
* **lp_api.channel_id** - Channel ID in Discord. Rightclick on the Discord text channel you want the bot to interact with and press "Copy ID", and then insert value in this (Default: **minetest**)
|
||||
* **lp_api.timeout** - Timeout connection (Default: **30**)
|
||||
* **lp_api.header** - Header if needed (Default: **nil**)
|
||||
* **lp_api.subscriber.timeout** - Timeout for subscribe (Default: **26**)
|
||||
* **lp_api.subscriber.timeout** - Timeout for subscribe (Default: **15**)
|
||||
* **lp_api.router.cmd** - Support commands or not (Default: **false**)
|
||||
|
||||
License
|
||||
|
|
19
init.lua
19
init.lua
|
@ -1,17 +1,24 @@
|
|||
lp_api = {}
|
||||
lp_api.http = minetest.request_http_api()
|
||||
lp_api.url = minetest.settings:get("lp_api.url") or "http://mtdbot.pp.ua:8003"
|
||||
lp_api.url = minetest.settings:get("lp_api.url") or "http://localhost:8003"
|
||||
lp_api.channel_id = minetest.settings:get("lp_api.channel_id") or "minetest"
|
||||
lp_api.timeout = minetest.settings:get("lp_api.timeout") or 30
|
||||
-- For secure connect set header value (Authorization: Basic bG9naW46cGFzc3dvcmQ=)
|
||||
-- where bG9naW46cGFzc3dvcmQ= is the base64(login:password)
|
||||
lp_api.header = minetest.settings:get("lp_api.header") or nil
|
||||
-- where bG9naW46cGFzc3dvcmQ= is the base64(login:password) "Content-Type: application/json; charset=utf8"
|
||||
lp_api.header = minetest.settings:get("lp_api.header") or nil
|
||||
|
||||
function lp_api.request_http(ext_url,pd,response_handler,pmethod)
|
||||
local req = {
|
||||
url = lp_api.url..ext_url,
|
||||
method = pmethod,
|
||||
post_data = pd,
|
||||
extra_headers = {lp_api.header},
|
||||
timeout = lp_api.timeout
|
||||
}
|
||||
|
||||
function lp_api.request_http(ext_url,pd,response_handler)
|
||||
local req = {url = lp_api.url..ext_url, post_data = pd, extra_headers = {lp_api.header}, timeout = lp_api.timeout}
|
||||
lp_api.http.fetch(req, function(result)
|
||||
pcall(response_handler, result)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
if lp_api.http then
|
||||
|
|
|
@ -3,17 +3,17 @@
|
|||
--
|
||||
lp_api.publisher = {}
|
||||
|
||||
function lp_api.publisher.pub_msg(name, type, title, message)
|
||||
if name == "minetest" and minetest.get_player_privs(name).shout or message:sub(1, 1) ~= "/" then
|
||||
local json_msg = minetest.write_json({player = name, type = type, title = title, message = message})
|
||||
if json_msg then
|
||||
lp_api.request_http("/pub?category="..lp_api.channel_id, json_msg, lp_api.publisher.resp_handler)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function lp_api.publisher.resp_handler(result)
|
||||
if not result.succeeded and result.code ~= 200 then
|
||||
minetest.log("[lp_api] Pub request... [ERROR]")
|
||||
end
|
||||
end
|
||||
|
||||
function lp_api.publisher.pub_msg(name, type, title, message)
|
||||
if name == "minetest" and minetest.get_player_privs(name).shout or message:sub(1, 1) ~= "/" then
|
||||
local json_msg = minetest.write_json({player = name, type = type, title = title, message = message})
|
||||
if json_msg then
|
||||
lp_api.request_http("/pub?category="..lp_api.channel_id, json_msg, lp_api.publisher.resp_handler,"PUT")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,19 +2,15 @@
|
|||
-- subscriber
|
||||
--
|
||||
lp_api.subscriber = {}
|
||||
lp_api.subscriber.timeout = minetest.settings:get("lp_api.subscriber.timeout") or 26
|
||||
|
||||
function lp_api.subscriber.sub_msg()
|
||||
lp_api.request_http("/sub?timeout="..lp_api.subscriber.timeout.."&category="..lp_api.channel_id, "",
|
||||
lp_api.subscriber.resp_handler)
|
||||
end
|
||||
lp_api.subscriber.timeout = minetest.settings:get("lp_api.subscriber.timeout") or 15
|
||||
|
||||
function lp_api.subscriber.resp_handler(result)
|
||||
if result.succeeded and result.code == 200 then
|
||||
lp_api.subscriber.sub_msg()
|
||||
local data = minetest.parse_json(string.sub(result.data, string.find(result.data, "data")+6, -4))
|
||||
if data then
|
||||
lp_api.msg_router(data)
|
||||
--local data = minetest.parse_json(string.sub(result.data, string.find(result.data, "data")+6, -4))
|
||||
local dt = minetest.parse_json(result.data)
|
||||
if dt then
|
||||
lp_api.msg_router(dt.events[1].data)
|
||||
end
|
||||
else
|
||||
minetest.after(lp_api.timeout, lp_api.subscriber.sub_msg)
|
||||
|
@ -22,4 +18,9 @@ function lp_api.subscriber.resp_handler(result)
|
|||
end
|
||||
end
|
||||
|
||||
function lp_api.subscriber.sub_msg()
|
||||
lp_api.request_http("/sub?timeout="..lp_api.subscriber.timeout.."&category="..lp_api.channel_id, nil,
|
||||
lp_api.subscriber.resp_handler,"GET")
|
||||
end
|
||||
|
||||
minetest.after(3, lp_api.subscriber.sub_msg)
|
||||
|
|
Loading…
Reference in New Issue