|
| 1 | +defmodule TelegramEx.Builder.Voice do |
| 2 | + @moduledoc """ |
| 3 | + Builder for voice message payloads. |
| 4 | +
|
| 5 | + Supports Telegram file IDs, URLs, local files, captions, parse modes, |
| 6 | + duration, and silent sends. See [Messages and Media](messages-and-media.md). |
| 7 | + """ |
| 8 | + |
| 9 | + alias TelegramEx.API |
| 10 | + alias TelegramEx.Builder |
| 11 | + alias TelegramEx.Effect |
| 12 | + |
| 13 | + @type input :: map() | Effect.t() |
| 14 | + |
| 15 | + @doc """ |
| 16 | + Sets the voice message by Telegram file ID. |
| 17 | + """ |
| 18 | + @spec id(input(), String.t()) :: Effect.t() |
| 19 | + def id(input, id) do |
| 20 | + Builder.put_payload(input, :voice, id) |
| 21 | + end |
| 22 | + |
| 23 | + @doc """ |
| 24 | + Sets the voice message from a URL. |
| 25 | + """ |
| 26 | + @spec url(input(), String.t()) :: Effect.t() |
| 27 | + def url(input, url) do |
| 28 | + Builder.put_payload(input, :voice, url) |
| 29 | + end |
| 30 | + |
| 31 | + @doc """ |
| 32 | + Sets the voice message from a local file path. |
| 33 | +
|
| 34 | + If the file cannot be read, the returned effect contains `{:file, reason}`. |
| 35 | + """ |
| 36 | + @spec path(input(), String.t()) :: Effect.t() |
| 37 | + def path(input, path) do |
| 38 | + Builder.put_file_payload(input, :voice, path) |
| 39 | + end |
| 40 | + |
| 41 | + @doc """ |
| 42 | + Sets the voice message caption. |
| 43 | + """ |
| 44 | + @spec caption(input(), String.t()) :: Effect.t() |
| 45 | + def caption(input, caption) do |
| 46 | + Builder.put_payload(input, :caption, caption) |
| 47 | + end |
| 48 | + |
| 49 | + @doc """ |
| 50 | + Sets the voice message caption with parse mode. |
| 51 | + """ |
| 52 | + @spec caption(input(), String.t(), String.t()) :: Effect.t() |
| 53 | + def caption(input, caption, parse_mode) do |
| 54 | + input |
| 55 | + |> Builder.put_payload(:caption, caption) |
| 56 | + |> Builder.put_payload(:parse_mode, parse_mode) |
| 57 | + end |
| 58 | + |
| 59 | + @doc """ |
| 60 | + Sets the voice message duration in seconds. |
| 61 | + """ |
| 62 | + @spec duration(input(), integer()) :: Effect.t() |
| 63 | + def duration(input, seconds) do |
| 64 | + Builder.put_payload(input, :duration, seconds) |
| 65 | + end |
| 66 | + |
| 67 | + @doc """ |
| 68 | + Sends the voice message without notification sound. |
| 69 | + """ |
| 70 | + @spec silent(input()) :: Effect.t() |
| 71 | + def silent(input) do |
| 72 | + Builder.put_payload(input, :disable_notification, true) |
| 73 | + end |
| 74 | + |
| 75 | + @doc """ |
| 76 | + Sends the voice message to the specified chat. |
| 77 | + """ |
| 78 | + @spec send(input(), integer()) :: Effect.t() |
| 79 | + def send(input, id) do |
| 80 | + input |
| 81 | + |> Effect.wrap() |
| 82 | + |> Effect.then(fn ctx -> |
| 83 | + new_ctx = |
| 84 | + ctx |
| 85 | + |> Map.put(:chat_id, id) |
| 86 | + |> Map.put(:method, "sendVoice") |
| 87 | + |> Map.put(:format, :multipart) |
| 88 | + |
| 89 | + case API.request(new_ctx) do |
| 90 | + :ok -> {:ok, new_ctx} |
| 91 | + {:error, reason} -> {:error, reason} |
| 92 | + end |
| 93 | + end) |
| 94 | + end |
| 95 | +end |
0 commit comments