Current section

Files

Jump to
telega src telega@update.erl
Raw

src/telega@update.erl

-module(telega@update).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([decode/1]).
-export_type([update/0, command/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type update() :: {text_update,
integer(),
integer(),
binary(),
telega@model:message(),
telega@model:update()} |
{command_update,
integer(),
integer(),
command(),
telega@model:message(),
telega@model:update()} |
{callback_query_update,
integer(),
integer(),
telega@model:callback_query(),
telega@model:update()}.
-type command() :: {command, binary(), binary(), gleam@option:option(binary())}.
-file("src/telega/update.gleam", 92).
-spec new_callback_query_update(
telega@model:update(),
telega@model:callback_query()
) -> update().
new_callback_query_update(Raw, Callback_query) ->
{callback_query_update,
erlang:element(2, erlang:element(3, Callback_query)),
case erlang:element(4, Callback_query) of
{some, Message} ->
case Message of
{message_maybe_inaccessible_message, Message@1} ->
erlang:element(2, erlang:element(10, Message@1));
{inaccessible_message_maybe_inaccessible_message, Message@2} ->
erlang:element(2, erlang:element(2, Message@2))
end;
none ->
erlang:element(2, erlang:element(3, Callback_query))
end,
Callback_query,
Raw}.
-file("src/telega/update.gleam", 70).
-spec try_decode_to_callback_query(
telega@model:update(),
fun(() -> {ok, update()} | {error, ANWD})
) -> {ok, update()} | {error, ANWD}.
try_decode_to_callback_query(Raw, On_none) ->
case erlang:element(15, Raw) of
{some, Callback_query} ->
_pipe = new_callback_query_update(Raw, Callback_query),
{ok, _pipe};
none ->
On_none()
end.
-file("src/telega/update.gleam", 109).
-spec new_text_update(telega@model:update(), telega@model:message(), binary()) -> update().
new_text_update(Raw, Message, Text) ->
{text_update, case erlang:element(4, Message) of
{some, User} ->
erlang:element(2, User);
none ->
erlang:element(2, erlang:element(10, Message))
end, erlang:element(2, erlang:element(10, Message)), Text, Message, Raw}.
-file("src/telega/update.gleam", 135).
-spec is_command_entity(binary(), telega@model:message_entity()) -> boolean().
is_command_entity(Text, Entity) ->
((erlang:element(2, Entity) =:= <<"bot_command"/utf8>>) andalso (erlang:element(
3,
Entity
)
=:= 0))
andalso (erlang:element(4, Entity) =:= string:length(Text)).
-file("src/telega/update.gleam", 141).
-spec is_command_update(binary(), telega@model:update()) -> boolean().
is_command_update(Text, Raw_update) ->
gleam@bool:guard(
not gleam_stdlib:string_starts_with(Text, <<"/"/utf8>>),
false,
fun() -> case erlang:element(3, Raw_update) of
{some, Message} ->
case erlang:element(25, Message) of
{some, Entities} ->
gleam@list:any(
Entities,
fun(_capture) ->
is_command_entity(Text, _capture)
end
);
none ->
false
end;
none ->
false
end end
).
-file("src/telega/update.gleam", 154).
-spec extract_command(binary()) -> command().
extract_command(Text) ->
case gleam@string:split(Text, <<" "/utf8>>) of
[Command | Payload] ->
{command,
Text,
gleam@string:drop_start(Command, 1),
begin
_pipe = Payload,
_pipe@1 = gleam@string:join(_pipe, <<" "/utf8>>),
{some, _pipe@1}
end};
[] ->
{command, Text, <<""/utf8>>, none}
end.
-file("src/telega/update.gleam", 122).
-spec new_command_update(
telega@model:update(),
telega@model:message(),
binary()
) -> update().
new_command_update(Raw, Message, Text) ->
{command_update, case erlang:element(4, Message) of
{some, User} ->
erlang:element(2, User);
none ->
erlang:element(2, erlang:element(10, Message))
end, erlang:element(2, erlang:element(10, Message)), extract_command(
Text
), Message, Raw}.
-file("src/telega/update.gleam", 77).
-spec try_to_decode_message_or_command(
telega@model:update(),
fun(() -> {ok, update()} | {error, ANXJ})
) -> {ok, update()} | {error, ANXJ}.
try_to_decode_message_or_command(Raw, On_none) ->
case erlang:element(3, Raw) of
{some, Message} ->
case erlang:element(24, Message) of
{some, Text} ->
case is_command_update(Text, Raw) of
true ->
_pipe = new_command_update(Raw, Message, Text),
{ok, _pipe};
false ->
_pipe@1 = new_text_update(Raw, Message, Text),
{ok, _pipe@1}
end;
none ->
On_none()
end;
none ->
On_none()
end.
-file("src/telega/update.gleam", 52).
?DOC(" Decode a update from the Telegram API to `Update` instance.\n").
-spec decode(gleam@dynamic:dynamic_()) -> {ok, update()} |
{error, telega@error:telega_error()}.
decode(Json) ->
gleam@result:'try'(
begin
_pipe = gleam@dynamic@decode:run(
Json,
telega@model:update_decoder()
),
gleam@result:map_error(
_pipe,
fun(E) ->
{decode_update_error,
<<<<<<"Cannot decode update: "/utf8,
(gleam@string:inspect(Json))/binary>>/binary,
" "/utf8>>/binary,
(gleam@string:inspect(E))/binary>>}
end
)
end,
fun(Raw_update) ->
try_decode_to_callback_query(
Raw_update,
fun() ->
try_to_decode_message_or_command(
Raw_update,
fun() -> {error, {unknown_update_error, Raw_update}} end
)
end
)
end
).