kannix
Poll Telegram API until response
I am currently stuck with an implementation of a function that has to poll the Telegram API periodically and wait for a response from this. I guess there is some async / await magic that will help me to solve this problem in an elegant way. Maybe someone can jump in an give me a hint 
Other suggestions for improvement are also very welcome 
defmodule TelegramHelper do
@moduledoc """
Documentation for TelegramHelper.
"""
import Nadia
@chat_id 123456
@spec get_sms_verification_code() :: any()
def get_sms_verification_code do
# telegram api will return some old messages from time to time
# so we have to check if there are old message and get the update_id to poll later only for newer messages
update_id = 0
{:ok, result} = get_updates
if length(result) > 0 do
update_id = List.last(result).update_id + 1
end
send_message(@chat_id, "Please send the Auth Code")
get_reply(update_id)
end
defp get_reply(update_id) do
Process.sleep(1000)
{:ok, result} = get_updates(%{:update_id => update_id})
if length(result) == 0 do
# this should stop after a timeout of e.g. 5 Minutes
# and is obviously the wrong way of doing it
get_reply(update_id)
end
# this is only for reference on what i want to return if i get back a new message from telegram
List.first(result).message.text
end
end
Most Liked
david_ex
I believe you should be returning a tuple from handle_info/2:
def handle_info(:poll, state) do
case get_updates(last_update_id) do
[] ->
Process.send_after(self(), :poll, 5000)
{:noreply, state}
new_messages ->
state = handle_new_messages(state, new_messages)
{:noreply, state}
end
end
2
Popular in Questions
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Hi all
I want to have a unix time, from the current time plus 1 hour.
DateTime.now + 1 hour
How to get it in elixir?
Thanks
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work.
Or rather, not char, but a substr...
New
Other popular topics
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New







