Error handling

This commit is contained in:
AliasAlreadyTaken 2022-03-02 08:18:22 +01:00
parent 8133e567ee
commit 7b37363064
2 changed files with 94 additions and 17 deletions

104
init.lua
View File

@ -25,28 +25,40 @@ yl_matterbridge.information.source = "https://gitea.your-land.de/AliasAlreadyTak
yl_matterbridge.information.additional =
"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 port = core.settings:get("yl_matterbridge.port") or "4242"
local gateway = core.settings:get("yl_matterbridge.gateway") or "default"
local token = core.settings:get("yl_matterbridge.token") or ""
local settings = {}
settings.address = core.settings:get("yl_matterbridge.address") or "127.0.0.1"
settings.port = core.settings:get("yl_matterbridge.port") or "4242"
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()
assert(http, "[MOD] yl_matterbridge: Please add yl_matterbridge to secure.http_mods")
local post_headers = {"Content-Type: application/json"}
if token and token ~= "" then
table.insert(post_headers, "Authorization: Bearer " .. token)
if settings.debug then
core.log("action", "[MOD] yl_matterbridge : settings = " .. dump(settings))
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)
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 data = {
text = message_text,
username = user_name,
gateway = gateway
gateway = settings.gateway
}
http.fetch(
@ -57,19 +69,27 @@ function send(user_name, message_text)
post_data = core.write_json(data)
},
function(result)
if result.succeeded then
local data = core.parse_json(result.data)
core.log("action", "[MOD] yl_matterbridge : Posted " .. dump(data))
else
if not result.completed or not result.succeeded or not result.code == 200 then
core.log("error", "[MOD] yl_matterbridge: send/http.fetch failed. Result = " .. dump(result))
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
)
return true
end
local function receive()
if settings.debug then
core.log("action", "[MOD] yl_matterbridge : receive")
end
local timeout = 0
http.fetch(
@ -79,6 +99,38 @@ local function receive()
timeout = timeout
},
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
local data = core.parse_json(result.data)
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))
return false
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)
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)
send(user_name, message_text)
end
@ -119,7 +191,7 @@ function yl_matterbridge.register_chat()
core.register_on_chat_message(yl_matterbridge.chat_message)
end
minetest.register_on_mods_loaded(yl_matterbridge.register_chat)
core.register_on_mods_loaded(yl_matterbridge.register_chat)
core.register_globalstep(receive)

View File

@ -21,4 +21,9 @@ yl_matterbridge.gateway (Bridge gateway) string "default"
# The token the matterbridge server
# If you don't configure this, every wellformed message to the right port will be relayed
# 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