Created
September 22, 2018 19:59
-
-
Save ayrat555/08b4a6f6a85336d4eeff172c2300bbb8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule QuotesBot.Polling.Logic do | |
alias QuotesBot.Config | |
alias QuotesBot.Bot.Server, as: ReplyServer | |
alias Nadia.Model.Update | |
@telegram_api Config.telegram_api | |
@spec poll(integer()) :: integer() | |
def poll(offset) do | |
offset | |
|> updates | |
|> send_replies | |
end | |
@spec updates(integer()) :: [Update.t] | |
defp updates(offset) do | |
try do | |
case @telegram_api.get_updates([offset: offset]) do | |
{:ok, new_updates} -> new_updates | |
_ -> [] | |
end | |
catch | |
[] | |
end | |
end | |
@spec send_replies([Update.t]) :: integer() | |
defp send_replies(updates) do | |
updates | |
|> Enum.reduce(0, fn(%Update{update_id: update_id} = update, _acc)-> | |
ReplyServer.reply(update) | |
update_id | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment