Iunknown
Iunknown

Reputation: 367

Anyone know how to record and playback a recording with Twilio?

I'm trying to implement a simple 'voicemail' sort of system, where the voicemail owner can record their own greeting.

I figured I could do something like this:

  response.Say("Record your message");
  response.Record(recordingStatusCallback: new Uri("http://<snip>.ngrok.io/voice/playmessage"));
  response.Say("Are you happy with this recording?");
  return TwiML(response);

then in the playmessage method

  [HttpPost]
  public TwiMLResult PlayMessage()
  {
    string url = Request.Form["RecordingUrl"];
    var vr = new VoiceResponse();
    vr.Play(url: new Uri(url));
    return TwiML(vr);
  }

It's hanging up right after the record, then the PlayMessage method gets called.

I tried adding a RecordingEventEnum.InProgress, like this:

response.Record(recordingStatusCallback: new Uri("http://<snip>.ngrok.io/voice/playmessage"), recordingStatusCallbackEvent: new List<Record.RecordingEventEnum> {Record.RecordingEventEnum.InProgress });

and the method APPEARS to be called, but it doesn't play anything and just hangs up.

I haven't gotten to the all the 'Press 1 if you're happy with your recording, press 2 to try again, etc...' stuff.

I figure I'm not calling the 'record' method correctly...does anyone know the magic sauce?

thanks,

Gene

Upvotes: 0

Views: 290

Answers (1)

yvesonline
yvesonline

Reputation: 4847

Use the action attribute instead of the recordingStatusCallback:

response.Record(action: new Uri("http://<snip>.ngrok.io/voice/playmessage"));

From the doc:

The action attribute takes a relative or absolute URL as a value. When recording is finished, Twilio will make a GET or POST request to this URL including the parameters below.

(Note "parameters below" include the RecordingUrl.)

Be careful though:

There is one exception: if Twilio receives an empty recording, it will not make a request to the action URL. The current call flow will continue with the next verb in the current TwiML document.

That being said your response.Say("Are you happy with this recording?"); should be part of the PlayMessage() after the Play.

Also have a look at the example Record a voicemail from the Twilio API doc.

Upvotes: 2

Related Questions