Add examples on how to send messages to rooms

This adds two similar examples, one using matrix.client, and the other using
matrix.api.
This commit is contained in:
Adrian Perez de Castro
2016-07-04 13:44:23 +03:00
parent fabb5330d1
commit 43470d72d1
3 changed files with 44 additions and 0 deletions

View File

@@ -69,9 +69,11 @@ api:send_text(response.room_id, "Hello!")
For the low-level `matrix.api`:
* [examples/set-display-name.lua](./examples/set-display-name.lua)
* [examples/api-send-message.lua](./examples/api-send-message.lua)
For the high-level `matrix.client`:
* [examples/get-user-info.lua](./examples/get-user-info.lua)
* [examples/client-send-message.lua](./examples/client-send-message.lua)
More examples can be found in the [examples](./examples) subdirectory.

View File

@@ -0,0 +1,18 @@
#! /usr/bin/env lua
--
-- api-send-message.lua
-- Copyright (C) 2016 Adrian Perez <aperez@igalia.com>
--
-- Distributed under terms of the MIT license.
--
if #arg ~= 4 then
io.stderr:write(string.format("Usage: %s <homeserver-URL> <username> <password> <room>\n", arg[0]))
os.exit(1)
end
local api = require "matrix" .api(arg[1])
local response = api:login("m.login.password", { user = arg[2], password = arg[3] })
api.token = response.access_token
api:send_message(arg[4], io.read("*a"))
api:logout()

View File

@@ -0,0 +1,24 @@
#! /usr/bin/env lua
--
-- send-message.lua
-- Copyright (C) 2016 Adrian Perez <aperez@igalia.com>
--
-- Distributed under terms of the MIT license.
--
if #arg ~= 4 then
io.stderr:write(string.format("Usage: %s <homeserver-URL> <username> <password> <room>\n", arg[0]))
os.exit(1)
end
local client = require "matrix" .client(arg[1])
client:login_with_password(arg[2], arg[3])
-- FIXME: This does not resolve room aliases, it works only with room IDs
local room = client.rooms[arg[4]]
if not room then
room = client:join_room(arg[4])
end
room:send_text(io.read("*a"))
client:logout()