Polyjuice Client v0.2.1 Polyjuice.Client.MsgBuilder View Source

Build a message out of composable parts.

Link to this section Summary

Functions

Escape special HTML characters.

Create an image.

Create a link.

Generate a Matrix message contents from message data.

Link to this section Functions

Link to this function

html_escape(str, esc_q)

View Source
html_escape(str :: String.t(), escape_quotes :: boolean()) :: iodata()

Escape special HTML characters.

Returns an iodata.

Examples:

iex> Polyjuice.Client.MsgBuilder.html_escape("a<>&\"", false)
...> |> IO.iodata_to_binary()
"a&lt;&gt;&amp;\""

iex> Polyjuice.Client.MsgBuilder.html_escape("a<>&b\"", true)
...> |> IO.iodata_to_binary()
"a&lt;&gt;&amp;b&quot;"
Link to this function

image(src, attrs \\ [])

View Source
image(src :: String.t(), attrs :: list()) ::
  Polyjuice.Client.MsgBuilder.MsgData.t()

Create an image.

Examples:

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.image("mxc://example.com/foo")
...> )
%{
  "msgtype" => "m.text",
  "body" => "[mxc://example.com/foo]",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<img src=\"mxc://example.com/foo\" />"
}

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.image("mxc://example.com/foo", alt: "some image")
...> )
%{
  "msgtype" => "m.text",
  "body" => "some image",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<img src=\"mxc://example.com/foo\" alt=\"some image\" />"
}

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.image(
...>     "mxc://example.com/foo", alt: "some image", width: 100
...>   )
...> )
%{
  "msgtype" => "m.text",
  "body" => "some image",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<img src=\"mxc://example.com/foo\" alt=\"some image\" width=\"100\" />"
}

Create a link.

Examples:

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.link(
...>     Polyjuice.Client.MsgBuilder.image(
...>       "mxc://example.com/foo", alt: "some image", width: 100
...>     ),
...>     "https://matrix.org/"
...>   )
...> )
%{
  "msgtype" => "m.text",
  "body" => "[some image](https://matrix.org/)",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<a href=\"https://matrix.org/\"><img src=\"mxc://example.com/foo\" alt=\"some image\" width=\"100\" /></a>"
}
Link to this function

mention(user_id, display_name \\ nil)

View Source
mention(user_id :: String.t(), display_name :: String.t() | nil) ::
  Polyjuice.Client.MsgBuilder.MsgData.t()

Mention a user.

Examples:

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.mention("@alice:example.com", "Alice")
...> )
%{
  "msgtype" => "m.text",
  "body" => "Alice",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<a href=\"https://matrix.to/#/@alice:example.com\">Alice</a>"
}

iex> Polyjuice.Client.MsgBuilder.to_message(
...>   Polyjuice.Client.MsgBuilder.mention("@alice:example.com")
...> )
%{
  "msgtype" => "m.text",
  "body" => "@alice:example.com",
  "format" => "org.matrix.custom.html",
  "formatted_body" => "<a href=\"https://matrix.to/#/@alice:example.com\">@alice:example.com</a>"
}
Link to this function

to_message(msgdata, msgtype \\ "m.text")

View Source

Generate a Matrix message contents from message data.

Examples:

iex> Polyjuice.Client.MsgBuilder.to_message("foo")
%{"msgtype" => "m.text", "body" => "foo"}

iex> Polyjuice.Client.MsgBuilder.to_message(["foo", "bar"])
%{"msgtype" => "m.text", "body" => "foobar"}