twiliotwilio-apitwilio-functionstwilio-studio

Twilio Send & Wait for Reply widget


I'm using an incoming call flow that starts call recording, asks a bunch of questions, gathers responses, at the end of the call: stops recording and sends a sms to caller using send/wait for reply widget. A response is expected and based on what's in the body of the incoming call, it calls a function. All this works, except, I am not receiving a response back from the caller.

  1. My concurrent call setting =off
  2. incoming trigger = incoming call

the flow is tied to a phone number (voice)

I'm not sure how to get a reply back into the same flow. Do I need to attach something to the message section of the phone number? Any guidance would be appreciated


Solution

  • A Studio flow execution represents one call, one SMS, or the incoming call trigger from the REST Trigger. As the call initiates your flow, it will terminate when the call ends.

    But you can work around this by using a function that gets invoked when the recording is done. This function can then use the Twilio APIs to fetch contextual information from the call and trigger the REST API interface of the same flow (but with a different trigger).

    I created a small example that does something similar:

    1. The flow is triggered by a call, starts a recording, and gathers dataenter image description here

    2. There is a recording callback URL that points to my function

    // This is your new function. To start, set the name and path on the left.
    
    exports.handler = function (context, event, callback) {
    
      console.log(`Recording ${event.RecordingSid} state changed to ${event.RecordingStatus}.`)
    
      if (event.RecordingStatus === "completed") {
    
        const client = context.getTwilioClient();
    
        return client.calls(event.CallSid).fetch()
          .then(call => {
            client.studio.v2.flows(<Flow ID>)
              .executions
              .create({
                to: call.from,
                from: <YOUR NUMBER>,
                parameters: {
                  RecordingUrl: event.RecordingUrl,
                }
              })
              .then(execution => {
                console.log(`Triggered execution ${execution.sid}`)
                return callback(null, "OK");
              });
          })
          .catch(callback)
      }
    
      return callback(null, "OK");
    };
    

    You can find the ID of your flow in the console (or when you click on the root element and check the Flow Configuration): enter image description here

    1. The REST API triggers a second flow execution that reads the parameter and uses them to send a text message: enter image description here