Rajesh Rajamani
Rajesh Rajamani

Reputation: 549

Twilio Autopilot Dynamic Say Action with variables from memory

I have a situation where I'm able to store additional details about a customer in memory . But Im unable to access them in the Task Definition .

E.g I'm storing the customer's last purchase amount and last purchase date to be presented if the customer wants to hear it as follows

{
"actions": [
    {
        "remember": {
            "last_Purchase": "17585",
            "last_Date":"25-Dec-2020"
        }
    },
    {
        "listen":True
    }
]
}

They get stored in the memory . However I'm unable to use this in a subsequent task (I'm not using it in the same task as Twilio doesnt supports it ) .

In a subsequent task I want to be able to create a dynamic Say in a task as follows

Dear Customer your last purchase is {memory.last_Purchase} on {memory.last_Date}. 

But I guess the syntax is wrong or the way I'm accessing the memory variable is wrong .

Request guidance .

Upvotes: 0

Views: 452

Answers (1)

lizziepika
lizziepika

Reputation: 3300

Twilio developer evangelist here.

In JavaScript, you'll need to put a money sign ("$") in front of the brackets surrounding your variable name, so your Say Action would look something like this in JavaScript, like in a Twilio Function:

say = {
      "say": `Dear Customer your last purchase is ${memory.last_Purchase} on ${memory.last_Date}.`
    }

Additionally, objects saved with the Remember Action are placed at the top-level of the Memory object, so make sure you pull it out with JSON.parse:

let memory = JSON.parse(event.Memory);

The total JS code (say, in a Twilio Function) would look something like

exports.handler = function(context, event, callback) {
    let actions = [];
    let say = {};
    let memory = JSON.parse(event.Memory);

    say = {
      "say": `Dear Customer your last purchase is ${memory.last_Purchase} on ${memory.last_Date}.`
    }
    actions.push(say);
    let respObj = {
        "actions": actions
    };
    callback(null, respObj);    
};

Of course, alternatively, you could use

say = {
      "say": "Dear Customer your last purchase is " + memory.last_Purchase + "on 
" + memory.last_Date
}

Upvotes: 2

Related Questions