0

enter image description here

Usecase : System makes outgoing call via twilio sstudo and gathet user response Steps:

  1. Outgoing call via twilio sdk v2 API to twilio studio.

  2. Twilio flow Trigger REST API is configured to receive as incoming call

  3. Make out going call widget[name = "call_user_1"] makes outgoing call

  4. Gather Input on call widget [name ="gather_input"] capture ressponse

  5. If I debug below java code , I see : ExcecutionStep name = "incomingRequest" ,transitionedFrom="Trigger",transitionedTo="call_user_1

  6. So the code exists and it does not even gathet input.

  7. Please see attached twilio flow and code

import com.twilio.Twilio;
import com.twilio.base.ResourceSet;
import com.twilio.rest.studio.v2.flow.Execution;
import com.twilio.rest.studio.v2.flow.execution.ExecutionContext;
import com.twilio.rest.studio.v2.flow.execution.ExecutionStep;
public class Example {
    public static final String ACCOUNT_SID = "<sid>";
     
    public static final String AUTH_TOKEN = "<token>";

    
    public static void main(String[] args) {
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
        String to = "<to>";
        String from = "<from>";
        String flowSid = "FW2c6c4c24e585c618803722e7ec6e10e5";
        com.twilio.rest.lookups.v2.PhoneNumber targetLookupPhoneNumber = com.twilio.rest.lookups.v2.PhoneNumber.fetcher(to).fetch();
        com.twilio.type.PhoneNumber toPhoneNumberTwilioType = targetLookupPhoneNumber.getPhoneNumber();
        com.twilio.rest.lookups.v2.PhoneNumber fromLookupPhoneNumber = com.twilio.rest.lookups.v2.PhoneNumber.fetcher(from).fetch();
        com.twilio.type.PhoneNumber fromPhoneNumberTwilioType = fromLookupPhoneNumber.getPhoneNumber();
        Execution execution = Execution.creator(
            flowSid,
            toPhoneNumberTwilioType,
            fromPhoneNumberTwilioType)
            .create();

        String executionSid = execution.getSid();
        // Fetch the execution context for the latest execution
        ExecutionContext executionContext = ExecutionContext.fetcher(flowSid, executionSid).fetch();

        // Get the execution step for the "gather_input" widget
        ExecutionStep executionStep = null;

        ResourceSet<ExecutionStep> steps = ExecutionStep.reader(
            flowSid,
            executionSid)
            .limit(20).read();
        for (ExecutionStep step : steps) {
            if (step.getTransitionedFrom().equals("call_user_1") && step.getName().equals("gather_input")) {
                executionStep = step;
                break;
            }
        }

        System.out.println("executionStep=" + executionStep);

       /* if (executionStep != null) {
            // Get the user input for the "gather_input" widget
            Map<String, Object> inputValues = executionStep.getInputValues();
            String userInput = (String) inputValues.get("answers");
            System.out.println("User input: " + userInput);
        } else {
            System.out.println("Could not find execution step for widget gather_input");
        }*/

    }
}

Vivek Misra
  • 165
  • 2
  • 15

1 Answers1

0

enter image description here

So I think I was making a mistake as there was no way to get response back after Say widget. So instead of ending in Say/Play widget after gathering input as user input keypad /speech from split widgets I added : http widget [name="http_1"] having 2 HTTP K-V with values being caputured from split keypress and speech widgets.

Widget name ="split_speech_result"

Variable to test="widgets.gather_input.Digits"

Widget name="split_key_press"

Variable to test="widgets.gather_input.SpeechResult"

Widget name= "http_1"

Http Parameters : key-"Digits" value-"{{widgets.gather_input.Digits}}" key-"SpeechResult" value -"{{widgets.gather_input.SpeechResult}}

Request URL : full uri of TwilioStudioReceiver rest controller for gatherResult

Below is Rest Controller with POST to receive callbacks

@RestController
@RequestMapping("/ivr")
@SuppressWarnings("ClassFanOutComplexity")
public class TwilioStudioReceiver {

    @PostMapping(value = "/gatherResult", produces = "application/xml")
    @ResponseBody
    public String handleGatherResult(
        @RequestParam(value = "Digits", required = false) String digits,
        @RequestParam(value = "SpeechResult", required = false) String speechResult) {

         
        System.out.println("speechResult=" + speechResult);
        System.out.println("Digits=" + digits);

        return null;
    }
}

Issue: I do not get speechResult everytime gathered but I can get digits everytime. Can anyone help?Does this mean speech gathering by twilio v2 sdk is not production quality

Vivek Misra
  • 165
  • 2
  • 15