-1

我正在使用 ruby​​ 和 twilio api 构建一个 Web 应用程序,它允许您拨打 twilio 号码并录制语音记录。

这是被调用来录制您的声音的方法:

 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

如何添加“复活节彩蛋”功能,例如“跳过说消息”,甚至根据用户点击的数字运行完全不同的方法。我尝试在通话后立即添加以下if语句response.Record,它确实有效,尽管它仅在用户*在录音开始后点击时才有效,并且我希望它从通话开始的那一刻开始工作。

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

我还尝试了以下方法:

 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

但是这样until循环中的任何东西都不会运行。

如何response.Gather在通话期间随时添加监听用户输入的内容?

4

1 回答 1

0

我是 Twilio 的开发人员宣传员,我想我可以在这里提供帮助。

以您的原始方法为例,您可以执行以下操作:

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

如您所见,您可以将TwiML嵌套在 Gather 块中。在这种情况下,Gather 将监听按键。如果用户在消息或录音进行时按下 *,它将跳转到 Gather 动词的末尾并在 Gather 之后继续 TwiML,在这种情况下调用方法getFeed。您可能只想让它重定向到您的提要端点,您可以使用response.Redirect '/feed', :method => 'get'它。

让我知道这是否有帮助。

于 2014-09-08T17:06:27.093 回答