summaryrefslogtreecommitdiff
path: root/tutorial_welcome.md
diff options
context:
space:
mode:
authorHubert Chathi <hubert@uhoreg.ca>2020-08-21 19:03:00 -0400
committerHubert Chathi <hubert@uhoreg.ca>2020-08-21 19:03:00 -0400
commit062b89962018f88c9390d7ba0822a99c68756c94 (patch)
tree982cbd9b21194284110c367a65f76890e9a072cc /tutorial_welcome.md
parentadd lower-level client, acting like the old client (diff)
automatically start sync process
Diffstat (limited to 'tutorial_welcome.md')
-rw-r--r--tutorial_welcome.md111
1 files changed, 57 insertions, 54 deletions
diff --git a/tutorial_welcome.md b/tutorial_welcome.md
index d77760f..3d91b0c 100644
--- a/tutorial_welcome.md
+++ b/tutorial_welcome.md
@@ -6,25 +6,14 @@ when they join a room. This tutorial will illustrate setting filters on the
sync, constructing messages, and responding to state events.
As before, the commands in this tutorial can be entered interactively into `iex
--S mix` to produce a working bot. And as before, we start by creating a
-`Polyjuice.Client` struct:
-
- iex> client = %Polyjuice.Client{
- ...> base_url: "http://localhost:8008",
- ...> access_token: "access_token",
- ...> user_id: "@echobot:localhost",
- ...> storage: Polyjuice.Client.Storage.Ets.open()
- ...> }
+-S mix` to produce a working bot.
Before continuing, you should make sure that the bot is already in a room.
-## Filtering
-
-We will also start a sync process as we did before. However, this time we will
-apply a filter to the sync so that we will only see the events that we are
-interested in. (We could have applied a filter to our previous bot as well,
-but the goal of the previous tutorial was to just give a quick feel for the
-library.)
+We begin by starting a client. However, this time we will apply a filter to
+the sync so that we will only see the events that we are interested in. (We
+could have applied a filter to our previous bot as well, but the goal of the
+previous tutorial was to just give a quick feel for the library.)
Filters can be constructed using the `Polyjuice.Client.Filter` module, which we
alias to `Filter` for brevity. In this bot, we are only interested in
@@ -38,14 +27,17 @@ certain types of events, it will only include those event types. If we give it
an empty list, it will give none of the events.
iex> alias Polyjuice.Client.Filter
- iex> children = [Polyjuice.Client.API.sync_child_spec(
- ...> client, self,
- ...> filter: Filter.include_presence_types([])
+ iex> client = Polyjuice.Client.start_link(
+ ...> "http://localhost:8008",
+ ...> access_token: "access_token",
+ ...> user_id: "@echobot:localhost",
+ ...> storage: Polyjuice.Client.Storage.Ets.open(),
+ ...> handler: self(),
+ ...> sync_filter: Filter.include_presence_types([])
...> |> Filter.include_ephemeral_types([])
...> |> Filter.include_state_types(["m.room.member"])
...> |> Filter.include_timeline_types(["m.room.member"])
- ...> )]
- iex> {:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
+ ...> )
Note that we include `m.room.member` events in both `state` and `timeline`.
This is because these refer to different sections of the sync response, rather
@@ -74,12 +66,14 @@ the code will look something like this:
receive do
{
- :state,
- room_id,
- %{
- "content" => %{"membership" => "join"} = content,
- "state_key" => user_id,
- "type" => "m.room.member"
+ :polyjuice_client, :state,
+ {
+ room_id,
+ %{
+ "content" => %{"membership" => "join"} = content,
+ "state_key" => user_id,
+ "type" => "m.room.member"
+ }
}
} ->
# join event: if the user was not previously a member, remember that
@@ -90,11 +84,13 @@ the code will look something like this:
# ... send welcome message
end
{
- :state,
- room_id,
- %{
- "state_key" => user_id,
- "type" => "m.room.member"
+ :polyjuice_client, :state,
+ {
+ room_id,
+ %{
+ "state_key" => user_id,
+ "type" => "m.room.member"
+ }
}
} ->
# any other `m.room.member` event, i.e. not a join event, so
@@ -149,11 +145,12 @@ purposes.
When we first start the bot, there may have already been users in the room; we
don't want to send welcome messages to these users. The sync process sends a
-message of the form `{:initial_sync_completed}` after the first sync response
-has been processed. So we can start the bot off by simply recording the
-`m.room.member` events, and when the `{:initial_sync_completed}` message
-arrives, then we start sending welcome messages. We will do this by using a
-variable that tells us if the `{:initial_sync_completed}` message was received.
+message of the form `{:polyjuice_client, :initial_sync_completed}` after the
+first sync response has been processed. So we can start the bot off by simply
+recording the `m.room.member` events, and when the `{:polyjuice_client,
+:initial_sync_completed}` message arrives, then we start sending welcome
+messages. We will do this by using a variable that tells us if the
+`{:polyjuice_client, :initial_sync_completed}` message was received.
Finally, we want to ignore membership events for the bot itself; it would be a
bit silly if the bot entered a room and then sent a welcome message to itself.
@@ -165,23 +162,27 @@ The full sync processing code would then look like this:
...> user_id = client.user_id
...> receive do
...> {
- ...> :state,
- ...> _room_id,
- ...> %{
- ...> "state_key" => ^user_id,
- ...> "type" => "m.room.member"
+ ...> :polyjuice_client, :state,
+ ...> {
+ ...> _room_id,
+ ...> %{
+ ...> "state_key" => ^user_id,
+ ...> "type" => "m.room.member"
+ ...> }
...> }
...> } ->
...> # ignore our own room membership
...> loop(client, initial_sync_completed)
...>
...> {
- ...> :state,
- ...> room_id,
- ...> %{
- ...> "content" => %{"membership" => "join"} = content,
- ...> "state_key" => user_id,
- ...> "type" => "m.room.member"
+ ...> :polyjuice_client, :state,
+ ...> {
+ ...> room_id,
+ ...> %{
+ ...> "content" => %{"membership" => "join"} = content,
+ ...> "state_key" => user_id,
+ ...> "type" => "m.room.member"
+ ...> }
...> }
...> } ->
...> # join event: if the user was not previously a member, remember that
@@ -201,11 +202,13 @@ The full sync processing code would then look like this:
...> loop(client, initial_sync_completed)
...>
...> {
- ...> :state,
- ...> room_id,
- ...> %{
- ...> "state_key" => user_id,
- ...> "type" => "m.room.member"
+ ...> :polyjuice_client, :state,
+ ...> {
+ ...> room_id,
+ ...> %{
+ ...> "state_key" => user_id,
+ ...> "type" => "m.room.member"
+ ...> }
...> }
...> } ->
...> # any other `m.room.member` event, i.e. not a join event, so
@@ -214,7 +217,7 @@ The full sync processing code would then look like this:
...> Polyjuice.Client.Storage.kv_del(client.storage, namespace, user_id)
...> loop(client, initial_sync_completed)
...>
- ...> {:initial_sync_completed} ->
+ ...> {:polyjuice_client, :initial_sync_completed} ->
...> loop(client, true)
...>
...> _ ->