APIs & Integrations

shahamit
Membro

How can I update session property in my javascript code script?

Hello, I have a chatflow with a "run a code snippet" node that contains my custom javascript code. From the documentation page I understand that I can get the session property in the event object. How can I update a session property (custom) while constructing the response json object so that I can use it in the next node in the chatflow?

I tried to add a "session" object in the responsejson object (shared below) but that didn't work out. Can you guide me with sample code that I can refer to?

 

const responseJson = { 
      botMessage: response.body.answer,
      session: {
        properties: {            
            "User Question": {        
                "value": response.body.prompt,  //follow up question
            }
        }
      },
      responseExpected: false
   }
   callback(responseJson);


Thanks.

0 Avaliação positiva
3 Respostas 3
Derek_Gervais
Alunos da HubSpot
Alunos da HubSpot

How can I update session property in my javascript code script?

Hey @shahamit ,

 

Thanks for reaching out. After chatting with the team, it turns out that we do support a custom state that is persisted between modules. The field is called customState, and you can use it like so:

 

const responseJson = {
  "customState" : {"key" "value": , "key2" : "value2"}, //Set 'customState' in the user's 'event.session.customState'. 'customState' is persisted throughout the entire bot. 
  "responseExpected": true,
  "allowUserInput" : true, // User is forced to select a quick reply
  "botMessage": message
}

One important thing to keep in mind is that writing to customState will overwrite the entire object. It's important to explicitly pass along any/all values that you need to pass on from module to module.

0 Avaliação positiva
shahamit
Membro

How can I update session property in my javascript code script?

Hi Derek,

Appreciate your effort in helping out. I have a follow up question on the solution that you detailed below - How do I access the keys from the customState object in the next node? I don't see them as ContactToken 's.

 

 

ggDKPRFHere's the chatflow that I have. I want to access the customState keys that are set in the "CallQnAMaker" node (through custom javascript) in the "Prompt" node to show it as a message to the user.


0 Avaliação positiva
Derek_Gervais
Alunos da HubSpot
Alunos da HubSpot

How can I update session property in my javascript code script?

Hey @shahamit ,

 

The values you provide customStatearen't pushed to contact properties, so they're not available as personalization tokens. You can only access customStateprogramatically in your code snippet, like this:

exports.main = (event, callback) => {
  const customState = JSON.stringify(event.session.customState);
customState.newField = "This is a new field"; const responseJson = { "customState" : customState, "responseExpected": true, "allowUserInput" : true, "botMessage": customState } callback(responseJson); };

I know this snippet doesn't actually mean anything, but here I'm accessing the customState (presumably set in a previous snippet) with event.session.customState. I'm setting a new field on the customStateobject, then updating it in the responseJson.

0 Avaliação positiva