Current section
Files
Jump to
Current section
Files
lib/teac/api/chat/announcements.ex
defmodule Teac.Api.Chat.Announcements do
alias Teac.Api
@schema NimbleOptions.new!(
broadcaster_id: [type: :string, required: true],
moderator_id: [type: :string],
message: [type: :string, required: true],
color: [type: {:in, ["blue", "green", "orange", "purple", "primary"]}],
for_source_only: [type: :boolean]
)
@doc """
Sends an announcement to the broadcaster's chat room.
Rate limit: one announcement every 2 seconds.
## Authorization
Requires a user access token with the `moderator:manage:announcements` scope, or an app access
token (in which case `:moderator_id` must be omitted).
## Options
* `:broadcaster_id` - required. The ID of the broadcaster whose chat to send the announcement to.
* `:moderator_id` - optional. The ID of the user sending the announcement. Required for user tokens; omit for app tokens.
* `:message` - required. The announcement message. Max 500 characters.
* `:color` - optional. Color used to highlight the announcement. Valid values: `"blue"`, `"green"`, `"orange"`, `"purple"`, `"primary"`. Default: `"primary"`.
* `:for_source_only` - optional. If `true`, sends the announcement only to the source channel in a shared chat session.
"""
@spec post(Teac.Client.t(), keyword()) :: {:ok, nil} | {:error, Teac.Error.t()}
def post(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @schema) do
{:ok, opts} ->
body =
%{message: opts[:message]}
|> then(fn b -> if opts[:color], do: Map.put(b, :color, opts[:color]), else: b end)
|> then(fn b ->
if opts[:for_source_only] != nil,
do: Map.put(b, :for_source_only, opts[:for_source_only]),
else: b
end)
params =
[broadcaster_id: opts[:broadcaster_id], moderator_id: opts[:moderator_id]]
|> Enum.reject(fn {_, v} -> is_nil(v) end)
[
base_url: Api.uri("chat/announcements"),
params: params,
headers: Api.headers(client),
json: body
]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.post()
|> Api.handle_response()
{:error, %NimbleOptions.ValidationError{} = err} ->
{:error, err.message}
end
end
end