How can I get a callback after a caller has been "enqueued"? - twilio

I want to run some code that takes a few seconds to complete (lookup the appropriate agents and call them). If I do it inline, the queueing of the call is delayed by a few seconds and the caller hears silence.
The "action" callback only triggers after the caller leaves the queue, and if I do it in the "waitUrl" callback, the call music is delayed.
Is there an elegant solution for this? Like some way to run the code async, or do it in a callback that won't affect the caller experience?
I guess I could use a 3rd party service (like Zapier, e.g. incoming webhook that calls a Twilio function from an outgoing webhook) to defer the long-running code, but I'd prefer to keep everything on the Twilio platform.

Twilio developer evangelist here.
As you've noted, there are several times that Twilio requests your application and gives you opportunities to perform these actions. But in the context of a voice call, these webhooks are synchronous.
Asynchronous webhooks come in the form of the statusCallback, but these callbacks only occur for major events in the lifecycle of the call such as queued (this is for when calls are initiated, not when they are enqueued), ringing, in-progress, completed, busy, failed or no-answer.
For asynchronous actions you want to take in response to synchronous webhooks, you will need to setup an asynchronous call or pass the long running action off to a job to be processed outside of the synchronous call flow. There isn't anything inherent in Twilio to do this for you.

Related

Cleanup on termination of durable function

Is it possibly to somehow get notified that the durable function as been or about to be terminated, such that it is possibly to initiate cleanup of the already finished activity functions for example? In my example we're sending multiple requests to a subsystem and need to revoke or refund the orders in case of the durable function is being terminated.
I don't believe that there is any way to subscribe to Terminatation events in Durable Functions, as the Durable Task Framework handles this before user code is ever invoked.
One option instead of using the explicit terminate API built into Durable Functions is to instead listen to some CustomTerminate event within your orchestration, using a Task.WhenAny() approach whenever you schedule an activity or suborchestration. Then, if you ever receive this CustomTerminate event instead of the Activity or SubOrchestration response, you could manually handle the cleanup at this point.

Synchronous API requests with Queue in Swift?

I need to execute synchronous requests on API using Swift. Requests must be queued. Meaning, if one is already in progress and it awaits response it must not be canceled or interrupted by the next synchronous request that enters queue or is already in queue.
Requests must be executed in order as they enter queue (FIFO). Next request must not start until previous is finished/completed in the queue.
Also, every single request in queue must be executed until queue is empty. Synchronous requests can enter queue at any time.
I meant to implement a Synchronous API Client as a singleton which contains its own Queue for queued requests. Requests must not stop/freeze UI. UI has to be responsive on user interaction all the time.
I know it can be done with semaphores but, unless you know what your are doing and you are completely sure how semaphores work, it is not the safest or maybe the best way do it. Otherwise, potential bugs and crashes could appear.
I'm expecting successful execution of every synchronous request that enters queue (by FIFO order, regardless if it returns success or an error as a response) and UI updates immediately after.
So, my question is what is the best way to approach and solve this problem?
Thanks for your help and time.
You can create your own DispatchQueue and put you operations on it as DispatchWorkItems. It is serial per default. Just remember to call your completions on DispatchQueue.main if you plan to update the UI.
John Sundell has a wonderful article about DispatchQueues here:
https://www.swiftbysundell.com/articles/a-deep-dive-into-grand-central-dispatch-in-swift/

how do i que calls with twilio studio so i can put callers in a que then anser when the line is free?

how do I queue calls with studio, so i can put callers in a queue if I'm on the phone once I reach 3 callers in a queue it would send them to a voice mailbox? I can see the enqueue function but not sure how to use it fully.
When a call comes in a can make it play a message then I would like it to be passed to the call queue if I'm on the phone.
Also, do I need 4 phone numbers for 3 lines or is there a way to just use one number and have extensions.
Twilio developer evangelist here.
The Enqueue widget will send a call into a TaskRouter workflow. I recommend you take a look into the TaskRouter documentation to find out what you can achieve with that. It is a fully featured call router that you can program to work the way you want.
Studio provides the Say/Play widget for you to play messages to callers.
A Twilio number can receive more than one call simultaneously and you can use the Gather input on Call widget to build extension like functionality.

Prevent twilio from calling after hours

I have an application that is not allowed to call people after 8 PM (TCPA). I push calls in batch. Twilio makes 1 call per second roughly. I want to prevent twilio from accidentally pushing calls after 8 PM local time to the area code called.
What is the approach to do this?
Twilio developer evangelist here.
I'm afraid Twilio doesn't have anything "out of the box" that can help you there. Twilio will continue to make all the calls that you have pushed to it.
You could keep a record of all the calls that you have pushed, you receive the call SID in the response to creating a call, and then as it comes close to your cutoff time for those calls, send requests to cancel them.
Alternatively, rather than sending the calls to Twilio in large batches, why not use smaller batches that you can more easily estimate the time they will take to be sent and then stop sending calls for those areas at the right time. For example, if you sent calls in batches of 60, you can be reasonably sure they will take 60 seconds for all of them to be made. You can then stop sending calls at 7:59pm.

How to grab a list of only active calls and active queues from Twilio

How do i grab only active queues while incoming call to my twilio number.Is there any way to filter only latest queues and update those queues order.
Twilio evangelist here.
I guess this depends on what you mean by "Active". If you mean, Queues that have calls waiting in them, there is not currently a way to have Twilio give you just that list.
You can ask Twilio to give you a list of Queues and then your application can filter that list using the CurrentSize parameter.
If you want to dequeue callers in a order other than the default FIFO behavior seen when you <Dial> a call into a <Queue>, you can use the REST API to dequeue a specific call:
https://www.twilio.com/docs/api/rest/member#instance-post-example-2
If that still does not give you enough control for your scenario you can always fall back to using a simple <Conference> instead of a <Queue>. In that case you would add calls to a conference in a muted state, and your application would be responsible for tracking what calls are in the conference, how long they have waited and what order they arrived.
Hope that helps. If I've misunderstood what you mean by Active, please let me know.

Resources