0

我正在尝试添加在我的外拨电话中播放预先录制的消息之前添加语音消息的选项。我能找到的这种类型的最接近的问题是Twilio Ruby Gem Take Other Params

我目前有一个表格,您可以在其中输入要拨打的号码和要发送的消息字段

<form action="calls" method="post">
    <input type="text" name="number" placeholder="number e.g. 2124095555" />
    <input type="text" name="message" placeholder="add a message" />
    <input type="submit" value="Roll em!">
</form>

在我的通话控制器中,我有:

 def create

    data = {
      :from => CALLER_ID,
      :to => params['number'],
      :say => params['message'],
      :url => 'http://howenstine.co/rick_roll.mp3',
      :if_machine => 'Continue'
    }

    begin
      client = Twilio::REST::Client.new(ACCOUNT_SID, ACCOUNT_TOKEN)
      client.account.calls.create(data)
    rescue StandardError => bang
      redirect_to :action => '.', 'msg' => "Error #{bang}"
      return
    end
    redirect_to root_path
  end

显然 :say 参数不起作用。对于呼入电话,我有类似的东西,但我认为这不适用于呼出电话

 def voice
    response = Twilio::TwiML::Response.new do |r|
      r.Say 'fooo bar', :voice => 'alice'
         r.Play 'http://linode.rabasa.com/cantina.mp3'
    end

    render_twiml response
  end

任何帮助或指导将不胜感激。

4

1 回答 1

2

Twilio 开发人员布道者在这里。

通过 REST API 创建调用时,您需要发送fromtourlapplications_sid参数,还有一堆可选参数,例如if_machine,您也可以发送。完整列表可在 REST API 文档中找到

但是,正如您所发现的,您不能发送say参数。为了获得您正在寻找的结果,您需要使用您的语音端点 url 来阅读消息,然后播放文件。

由于您已经有一个语音端点,您还需要在通话时决定要做什么。

因此,如果您将您的create操作修改为类似以下内容,这会将消息传递到您的语音 URL:

def create
  data = {
    :from => CALLER_ID,
    :to => params['number'],
    :url => voice_url(:message => params['message']),
    :if_machine => 'Continue'
  }

  begin
    client = Twilio::REST::Client.new(ACCOUNT_SID, ACCOUNT_TOKEN)
    client.account.calls.create(data)
  rescue StandardError => bang
    redirect_to :action => '.', 'msg' => "Error #{bang}"
    return
  end
  redirect_to root_path
end

然后在您的语音端点中,检查消息并将其返回,然后是您要播放的文件,否则返回您已经拥有的文件。

def voice
  if params['message'].present?
    # if we have a message, say it followed by the rick roll
    response = Twilio::TwiML::Response.new do |r|
      r.Say params['message'], :voice => 'alice'
      r.Play 'http://howenstine.co/rick_roll.mp3'
    end
  else
    # otherwise return the original response
    response = Twilio::TwiML::Response.new do |r|
      r.Say 'fooo bar', :voice => 'alice'
      r.Play 'http://linode.rabasa.com/cantina.mp3'
    end
  end

  render_twiml response
end

让我知道这是否有帮助。

于 2014-07-28T10:21:31.377 回答