summaryrefslogtreecommitdiff
path: root/tutorial_echo.md
diff options
context:
space:
mode:
Diffstat (limited to 'tutorial_echo.md')
-rw-r--r--tutorial_echo.md54
1 files changed, 23 insertions, 31 deletions
diff --git a/tutorial_echo.md b/tutorial_echo.md
index 69cbfca..27e7565 100644
--- a/tutorial_echo.md
+++ b/tutorial_echo.md
@@ -10,19 +10,20 @@ produce a working bot.
## Creating a client
-We start by creating a `Polyjuice.Client` struct:
+We begin by starting a client:
- iex> client = %Polyjuice.Client{
- ...> base_url: "http://localhost:8008",
+ iex> client = Polyjuice.Client.start_link(
+ ...> "http://localhost:8008",
...> access_token: "access_token",
...> user_id: "@echobot:localhost",
- ...> storage: Polyjuice.Client.Storage.Ets.open()
- ...> }
+ ...> storage: Polyjuice.Client.Storage.Ets.open(),
+ ...> handler: self()
+ ...> )
-The `base_url` parameter is the base URL to the homeserver that the bot will be
+The first parameter is the base URL to the homeserver that the bot will be
using. `access_token` is an access token for the bot to use. The access token
can be obtained by using `curl` to call the
-[login](https://matrix.org/docs/spec/client_server/r0.5.0#post-matrix-client-r0-login)
+[login](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login)
endpoint on the homeserver, or by retrieving it from an already logged-in
client. `user_id` is the Matrix user ID of the bot. And `storage` provides
some persistent storage for the client library. In the example above, it is
@@ -34,19 +35,9 @@ when it is restarted. If you wish to have persistence, you can use the
storage to disk. To use it, call `Polyjuice.Client.Storage.Dets.open/1` with
the name of a file to use for storage.
-## Sync
-
-Once the client has been created, a sync process can be started to receive
-messages. The sync can be started in a supervisor, so that if it dies, it will
-be restarted. Calling `Polyjuice.Client.API.sync_child_spec/3` will generate a
-child spec that can be passed to `Supervisor.start_link`. Its first argument
-is the client struct that we created earlier, and its second argument will be a
-`pid` that will receive messages. In our case, we will send it to `self` since
-we will handle the messages. You can also pass in some options; we will omit
-them for now.
-
- iex> children = [Polyjuice.Client.API.sync_child_spec(client, self)]
- iex> {:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
+The `handler` parameter tells the client how client events (such as Matrix
+messages) should be handled. By passing a PID (`self()`), Erlang messages will
+be sent to the given PID.
## Reacting to sync messages
@@ -56,8 +47,8 @@ Matrix server, new Matrix messages, and state changes. The messages that we
are most interested in are new Matrix messages (so that we can echo them), and
room invites (so that we can join).
-The message for a Matrix message is of the form `{:message, room_id, event}`.
-When we receive such a message, we can use the
+The message for a Matrix message is of the form `{:polyjuice_client, :message,
+{room_id, event}}`. When we receive such a message, we can use the
`Polyjuice.Client.Room.send_message/3` function to respond to the message. We
will use pattern matching to make sure to only respond to `m.text` messages,
and respond with a `m.notice` message, so that we don't create a message loop.
@@ -66,21 +57,22 @@ We can simply take the message contents from the incoming message, change the
echo. A `receive` statement to do this would look something like this:
receive do
- {:message, room_id, %{"content" => %{"msgtype" => "m.text"} = content}} ->
+ {:polyjuice_client, :message, {room_id, %{"content" => %{"msgtype" => "m.text"} = content}}} ->
Polyjuice.Client.Room.send_message(
client, room_id,
%{content | "msgtype" => "m.notice"}
)
end
-Room invites are of the form `{:invite, room_id, inviter, invite_state}`. When
-we get an invite, we can join the room using `Polyjuice.Client.Room.join/4`.
-Although that function takes four arguments, the last two are optional, and are
-not needed when responding to an invite. A `receive` statement that joins a
-room that we're invited to would look something like this:
+Room invites are of the form `{:polyjuice_client, :invite, {room_id, inviter,
+invite_state}}`. When we get an invite, we can join the room using
+`Polyjuice.Client.Room.join/4`. Although that function takes four arguments,
+the last two are optional, and are not needed when responding to an invite. A
+`receive` statement that joins a room that we're invited to would look
+something like this:
receive do
- {:invite, room_id, _inviter, _invite_state} ->
+ {:polyjuice_client, :invite, {room_id, _inviter, _invite_state}} ->
Polyjuice.Client.Room.join(client, room_id)
end
@@ -91,13 +83,13 @@ that calls itself recursively:
iex> defmodule EchoBot do
...> def loop(client) do
...> receive do
- ...> {:message, room_id, %{"content" => %{"msgtype" => "m.text"} = content}} ->
+ ...> {:polyjuice_client, :message, {room_id, %{"content" => %{"msgtype" => "m.text"} = content}}} ->
...> Polyjuice.Client.Room.send_message(
...> client, room_id,
...> %{content | "msgtype" => "m.notice"}
...> )
...>
- ...> {:invite, room_id, _inviter, _invite_state} ->
+ ...> {:polyjuice_client, :invite, {room_id, _inviter, _invite_state}} ->
...> Polyjuice.Client.Room.join(client, room_id)
...>
...> _ ->