Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions example/lib/bot.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
defmodule Example.Bot do
use TelegramEx,
name: :example_bot,
routers: [Example.Routers.Commands, Example.Routers.Admin, Example.Routers.Survey]
routers: [
Example.Routers.Commands,
Example.Routers.Admin,
Example.Routers.Survey
]

# ── /start ─────────────────────────────────────────────────────────
# Reply keyboard with all available commands
Expand All @@ -11,9 +15,9 @@ defmodule Example.Bot do
["/html", "/keyboard", "/reply_kb"],
["/photo", "/document", "/sticker"],
["/video", "/voice", "/location"],
["/contact", "/silent", "/admin"],
["/contact", "/silent", "/reply_to"],
["/survey", "/poll", "/quiz"],
["/command_demo", "/echo hello"]
["/admin", "/command_demo", "/echo hello"]
]

ctx
Expand All @@ -36,6 +40,7 @@ defmodule Example.Bot do
/markdown — Markdown formatted message
/html — HTML formatted message
/silent — message without notification
/reply_to — reply to the command message

<b>Keyboards</b>
/keyboard — inline keyboard with callbacks
Expand Down Expand Up @@ -219,6 +224,20 @@ defmodule Example.Bot do
|> Message.send(chat["id"])
end

# ── /reply_to ──────────────────────────────────────────────────────
def handle_message(%{text: "/reply_to", chat: chat, message_id: message_id}, ctx) do
ctx
|> Message.text("This message replies to your /reply_to command.")
|> Message.reply_to(message_id)
|> Message.send(chat["id"])
end

def handle_message(%{chat: chat, reply: %{text: text}}, ctx) do
ctx
|> Message.text("You replied to message with text:\n#{text}")
|> Message.send(chat["id"])
end

# ── /admin ─────────────────────────────────────────────────────────
# FSM transition → :admin state (handled in Admin router)
def handle_message(%{text: "/admin", chat: chat}, ctx) do
Expand Down
26 changes: 26 additions & 0 deletions lib/builders/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,30 @@ defmodule TelegramEx.Builder do
|> Map.put(key, file)
|> then(&Map.put(ctx, :payload, &1))
end

defmacro __using__(_opts) do
quote do
alias TelegramEx.{API, Builder, Effect}

@type input :: map() | Effect.t()

@doc """
Sends the message without notification sound.
"""
@spec silent(input()) :: Effect.t()
def silent(input) do
Builder.put_payload(input, :disable_notification, true)
end

@doc """
Selects message to reply to.
"""
@spec reply_to(input(), String.t()) :: Effect.t()
def reply_to(input, message_id) do
Builder.put_payload(input, :reply_parameters, %{message_id: message_id})
end

defoverridable silent: 1, reply_to: 2
end
end
end
14 changes: 1 addition & 13 deletions lib/builders/contact.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ defmodule TelegramEx.Builder.Contact do
[Messages and Media](messages-and-media.md).
"""

alias TelegramEx.API
alias TelegramEx.Builder
alias TelegramEx.Effect

@type input :: map() | Effect.t()
use TelegramEx.Builder

@doc """
Sets contact information with first name and phone number.
Expand All @@ -33,14 +29,6 @@ defmodule TelegramEx.Builder.Contact do
|> Builder.put_payload(:last_name, last_name)
end

@doc """
Sends the contact without notification sound.
"""
@spec silent(input()) :: Effect.t()
def silent(input) do
Builder.put_payload(input, :disable_notification, true)
end

@doc """
Sends the contact to the specified chat.
"""
Expand Down
25 changes: 1 addition & 24 deletions lib/builders/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ defmodule TelegramEx.Builder.Document do
[Messages and Media](messages-and-media.md).
"""

alias TelegramEx.API
alias TelegramEx.Builder
alias TelegramEx.Effect

@type input :: map() | Effect.t()
use TelegramEx.Builder

@doc """
Sets the document from a URL.
Expand Down Expand Up @@ -96,25 +92,6 @@ defmodule TelegramEx.Builder.Document do
|> Builder.put_payload(:parse_mode, parse_mode)
end

@doc """
Sends the document without notification sound.

Accepts a handler context or an existing `TelegramEx.Effect` and returns an
effect with `:disable_notification` stored in the payload.

## Parameters

- `input` - Context map or effect

## Returns

`TelegramEx.Effect` with `:disable_notification` stored in the payload.
"""
@spec silent(input()) :: Effect.t()
def silent(input) do
Builder.put_payload(input, :disable_notification, true)
end

@doc """
Sends the document to the specified chat.

Expand Down
14 changes: 1 addition & 13 deletions lib/builders/location.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ defmodule TelegramEx.Builder.Location do
[Messages and Media](messages-and-media.md).
"""

alias TelegramEx.API
alias TelegramEx.Builder
alias TelegramEx.Effect

@type input :: map() | Effect.t()
use TelegramEx.Builder

@doc """
Sets the geographic coordinates.
Expand All @@ -22,14 +18,6 @@ defmodule TelegramEx.Builder.Location do
|> Builder.put_payload(:longitude, lng)
end

@doc """
Sends the location without notification sound.
"""
@spec silent(input()) :: Effect.t()
def silent(input) do
Builder.put_payload(input, :disable_notification, true)
end

@doc """
Sends the location to the specified chat.
"""
Expand Down
14 changes: 1 addition & 13 deletions lib/builders/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ defmodule TelegramEx.Builder.Message do
`sendMessage` request. See [Messages and Media](messages-and-media.md).
"""

alias TelegramEx.API
alias TelegramEx.Builder
alias TelegramEx.Effect

@type input :: map() | Effect.t()
use TelegramEx.Builder

@doc """
Sets the text content of the message.
Expand Down Expand Up @@ -60,14 +56,6 @@ defmodule TelegramEx.Builder.Message do
Builder.put_payload(input, :reply_markup, %{remove_keyboard: true})
end

@doc """
Sends the message without notification sound.
"""
@spec silent(input()) :: Effect.t()
def silent(input) do
Builder.put_payload(input, :disable_notification, true)
end

@doc """
Answers a callback query from an inline keyboard button.

Expand Down
14 changes: 1 addition & 13 deletions lib/builders/photo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ defmodule TelegramEx.Builder.Photo do
[Messages and Media](messages-and-media.md).
"""

alias TelegramEx.API
alias TelegramEx.Builder
alias TelegramEx.Effect

@type input :: map() | Effect.t()
use TelegramEx.Builder

@doc """
Sets the photo from a URL.
Expand Down Expand Up @@ -48,14 +44,6 @@ defmodule TelegramEx.Builder.Photo do
|> Builder.put_payload(:parse_mode, parse_mode)
end

@doc """
Sends the photo without notification sound.
"""
@spec silent(input()) :: Effect.t()
def silent(input) do
Builder.put_payload(input, :disable_notification, true)
end

@doc """
Sends the photo to the specified chat.
"""
Expand Down
11 changes: 1 addition & 10 deletions lib/builders/poll.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ defmodule TelegramEx.Builder.Poll do
[Messages and Media](messages-and-media.md).
"""

alias TelegramEx.API
alias TelegramEx.Builder
alias TelegramEx.Effect

@type input :: map() | Effect.t()
use TelegramEx.Builder

@spec poll(input(), String.t(), list(String.t())) :: Effect.t()
def poll(input, question, options) do
Expand Down Expand Up @@ -61,11 +57,6 @@ defmodule TelegramEx.Builder.Poll do
Builder.put_payload(input, :close_date, timestamp)
end

@spec silent(input()) :: Effect.t()
def silent(input) do
Builder.put_payload(input, :disable_notification, true)
end

@spec send(input(), integer()) :: Effect.t()
def send(input, id) do
input
Expand Down
14 changes: 1 addition & 13 deletions lib/builders/sticker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ defmodule TelegramEx.Builder.Sticker do
[Messages and Media](messages-and-media.md).
"""

alias TelegramEx.API
alias TelegramEx.Builder
alias TelegramEx.Effect

@type input :: map() | Effect.t()
use TelegramEx.Builder

@doc """
Sets the sticker by Telegram file ID.
Expand Down Expand Up @@ -38,14 +34,6 @@ defmodule TelegramEx.Builder.Sticker do
Builder.put_file_payload(input, :sticker, path)
end

@doc """
Sends the sticker without notification sound.
"""
@spec silent(input()) :: Effect.t()
def silent(input) do
Builder.put_payload(input, :disable_notification, true)
end

@doc """
Sends the sticker to the specified chat.
"""
Expand Down
14 changes: 1 addition & 13 deletions lib/builders/video.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ defmodule TelegramEx.Builder.Video do
sends. See [Messages and Media](messages-and-media.md).
"""

alias TelegramEx.API
alias TelegramEx.Builder
alias TelegramEx.Effect

@type input :: map() | Effect.t()
use TelegramEx.Builder

@doc """
Sets the video by Telegram file ID.
Expand Down Expand Up @@ -64,14 +60,6 @@ defmodule TelegramEx.Builder.Video do
Builder.put_payload(input, :cover, url)
end

@doc """
Sends the video without notification sound.
"""
@spec silent(input()) :: Effect.t()
def silent(input) do
Builder.put_payload(input, :disable_notification, true)
end

@doc """
Sends the video to the specified chat.
"""
Expand Down
14 changes: 1 addition & 13 deletions lib/builders/voice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ defmodule TelegramEx.Builder.Voice do
duration, and silent sends. See [Messages and Media](messages-and-media.md).
"""

alias TelegramEx.API
alias TelegramEx.Builder
alias TelegramEx.Effect

@type input :: map() | Effect.t()
use TelegramEx.Builder

@doc """
Sets the voice message by Telegram file ID.
Expand Down Expand Up @@ -64,14 +60,6 @@ defmodule TelegramEx.Builder.Voice do
Builder.put_payload(input, :duration, seconds)
end

@doc """
Sends the voice message without notification sound.
"""
@spec silent(input()) :: Effect.t()
def silent(input) do
Builder.put_payload(input, :disable_notification, true)
end

@doc """
Sends the voice message to the specified chat.
"""
Expand Down
6 changes: 3 additions & 3 deletions lib/types/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule TelegramEx.Types.Message do
audio: map() | nil,
caption: String.t() | nil,
message_thread_id: integer() | nil,
reply_to_message: t() | nil
reply: t() | nil
}

defstruct [
Expand All @@ -59,7 +59,7 @@ defmodule TelegramEx.Types.Message do
:voice,
:caption,
:message_thread_id,
:reply_to_message
:reply
]

@doc """
Expand All @@ -83,7 +83,7 @@ defmodule TelegramEx.Types.Message do
video: map["video"],
voice: map["voice"],
caption: map["caption"],
reply_to_message: Message.from_map(map["reply_to_message"])
reply: Message.from_map(map["reply_to_message"])
}
end
end
19 changes: 19 additions & 0 deletions test/builders/builder_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Builder do
use TelegramEx.Builder
end

defmodule TelegramEx.BuilderTest do
use ExUnit.Case

test "silent/1 adds disable_notification to payload" do
effect = Builder.silent(%{})

assert effect.ctx.payload == %{disable_notification: true}
end

test "reply_to/2 adds reply parameters to payload" do
effect = Builder.reply_to(%{}, 123)

assert effect.ctx.payload == %{reply_parameters: %{message_id: 123}}
end
end
Loading