kannix
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
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #ai
- #graphql
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











Showing Posts 1 to 9- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
idi527
Have you considered using webhooks?
kannix
yep but i think it won’t help me very much..
I am using this in some browser automation where i will forward a two factor authentication code from sms to telegram to the login process handling.. so i will end up with something like that:
Fill in email + password → click button to login → click button to send the sms → wait for sms code
Where “wait for sms code” will most likely be something similar to what I am doing now with the only difference that the waiting loop will be aborted by the webhook and not by my polling logic
Additionally I need some kind of webapp to handle the webhook (including access from the outside) which is not necessary if i use the polling method
idi527
Ah, I seem to have misunderstood the question then …
I’ve never used telegram’s polling api, but I guess the same principles as with webhooks would apply – in general, I abstract the polling / webhooks into a separate process which acts as a “telegram message producer” and allows other processes to subscribe to it, I then have another processes for it as consumers which in their turn output “actions” for a “telegram broadcaster” process … (The last one acting as a queue because telegram doesn’t allow sending more than 30 messages per second).
In your case however, it seems that this is not necessary. So maybe you can have a single process that would keep a map of auth codes as its state and match against them for every incoming message:
The map could look like
%{chat_id => auth_code}, so when you receive a message forchat_id, you compare the contents of that message withauth_codeand then do something about it …david_ex
I believe you should be returning a tuple from
handle_info/2:kannix
Nice, that looks like it goes into the right direction, thank you very much for the help
Quick additional Info: that programm is only used by me with a single chat id so I only need the last/one auth code and not a map of codes.. but I can adapt that to my needs..
Would you mind showing me an example on how I would integrate that Auther Module into my procedural browser automation code? E.g. if i have this pseudo code for my browser automation:
idi527
Note that
'mail' != "mail"in elixir, since the former is a charlist and the latter is a binary.Sure, maybe a blocking call would work
This can be achieved with
Task.await, for example, where you’d call the Auther module to add a chat id to its internal state to wait an auth code for.kannix
good catch.. edited my post to fix this..
kannix
And exactly thats the point where I am stuck. I couldn’t wrap my head around that. How would this look? Even after reading the Docs about Task i have no clue on how to build a looping async function
idi527
I don’t think you need a looping async functions … I’d do something like
Then in your browser automation program you can do
state.auth_codesshould probably be renamed tostate.awaiting_auth_codesorawaiting_chats_auth_codesomething like that. It was named incorrectly the first time since I misunderstood your problem again, it seems.