Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and addition in InputMessegeContent and Parser modules #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
10 changes: 2 additions & 8 deletions lib/nadia.ex
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ defmodule Nadia do
def get_chat_member(chat_id, user_id) do
request("getChatMember", chat_id: chat_id, user_id: user_id)
end

@doc """
Use this method to send answers to callback queries sent from inline keyboards.
The answer will be displayed to the user as a notification at the top of the chat
Expand Down Expand Up @@ -630,13 +630,7 @@ defmodule Nadia do
"""
@spec answer_inline_query(binary, [Nadia.Model.InlineQueryResult.t], [{atom, any}]) :: :ok | {:error, Error.t}
def answer_inline_query(inline_query_id, results, options \\ []) do
encoded_results = results
|> Enum.map(fn result ->
for {k, v} <- Map.from_struct(result), v != nil, into: %{}, do: {k, v}
end)
|> Poison.encode!
args = [inline_query_id: inline_query_id, results: encoded_results]

args = [inline_query_id: inline_query_id, results: results]
request("answerInlineQuery", args ++ options)
end
end
16 changes: 15 additions & 1 deletion lib/nadia/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,28 @@ defmodule Nadia.API do
defp build_request(params, file_field) do
params = params
|> Keyword.update(:reply_markup, nil, &(Poison.encode!(&1)))
|> Enum.filter_map(fn {_, v} -> v end, fn {k, v} -> {k, to_string(v)} end)
|> Enum.map(fn({k, v}) -> {k, drop_nil_fields(v)} end)

if !is_nil(file_field) and File.exists?(params[file_field]) do
build_multipart_request(params, file_field)
else
{:form, params}
end
end

defp drop_nil_fields(params) when is_list(params) do
params
|> Enum.map(&drop_nil_fields/1)
|> Poison.encode!
end
defp drop_nil_fields(params) when is_map(params) do
params
|> Map.from_struct
|> Enum.filter_map(fn {_, v} -> v != nil end, fn {k, v} -> {k, drop_nil_fields(v)} end)
|> Enum.into(%{})
end
defp drop_nil_fields(params), do: to_string(params)

@doc """
Generic method to call Telegram Bot API.

Expand Down
11 changes: 7 additions & 4 deletions lib/nadia/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defmodule Nadia.Parser do
"""

alias Nadia.Model.{User, Chat, ChatMember, Message, PhotoSize, Audio, Document, Sticker}
alias Nadia.Model.{Video, Voice, Contact, Location, Venue, Update, File}
alias Nadia.Model.UserProfilePhotos
alias Nadia.Model.{Video, Voice, Contact, Location, Venue, Update, File, UserProfilePhotos}
alias Nadia.Model.{ChosenInlineResult, InlineQuery, CallbackQuery}

@doc """
parse `result` field of decoded API response json.
Expand All @@ -30,9 +30,9 @@ defmodule Nadia.Parser do
end
end

@keys_of_message [:message, :reply_to_message]
@keys_of_message [:message, :edited_message, :reply_to_message]
@keys_of_photo [:photo, :new_chat_photo]
@keys_of_user [:from, :forward_from, :new_chat_participant, :left_chat_participant]
@keys_of_user [:from, :forward_from, :new_chat_member, :left_chat_member, :user]

defp parse(:photo, l) when is_list(l), do: Enum.map(l, &(parse(PhotoSize, &1)))
defp parse(:photos, l) when is_list(l), do: Enum.map(l, &(parse(:photo, &1)))
Expand All @@ -50,6 +50,9 @@ defmodule Nadia.Parser do
defp parse({:venue, val}), do: {:venue, parse(Venue, val)}
defp parse({:thumb, val}), do: {:thumb, parse(PhotoSize, val)}
defp parse({:photos, val}), do: {:photos, parse(:photos, val)}
defp parse({:inline_query, val}), do: {:inline_query, parse(InlineQuery, val)}
defp parse({:chosen_inline_result, val}), do: {:chosen_inline_result, parse(ChosenInlineResult, val)}
defp parse({:callback_query, val}), do: {:callback_query, parse(CallbackQuery, val)}
defp parse({key, val}) when key in @keys_of_photo, do: {key, parse(:photo, val)}
defp parse({key, val}) when key in @keys_of_user, do: {key, parse(User, val)}
defp parse({key, val}) when key in @keys_of_message, do: {key, parse(Message, val)}
Expand Down