twiliotwilio-apitwilio-twimltwilio-php

TwiML: Enqueueing inbound call with custom parameters


I am using a TwiML app and PHP to handle inbound calls. I handle the inbound call in the TwiML voice xml call back.

I enqueue the call to the task router...

$voiceResponse = new VoiceResponse;
$waitUrl = $this->generateUrl('admin.callcenter:get-call-waiting-xml',['queueEntryId' => $queueEntryId,'scriptId' => $scriptId]);
$actionUrl = $this->generateUrl('admin.callcenter:get-call-action-xml',['queueEntryId' => $queueEntryId,'scriptId' => $scriptId]);
$enqueue = $voiceResponse->enqueue('',[
    'workflowSid' => $workflowSid,
    'action' => $actionUrl,
    'method' => 'POST',
    'waitUrl' => $waitUrl,
    'waitUrlMethod' => 'GET'
]);
$xml = $voiceResponse->asXml();
$response = new Response($xml, Response::HTTP_OK, ['context-type' => 'text/xml']);
return $response;

I pass customer parameters to the action and wait callbacks by encoding parameters in the URL. However I cannot seem to add custom parameters to the task that is created as a result of the enqueue().

I've looked at using the task router status callback to add custom parameters to the task on the task.created event. This works but it is not clear that these parameters will be added to the task before it is evaluated in the task queue. There seems to be a race condition because I don't think that task router will wait on the return from the status call before putting the task in the queue. At least there is nothing in the documentation that says so.

What I want to do is to add attributes like 'customer_id' => 5 and 'customer_type' => 'preferred' to the task attributes so as to effect the worker selection specified in the workflow filters.

Many examples in the workflow documentation for the task router use custom task attributes but there no example where custom parameters are added as a result of a voice API ENQUEUE which I would think would be a very common approach.


Solution

  • Add the task attributes when you enqueue the call to taskrouter. Here is the example from twilios documentation:

    <?php
    // Download the library and copy into the folder containing this file.
    require_once '/path/to/vendor/autoload.php'; // Loads the library
    
    use Twilio\TwiML\VoiceResponse;
    
    $response = new VoiceResponse();
    $response->enqueue(null, ["workflowSid" => "WW0123456789abcdef0123456789abcdef"])
        ->task("{'account_number':'12345abcdef'}");
    print $response;
    ?>
    
    <!-- alternatively -->
    
    <?php
    $workflowSid = "WW0123456789abcdef0123456789abcdef";
    
    header('Content-Type: application/xml');
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    
    ?>
    <Response>
        <Enqueue workflowSid="<?php echo $workflowSid ?>">
            <Task>{"account_number": "12345abcdef"}</Task>
        </Enqueue>
    </Response>
    

    Further more if you decide to use the event callback and update the task there. This will cause the task to be re sent through your taskrouter workflow. Any update to a task will cause the task to restart the assigned workflow. Hope this helps!