Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
defmodule Lux.Prisms.Telegram.GroupManagement.ApproveChatJoinRequest do
@moduledoc """
A prism for approving a chat join request via the Telegram Bot API.

## Examples

iex> ApproveChatJoinRequest.handler(%{
...> chat_id: 123_456_789,
...> user_id: 987_654
...> }, %{name: "Agent"})
{:ok, %{success: true, chat_id: 123_456_789}}
"""

use Lux.Prism,
name: "Approve Chat Join Request",
description: "Approves a chat join request",
input_schema: %{
type: :object,
properties: %{
chat_id: %{
type: [:string, :integer],
description: "Unique identifier for the target chat"
},
user_id: %{
type: :integer,
description: "Unique identifier of the target user"
}
},
required: ["chat_id", "user_id"]
},
output_schema: %{
type: :object,
properties: %{
success: %{
type: :boolean,
description: "Whether the operation was successful"
},
chat_id: %{
type: [:string, :integer],
description: "Identifier of the target chat"
}
},
required: ["success", "chat_id"]
}

alias Lux.Integrations.Telegram.Client
require Logger

def handler(params, agent) do
with {:ok, chat_id} <- validate_param(params, :chat_id),
{:ok, _} <- validate_param(params, :user_id) do
agent_name = agent[:name] || "Unknown Agent"
Logger.info("Agent #{agent_name} attempting to approve join request in chat #{chat_id}")

request_body = Map.take(params, [:chat_id, :user_id])
request_opts = %{json: request_body}
request_opts = if params[:plug], do: Map.put(request_opts, :plug, params[:plug]), else: request_opts

case Client.request(:post, "/approveChatJoinRequest", request_opts) do
{:ok, %{"result" => true}} ->
Logger.info("Successfully approved join request in chat #{chat_id}")
{:ok, %{success: true, chat_id: chat_id}}

{:error, {status, %{"description" => description}}} ->
{:error, "Failed to approve join request: #{description} (HTTP #{status})"}

{:error, {status, description}} when is_binary(description) ->
{:error, "Failed to approve join request: #{description} (HTTP #{status})"}

{:error, error} ->
{:error, "Failed to approve join request: #{inspect(error)}"}
end
end
end

defp validate_param(params, key) do
case Map.fetch(params, key) do
{:ok, value} when is_binary(value) and value != "" -> {:ok, value}
{:ok, value} when is_integer(value) -> {:ok, value}
_ -> {:error, "Missing or invalid #{key}"}
end
end
end
88 changes: 88 additions & 0 deletions lux/lib/lux/prisms/telegram/group_management/ban_chat_member.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
defmodule Lux.Prisms.Telegram.GroupManagement.BanChatMember do
@moduledoc """
A prism for bans a member from a chat/group via the Telegram Bot API.

## Examples

iex> BanChatMember.handler(%{
...> chat_id: 123_456_789,
...> user_id: 987_654
...> }, %{name: "Agent"})
{:ok, %{success: true, chat_id: 123_456_789}}
"""

use Lux.Prism,
name: "Ban Chat Member",
description: "Bans a member from a chat/group",
input_schema: %{
type: :object,
properties: %{
chat_id: %{
type: [:string, :integer],
description: "Unique identifier for the target chat"
},
user_id: %{
type: :integer,
description: "Unique identifier of the target user"
},
until_date: %{
type: :integer,
description: "Date when the user will be unbanned, unix time"
}
},
required: ["chat_id", "user_id"]
},
output_schema: %{
type: :object,
properties: %{
success: %{
type: :boolean,
description: "Whether the operation was successful"
},
chat_id: %{
type: [:string, :integer],
description: "Identifier of the target chat"
}
},
required: ["success", "chat_id"]
}

alias Lux.Integrations.Telegram.Client
require Logger

def handler(params, agent) do
with {:ok, chat_id} <- validate_param(params, :chat_id),
{:ok, _} <- validate_param(params, :user_id) do
agent_name = agent[:name] || "Unknown Agent"
Logger.info("Agent #{agent_name} attempting to ban member in chat #{chat_id}")

request_body = Map.take(params, [:chat_id, :user_id, :until_date])
request_opts = %{json: request_body}
request_opts = if params[:plug], do: Map.put(request_opts, :plug, params[:plug]), else: request_opts

case Client.request(:post, "/banChatMember", request_opts) do
{:ok, %{"result" => true}} ->
Logger.info("Successfully completed ban member in chat #{chat_id}")
{:ok, %{success: true, chat_id: chat_id}}

{:error, {status, %{"description" => description}}} ->
{:error, "Failed to ban member: #{description} (HTTP #{status})"}

{:error, {status, description}} when is_binary(description) ->
{:error, "Failed to ban member: #{description} (HTTP #{status})"}

{:error, error} ->
{:error, "Failed to ban member: #{inspect(error)}"}
end
end
end

defp validate_param(params, key) do
case Map.fetch(params, key) do
{:ok, value} when is_binary(value) and value != "" -> {:ok, value}
{:ok, value} when is_integer(value) -> {:ok, value}
{:ok, value} when is_map(value) -> {:ok, value}
_ -> {:error, "Missing or invalid #{key}"}
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
defmodule Lux.Prisms.Telegram.GroupManagement.CreateChatInviteLink do
@moduledoc """
A prism for creating an additional invite link for a chat via the Telegram Bot API.

## Examples

iex> CreateChatInviteLink.handler(%{
...> chat_id: 123_456_789
...> }, %{name: "Agent"})
{:ok, %{success: true, invite_link: "https://t.me/joinchat/..."}}
"""

use Lux.Prism,
name: "Create Chat Invite Link",
description: "Creates an additional invite link for a chat",
input_schema: %{
type: :object,
properties: %{
chat_id: %{
type: [:string, :integer],
description: "Unique identifier for the target chat"
},
name: %{
type: :string,
description: "Invite link name"
},
expire_date: %{
type: :integer,
description: "Point in time when the link will expire, unix time"
},
member_limit: %{
type: :integer,
description: "Maximum number of users that can be members of the chat simultaneously"
},
creates_join_request: %{
type: :boolean,
description: "True, if users joining the chat via the link need to be approved by chat administrators"
}
},
required: ["chat_id"]
},
output_schema: %{
type: :object,
properties: %{
success: %{
type: :boolean,
description: "Whether the operation was successful"
},
invite_link: %{
type: :string,
description: "The newly created invite link"
}
},
required: ["success", "invite_link"]
}

alias Lux.Integrations.Telegram.Client
require Logger

def handler(params, agent) do
with {:ok, chat_id} <- validate_param(params, :chat_id) do
agent_name = agent[:name] || "Unknown Agent"
Logger.info("Agent #{agent_name} attempting to create invite link for chat #{chat_id}")

request_body = Map.take(params, [:chat_id, :name, :expire_date, :member_limit, :creates_join_request])
request_opts = %{json: request_body}
request_opts = if params[:plug], do: Map.put(request_opts, :plug, params[:plug]), else: request_opts

case Client.request(:post, "/createChatInviteLink", request_opts) do
{:ok, %{"result" => %{"invite_link" => link}}} ->
Logger.info("Successfully created invite link for chat #{chat_id}")
{:ok, %{success: true, invite_link: link}}

{:error, {status, %{"description" => description}}} ->
{:error, "Failed to create invite link: #{description} (HTTP #{status})"}

{:error, {status, description}} when is_binary(description) ->
{:error, "Failed to create invite link: #{description} (HTTP #{status})"}

{:error, error} ->
{:error, "Failed to create invite link: #{inspect(error)}"}
end
end
end

defp validate_param(params, key) do
case Map.fetch(params, key) do
{:ok, value} when is_binary(value) and value != "" -> {:ok, value}
{:ok, value} when is_integer(value) -> {:ok, value}
_ -> {:error, "Missing or invalid #{key}"}
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
defmodule Lux.Prisms.Telegram.GroupManagement.DeclineChatJoinRequest do
@moduledoc """
A prism for declining a chat join request via the Telegram Bot API.

## Examples

iex> DeclineChatJoinRequest.handler(%{
...> chat_id: 123_456_789,
...> user_id: 987_654
...> }, %{name: "Agent"})
{:ok, %{success: true, chat_id: 123_456_789}}
"""

use Lux.Prism,
name: "Decline Chat Join Request",
description: "Declines a chat join request",
input_schema: %{
type: :object,
properties: %{
chat_id: %{
type: [:string, :integer],
description: "Unique identifier for the target chat"
},
user_id: %{
type: :integer,
description: "Unique identifier of the target user"
}
},
required: ["chat_id", "user_id"]
},
output_schema: %{
type: :object,
properties: %{
success: %{
type: :boolean,
description: "Whether the operation was successful"
},
chat_id: %{
type: [:string, :integer],
description: "Identifier of the target chat"
}
},
required: ["success", "chat_id"]
}

alias Lux.Integrations.Telegram.Client
require Logger

def handler(params, agent) do
with {:ok, chat_id} <- validate_param(params, :chat_id),
{:ok, _} <- validate_param(params, :user_id) do
agent_name = agent[:name] || "Unknown Agent"
Logger.info("Agent #{agent_name} attempting to decline join request in chat #{chat_id}")

request_body = Map.take(params, [:chat_id, :user_id])
request_opts = %{json: request_body}
request_opts = if params[:plug], do: Map.put(request_opts, :plug, params[:plug]), else: request_opts

case Client.request(:post, "/declineChatJoinRequest", request_opts) do
{:ok, %{"result" => true}} ->
Logger.info("Successfully declined join request in chat #{chat_id}")
{:ok, %{success: true, chat_id: chat_id}}

{:error, {status, %{"description" => description}}} ->
{:error, "Failed to decline join request: #{description} (HTTP #{status})"}

{:error, {status, description}} when is_binary(description) ->
{:error, "Failed to decline join request: #{description} (HTTP #{status})"}

{:error, error} ->
{:error, "Failed to decline join request: #{inspect(error)}"}
end
end
end

defp validate_param(params, key) do
case Map.fetch(params, key) do
{:ok, value} when is_binary(value) and value != "" -> {:ok, value}
{:ok, value} when is_integer(value) -> {:ok, value}
_ -> {:error, "Missing or invalid #{key}"}
end
end
end
Loading