mt-matrix/lua/send.lua

46 lines
1.4 KiB
Lua
Raw Normal View History

2024-02-20 09:57:29 +00:00
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)
2024-02-20 11:00:04 +00:00
small_text = small_text or ""
strong_text = strong_text or ""
2024-02-20 09:57:29 +00:00
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)
2024-02-20 15:54:57 +00:00
return matrix.send(http_api, name .. ":", message)
2024-02-20 09:57:29 +00:00
end
end