0

I'm trying to add the option of adding a spoken message before a prerecorded message plays in my outbound calls. The closest question of this this type I could find was this Does Twilio Ruby Gem Take Other Params

I currently have a form where you enter the number you want to call and field for the message you'd like to

<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>

In my call's controller I have:

 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

Obviously the :say param is not working. I've something like this for inbound calls, but I don't believe that will work for an outbound call

 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

Any help or guidance would be greatly appreciated.

Community
  • 1
  • 1

1 Answers1

2

Twilio developer evangelist here.

When creating calls via the REST API, you need to send the from, to and either url or applications_sid parameters and there are a bunch of optional parameters, like if_machine, that you can send too. The full list is available in the REST API documentation.

However, as you have discovered, you can't send a say parameter. In order to get the result you're looking for, you'll need use your voice endpoint url to read the message, then play the file.

Since you already have a voice endpoint, you will also need to decide at the time of the call what to do.

So, if you modify your create action to something like the following, which passes the message along to your voice 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

Then in your voice endpoint, check for the message and return it, followed by the file you wanted to play, otherwise return what you had already.

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

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88