Error handling
This commit is contained in:
parent
8133e567ee
commit
7b37363064
104
init.lua
104
init.lua
@ -25,28 +25,40 @@ yl_matterbridge.information.source = "https://gitea.your-land.de/AliasAlreadyTak
|
|||||||
yl_matterbridge.information.additional =
|
yl_matterbridge.information.additional =
|
||||||
"Sends and receives ingame chat to and from matterbridge https://github.com/42wim/matterbridge"
|
"Sends and receives ingame chat to and from matterbridge https://github.com/42wim/matterbridge"
|
||||||
|
|
||||||
local address = core.settings:get("yl_matterbridge.address") or "127.0.0.1"
|
local settings = {}
|
||||||
local port = core.settings:get("yl_matterbridge.port") or "4242"
|
settings.address = core.settings:get("yl_matterbridge.address") or "127.0.0.1"
|
||||||
local gateway = core.settings:get("yl_matterbridge.gateway") or "default"
|
settings.port = core.settings:get("yl_matterbridge.port") or "4242"
|
||||||
local token = core.settings:get("yl_matterbridge.token") or ""
|
settings.gateway = core.settings:get("yl_matterbridge.gateway") or "default"
|
||||||
|
settings.token = core.settings:get("yl_matterbridge.token") or ""
|
||||||
|
settings.debug = core.settings:get("yl_matterbridge.debug") or true
|
||||||
|
|
||||||
local http = core.request_http_api()
|
local http = core.request_http_api()
|
||||||
|
|
||||||
assert(http, "[MOD] yl_matterbridge: Please add yl_matterbridge to secure.http_mods")
|
assert(http, "[MOD] yl_matterbridge: Please add yl_matterbridge to secure.http_mods")
|
||||||
|
|
||||||
local post_headers = {"Content-Type: application/json"}
|
if settings.debug then
|
||||||
if token and token ~= "" then
|
core.log("action", "[MOD] yl_matterbridge : settings = " .. dump(settings))
|
||||||
table.insert(post_headers, "Authorization: Bearer " .. token)
|
|
||||||
end
|
end
|
||||||
local url_send = "http://" .. address .. ":" .. port .. "/api/message"
|
|
||||||
local url_receive = "http://" .. address .. ":" .. port .. "/api/messages"
|
local post_headers = {"Content-Type: application/json"}
|
||||||
|
if settings.token and settings.token ~= "" then
|
||||||
|
table.insert(post_headers, "Authorization: Bearer " .. settings.token)
|
||||||
|
end
|
||||||
|
local url_send = "http://" .. settings.address .. ":" .. settings.port .. "/api/message"
|
||||||
|
local url_receive = "http://" .. settings.address .. ":" .. settings.port .. "/api/messages"
|
||||||
|
|
||||||
function send(user_name, message_text)
|
function send(user_name, message_text)
|
||||||
|
if settings.debug then
|
||||||
|
core.log(
|
||||||
|
"action",
|
||||||
|
"[MOD] yl_matterbridge : send user_name = " .. dump(user_name) .. ", message_text = " .. dump(message_text)
|
||||||
|
)
|
||||||
|
end
|
||||||
local timeout = 10
|
local timeout = 10
|
||||||
local data = {
|
local data = {
|
||||||
text = message_text,
|
text = message_text,
|
||||||
username = user_name,
|
username = user_name,
|
||||||
gateway = gateway
|
gateway = settings.gateway
|
||||||
}
|
}
|
||||||
|
|
||||||
http.fetch(
|
http.fetch(
|
||||||
@ -57,19 +69,27 @@ function send(user_name, message_text)
|
|||||||
post_data = core.write_json(data)
|
post_data = core.write_json(data)
|
||||||
},
|
},
|
||||||
function(result)
|
function(result)
|
||||||
if result.succeeded then
|
if not result.completed or not result.succeeded or not result.code == 200 then
|
||||||
local data = core.parse_json(result.data)
|
|
||||||
core.log("action", "[MOD] yl_matterbridge : Posted " .. dump(data))
|
|
||||||
else
|
|
||||||
core.log("error", "[MOD] yl_matterbridge: send/http.fetch failed. Result = " .. dump(result))
|
core.log("error", "[MOD] yl_matterbridge: send/http.fetch failed. Result = " .. dump(result))
|
||||||
return false
|
return false
|
||||||
|
else
|
||||||
|
if settings.debug then
|
||||||
|
local data = core.parse_json(result.data)
|
||||||
|
core.log(
|
||||||
|
"action",
|
||||||
|
"[MOD] yl_matterbridge : send result = " .. dump(result) .. ", data = " .. dump(data)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function receive()
|
local function receive()
|
||||||
|
if settings.debug then
|
||||||
|
core.log("action", "[MOD] yl_matterbridge : receive")
|
||||||
|
end
|
||||||
local timeout = 0
|
local timeout = 0
|
||||||
|
|
||||||
http.fetch(
|
http.fetch(
|
||||||
@ -79,6 +99,38 @@ local function receive()
|
|||||||
timeout = timeout
|
timeout = timeout
|
||||||
},
|
},
|
||||||
function(result)
|
function(result)
|
||||||
|
if not result.completed or not result.succeeded or not result.code == 200 then
|
||||||
|
core.log("error", "[MOD] yl_matterbridge: receive/http.fetch failed. Result (req) = " .. dump(result))
|
||||||
|
return false
|
||||||
|
else
|
||||||
|
local data = core.parse_json(result.data)
|
||||||
|
if not data then
|
||||||
|
if not next(data) then
|
||||||
|
if settings.debug then
|
||||||
|
core.log(
|
||||||
|
"error",
|
||||||
|
"[MOD] yl_matterbridge: receive/http.fetch failed. Result (data) = " .. dump(result)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
for _, dataset in ipairs(data) do
|
||||||
|
if dataset then
|
||||||
|
if settings.debug then
|
||||||
|
core.log("action", "[MOD] yl_matterbridge : dataset = " .. dump(dataset))
|
||||||
|
end
|
||||||
|
yl_matterbridge.receive_all_from_bridge(dataset)
|
||||||
|
else
|
||||||
|
core.log(
|
||||||
|
"error",
|
||||||
|
"[MOD] yl_matterbridge: receive/http.fetch failed. No dataset in returned data = " ..
|
||||||
|
dump(data)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
--[[
|
||||||
if result.succeeded then
|
if result.succeeded then
|
||||||
local data = core.parse_json(result.data)
|
local data = core.parse_json(result.data)
|
||||||
for _, v in ipairs(data) do
|
for _, v in ipairs(data) do
|
||||||
@ -91,6 +143,8 @@ local function receive()
|
|||||||
core.log("error", "[MOD] yl_matterbridge: receive/http.fetch failed. Result = " .. dump(result))
|
core.log("error", "[MOD] yl_matterbridge: receive/http.fetch failed. Result = " .. dump(result))
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
]]
|
||||||
|
--
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@ -103,6 +157,24 @@ function yl_matterbridge.receive_from_bridge(user_name, message_text, account_na
|
|||||||
core.chat_send_all("<" .. account_name .. "|" .. user_name .. "> " .. message_text)
|
core.chat_send_all("<" .. account_name .. "|" .. user_name .. "> " .. message_text)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Overwrite this function instead if you want to use other fields coming from the bridge.
|
||||||
|
|
||||||
|
function yl_matterbridge.receive_all_from_bridge(dataset)
|
||||||
|
if dataset.username and dataset.text and dataset.account then
|
||||||
|
local user_name = dataset.username
|
||||||
|
local message_text = dataset.text
|
||||||
|
local account_name = dataset.account
|
||||||
|
yl_matterbridge.receive_from_bridge(user_name, message_text, account_name)
|
||||||
|
else
|
||||||
|
core.log(
|
||||||
|
"error",
|
||||||
|
"[MOD] yl_matterbridge: receive/http.fetch failed. No dataset in returned data = " .. dump(dataset)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Call this function to feed into the bridge from your own mod, if you unset yl_matterbridge.chat_message
|
||||||
|
|
||||||
function yl_matterbridge.send_to_bridge(user_name, message_text)
|
function yl_matterbridge.send_to_bridge(user_name, message_text)
|
||||||
send(user_name, message_text)
|
send(user_name, message_text)
|
||||||
end
|
end
|
||||||
@ -119,7 +191,7 @@ function yl_matterbridge.register_chat()
|
|||||||
core.register_on_chat_message(yl_matterbridge.chat_message)
|
core.register_on_chat_message(yl_matterbridge.chat_message)
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_mods_loaded(yl_matterbridge.register_chat)
|
core.register_on_mods_loaded(yl_matterbridge.register_chat)
|
||||||
|
|
||||||
core.register_globalstep(receive)
|
core.register_globalstep(receive)
|
||||||
|
|
||||||
|
@ -21,4 +21,9 @@ yl_matterbridge.gateway (Bridge gateway) string "default"
|
|||||||
# The token the matterbridge server
|
# The token the matterbridge server
|
||||||
# If you don't configure this, every wellformed message to the right port will be relayed
|
# If you don't configure this, every wellformed message to the right port will be relayed
|
||||||
# Optional
|
# Optional
|
||||||
yl_matterbridge.token (Bridge token) string ""
|
yl_matterbridge.token (Bridge token) string ""
|
||||||
|
|
||||||
|
# Whether or not debugging messages are enabled
|
||||||
|
# If set to true, the mod will write every send and receive to the log, among other debug messages.
|
||||||
|
# Optional, default false
|
||||||
|
yl_matterbridge.debug (Debug mode) boolean false
|
Loading…
Reference in New Issue
Block a user