From aa560fd6b37e2d137ca4b00c027605de5e16e48c Mon Sep 17 00:00:00 2001 From: "Andrey N. Ronin" Date: Wed, 25 May 2016 14:18:12 +0400 Subject: [PATCH 1/2] Fixes and additions in Nadia.Parser parse function --- lib/nadia/parser.ex | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/nadia/parser.ex b/lib/nadia/parser.ex index 388dc31..f140a26 100644 --- a/lib/nadia/parser.ex +++ b/lib/nadia/parser.ex @@ -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. @@ -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))) @@ -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)} From 201c9457e425dc03a2919c3a5ec49fa197fbdc88 Mon Sep 17 00:00:00 2001 From: "Andrey N. Ronin" Date: Wed, 25 May 2016 14:33:38 +0400 Subject: [PATCH 2/2] Remove nil fields in request params --- lib/nadia.ex | 10 ++-------- lib/nadia/api.ex | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/nadia.ex b/lib/nadia.ex index 248cc32..e2b57b8 100644 --- a/lib/nadia.ex +++ b/lib/nadia.ex @@ -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 @@ -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 diff --git a/lib/nadia/api.ex b/lib/nadia/api.ex index ed9b4fc..bed2e26 100644 --- a/lib/nadia/api.ex +++ b/lib/nadia/api.ex @@ -40,7 +40,8 @@ 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 @@ -48,6 +49,19 @@ defmodule Nadia.API do 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.