43 lines
1.3 KiB
Lua
43 lines
1.3 KiB
Lua
function matrix.send_message(http_api, body, formatted_body, callback, txnId)
|
|
if txnId == nil then
|
|
txnId = os.time()
|
|
end
|
|
|
|
http_api.fetch({
|
|
url = (
|
|
config.url .. "/_matrix/client/v3/rooms/" .. config.room_id ..
|
|
"/send/m.room.message/" .. txnId
|
|
),
|
|
data = json.encode({
|
|
msgtype = "m.text",
|
|
body = body,
|
|
format = "org.matrix.custom.html",
|
|
formatted_body = formatted_body
|
|
}),
|
|
extra_headers = { "Authorization: Bearer " .. config.access_token },
|
|
method = "PUT"
|
|
}, function(result)
|
|
if result.code ~= 200 then
|
|
minetest.debug("Failed to send message to Matrix homeserver with code " .. result.code)
|
|
minetest.debug(result.data)
|
|
else
|
|
if callback ~= nil then
|
|
callback(json.decode(result.data))
|
|
end
|
|
end
|
|
end)
|
|
end
|
|
|
|
function matrix.send(http_api, strong_text, small_text, callback)
|
|
return matrix.send_message(
|
|
http_api, "**" .. strong_text .. "** " .. small_text,
|
|
"<strong>" .. strong_text .. "</strong> " .. small_text,
|
|
callback
|
|
)
|
|
end
|
|
|
|
function matrix.say(http_api)
|
|
return function(name, message)
|
|
return matrix.send(name .. ":", message)
|
|
end
|
|
end |