-1

I am building a web app using ruby and the twilio api that allows you to call a twilio number and record a voice recording.

This is the method that gets called to record your voice:

 def getRecord()
    Twilio::TwiML::Response.new do |response|
      // userResponse = response.Gather --> does this Gather anything someone types at anytime?
      response.Say "Hello"
      response.Say "Record your message.", :voice => 'woman'
      response.Record :maxLength => '5', :trim => "trim-silence", :playBeep => "false", :action => '/feed', :method => 'get'
    end.text
  end

How do I add in "easter egg" function such as "skip the Say messages" or even, run a complete different method depending on what Digits the user hits. I tried adding the following if statement right after my response.Record call, and it does work, although it only works if the user hits * after the Recording starts and I want it to work starting from the moment the call starts.

  if userResponse['Digits'] == "*"
    getFeed()
  end 

I also tried the following:

 def getRecord()
    Twilio::TwiML::Response.new do |response|
      // userResponse = response.Gather --> does this Gather anything someone types at anytime?
     until userResponse == '*' do
      response.Say "Hello"
      response.Say "Record your message.", :voice => 'woman'
      response.Record :maxLength => '5', :trim => "trim-silence", :playBeep => "false", :action => '/feed', :method => 'get'
     end
     if userResponse == '*'
       getFeed()
     end

    end.text
  end

but this way nothing within the until loop runs at all.

How do I achieve adding a response.Gather that listens to whatever the user types in at any time during the call?

stecd
  • 1,681
  • 6
  • 19
  • 28

1 Answers1

0

I'm a developer evangelist for Twilio and I think I can help here.

Taking your original method as an example, you can do the following:

def getRecord()
  Twilio::TwiML::Response.new do |response|
    response.Gather :finishOnKey => '*' do
      response.Say "Hello"
      response.Say "Record your message.", :voice => 'woman'
      response.Record :maxLength => '5', :trim => "trim-silence", :playBeep => "false", :action => '/feed', :method => 'get'
    end
    # Whatever you want to happen when the user presses *
    getFeed()
  end.text
end

As you can see, you can nest TwiML within a Gather block. In this case, Gather will listen to key presses. If a user presses * while the messages or the recording is going on, it will jump to the end of the Gather verb and continue with the TwiML after Gather, in this case calling the method getFeed. You might want to just have it redirect to your feed endpoint, and you could use response.Redirect '/feed', :method => 'get' for that.

Let me know if this helps.

Megan Speir
  • 3,745
  • 1
  • 15
  • 25
philnash
  • 70,667
  • 10
  • 60
  • 88