Stefan Russo
Stefan Russo

Reputation: 11

Twilio Studio & Functions - Call Whispers

I'm trying to implement a simple call whisper that lets our agent know which phone number/product has been dialed. When a call comes in, it goes through a studio flow where the caller chooses a language. The call is then routed to a taskrouter queue via the "Enqueue" widget. From what I've read in the documentation, I need to pass the callback function a instruction: 'call' parameter in order to be able to specify a URL to be "played" to the agent.

I'm currently limited to implementing everything inside of twilio itself via Studio and Functions.

Why isn't this working, and what specifically do I need to do?

Thanks!

Assignment Callback Function

exports.handler = function(context, event, callback) {
    const client = context.getTwilioClient();
    let eventInfo = JSON.parse(event.TaskAttributes);
    // const response = new Twilio.twiml.VoiceResponse();
    console.log(Object.keys(event));
    console.log(JSON.parse(event.TaskAttributes));
    console.log(Object.keys(JSON.parse(event.TaskAttributes)));
    // console.log(JSON.parse(event.WorkerAttributes));
    const worker = JSON.parse(event.WorkerAttributes);
    // console.log("ReservationSid: " + event.ReservationSid);
    // console.log(typeof eventInfo);

    // // ATTEMPT 1 
    // // This works to dequeue a call to a worker, but there is no whisper functionality
    // callback(null, {
    //   'instruction':'dequeue',
    //   'post_work_activity_sid' : 'WORKSPACEACTIVITYSID',
    //   'from' : eventInfo.from
    // });

    // // ATTEMPT 2
    // // This works to play the whisper to the agent, but the caller is never connected to the agent.
    let callbackURL = 'TWILIOFUNCTIONURL';
    callback(null, {
      'instruction':'call',
      'post_work_activity_sid' : 'WORKSPACEACTIVITYSID',
      'from' : eventInfo.from,
      'url' : callbackURL
    });

    // // ATTEMPT 3 - Building a twiml version of attempt #2
    // // This doesn't work.
    //
    // let callbackURL = 'TWILIOFUNCTIONURL';
    // let twiml = new Twilio.twiml.VoiceResponse();
    // const dial = twiml.dial();    
    // dial.queue({
    //     'instruction':'call',
    //     // 'post_work_activity_sid' : 'WORKSPACEACTIVITYSID',
    //     'from' : eventInfo.from,
    //     'accept' : true,
    //     'url' : callbackURL,
    //     // 'reservationSid' : event.ReservationSid
    // }, event.selected_language);
    // console.log(dial.toString());
    // console.log(twiml.toString());
    // callback(null, twiml);

    // // ATTEMPT 4 - Build a twiml version of attempt #1
    // // This doesn't work.
    //
    // let twiml = new Twilio.twiml.VoiceResponse();
    // twiml.dial({
    //   'instruction':'dequeue',
    //   'post_work_activity_sid' : 'WORKSPACEACTIVITYSID',
    //   'from' : eventInfo.from
    // //   'to' : 'WORKSPACEQUEUESID'
    // });
    // console.log(twiml.toString());
    // callback(null, twiml);
};

Callback URL containing queue announcement stuff.

exports.handler = function(context, event, callback) {
    let twiml = new Twilio.twiml.VoiceResponse();
//  let client = context.getTwilioClient();
//  console.log("BEGIN Queue Announcer - context");
//  console.log(Object.keys(context));
//  console.log(context);
//  console.log("BEGIN Queue Announcer - event");
//  console.log(Object.keys(event));
//  console.log(event);
//  console.log("BEGIN Queue Announcer - client");
//  console.log(Object.keys(client));
//  console.log(client);
//  console.log("END Queue Announcer");

// Random attempt to get the call info
//  console.log("BEGIN Queue Announcer - CallSid");
//  console.log(event.CallSid);
//  client.calls(event.CallSid)
//      .fetch()
//      .then(call => console.log("Call: " + call));
//  console.log("END Queue Announcer - CallSid");


    let client = context.getTwilioClient();
    twiml.say("QUEUE NAME GOES HERE");
    twiml.dial()
    .queue({
        // 'reservationSid' : event.ReservationSid
    }, 'english');
    callback(null, twiml);
};

Upvotes: 0

Views: 556

Answers (1)

Stefan Russo
Stefan Russo

Reputation: 11

Here's what I did that worked. This is contained within a function that is effectively the "assignment callback URL".

Steps:

  1. Use the 'echo' twimlet to generate the twiml. Modify the supplied URL to include the 'event.RegistrationSid' where appropriate.
  2. Accept the registration via a POST request.
  3. Do the callback, including the 'instruction : call' parameter.

exports.handler = function(context, event, callback) {
const client = context.getTwilioClient();
let eventInfo = JSON.parse(event.TaskAttributes);

// Generate the callback url
let callbackURL = `http://twimlets.com/echo?Twiml=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3CResponse%3E%0A%3CSay%3ECALLWHISPERGOESHERE%3C%2FSay%3E%0A%3CDial%3E%0A%3CQueue%20reservationSid%3D%22${event.ReservationSid}%22%3E%3C%2FQueue%3E%0A%3C%2FDial%3E%0A%3C%2FResponse%3E&`;

// Generate POST request to accept the reservation
const rp = require("request-promise");
const postURI = `https://taskrouter.twilio.com/v1/Workspaces/${event.WorkspaceSid}/Tasks/${event.TaskSid}/Reservations/${event.ReservationSid}`;
const postForm = {
    ReservationStatus: "accepted"
};
const postAuth = {
    user: context.ACCOUNT_SID,
    pass: context.AUTH_TOKEN
};
const options = {
    method: "POST",
    uri: postURI,
    auth: postAuth,
    form: postForm
};
rp(options)
    .then(body => {
        console.log(body);
        callback(null, null);
    })
    .catch(err => {
        console.log(err);
        callback(null, err);
    });

// Call the agent
callback(null, {
    instruction: "call",
    post_work_activity_sid: "POSTWORKACTIVITYSID",
    from: eventInfo.from,
    url: callbackURL
});
};

Upvotes: 1

Related Questions