How to use pipeline action: Evaluate JavaScript simple commands

Overview

Script Choice Pipeline module evaluates custom JavaScript to determine next state/pipeline for the session.

JavaScript has access to all of the session properties (meta data) like:

  1. Seeker Profile
  2. Seeker Question
  3. Seeker Language
  4. Seeker Intent
  5. SessionId
  6. Skill Tags

Session metadata JSON representation

    "Guest": {
        "Name": "John Doe",
        "Id": "dl_vBFbdIv1Y0aPMOsabBBC9Q@30ce8ae3-4ac8-4a25-b2e3-a313841a625d",
        "Question": "I am unable to access Hardware Access Center for laptop replacement",
        "Locale": "en-US",
        "ChannelId": "directline",
        "GivenName": "John",
        "Surname": "Doe",
        "TenantName": "Instant"
    },
    "Session": {
        "SessionId": "a5d1fe78-d954-47bd-b196-ba4ac5da15d0",
        "StartedUtc": "2022-09-26T10:47:53.7976577+00:00",
        "ChatID": "6btnBr9ATyp5a4onmInj1D-us",
        "ReferrerUrl": "https://chime5-entrybot.azurewebsites.net/admin",
        "CurrentPipelineStage": {
            "Name": "chatend",
            "Type": "SendTextState"
        },
        "PipelinePath": [
            {
                "ToState": "start",
                "ToStateType": "SendTextState",
                "FromState": "StartChat",
                "Timestamp": "2022-09-26T10:47:59.2284505+00:00"
            },
            {
                "ToState": "azurelookup",
                "ToStateType": "KeyPhraseExtractionState",
                "FromState": "start",
                "FromStateType": "SendTextState",
                "Timestamp": "2022-09-26T10:48:02.8726515+00:00"
            },
            {
                "ToState": "chatend",
                "ToStateType": "SendTextState",
                "FromState": "azurelookup",
                "FromStateType": "KeyPhraseExtractionState",
                "Timestamp": "2022-09-26T10:48:23.4607605+00:00"
            }
        ],
        "SkillTagList": [],
        "AverageWaitTime": {
            "String": "0:00:00",
            "Seconds": 0,
            "Minutes": 0,
            "TotalSeconds": 0,
            "TotalMinutes": 0
        },
        "AverageWaitTimeLastHour": {
            "String": "0:00:00",
            "Seconds": 0,
            "Minutes": 0,
            "TotalSeconds": 0,
            "TotalMinutes": 0
        }
    },
    "clientActivityID": "1664189273805oy691xusb5",
    "azurelookup_language": "English",
    "azurelookup_keywords": "hardware,access,center,laptop",
    "azurelookup_entities": "hardware, laptop",
    "System": {
        "CurrentTimeUTC": "2022-09-26T10:48:24.7578914Z",
        "BaseUrl": "https://chime5-entrybot.azurewebsites.net",
        "Tenant": "ChimeV5",
        "PipelineName": "Azure Keyword Extraction Sample",
        "MediaUrl": "https://chime5-entrybot.azurewebsites.net/media/"
    }
}

Session Metadata Wrapper Variable

Session metadata is accessible through JavaScript variable (wrapper)

sessionMetaData

Format sessionMetaData.<Keys as they appear in JSON representation>

Examples

To get seeker question use the code

sessionMetaData.Guest.Question

To get seeker name use the code

sessionMetaData.Guest.Name

To get session SessionId

sessionMetaData.Session.SessionId

Script Method - sendReply('Message')

This method enables sending across message to seeker

Script Method - updateSessionMetaData('Key', 'Value')

This method allows updating of session meta data

Script Method - createContent('ContentType', 'Publish':true|false, {'Content-Properties-As-JSON'})

This method allows creating of a content item in Orchard

Example:

createContent('EvalRegistration', true, {"DisplayText": "Eval: Form Script, jdoe@domain", "EvalRegistration": {},"TitlePart": {"Title": "Eval: John Doe, jdoe@domain"}})

To get JSON properties structure/format, access an existing record of corresponding 'Content Type' as JSON

From JSON remove fields:

  1. ContentItemId
  2. ContentItemVersionId
  3. ContentType
  4. Latest
  5. Published
  6. ModifiedUtc
  7. PublishedUtc
  8. CreatedUtc
  9. Owner
  10. Author

Content data fields will remain, will appear as follows

{  
  "DisplayText": "Eval: Form Script - Vivek, vgarg@outlook.com",
  "EvalRegistration": {},
  "TitlePart": {
    "Title": "Eval: VG TEST - VG, vgarg@outlook.com"
  }
}

Script Method - getTickets('seekerEmailAddress')

To get list of tickets, script call will appear as follows:

//Make API call to get tickets
var tickets = getTickets("${Guest.Email}");

In response method will return list of tickets JSON

[
  {
    "SeekerEmailAddress": "webvisitor@example.com",
    "AgentEmailAddress": null,
    "Question": "Employee payroll application server is down.",
    "ChatTranscript": null,
    "Id": "f12ca184735123002728660c4cf6a7ef",
    "Number": "INC0007001",
    "URL": "https://dev82149.service-now.com/nav_to.do?url=incident.do?sys_id=f12ca184735123002728660c4cf6a7ef",
    "UpdatedOn": "2023-06-07 12:24:46",
    "TicketState": "New"
  },
  {
    "SeekerEmailAddress": "webvisitor@example.com",
    "AgentEmailAddress": null,
    "Question": "Performance problems with wifi",
    "ChatTranscript": null,
    "Id": "78271e1347c12200e0ef563dbb9a7109",
    "Number": "INC0000057",
    "URL": "https://dev82149.service-now.com/nav_to.do?url=incident.do?sys_id=78271e1347c12200e0ef563dbb9a7109",
    "UpdatedOn": "2023-05-29 13:20:52",
    "TicketState": "New"
  },
  {
    "SeekerEmailAddress": "webvisitor@example.com",
    "AgentEmailAddress": null,
    "Question": "Request for a Blackberry",
    "ChatTranscript": null,
    "Id": "b067a86a2f87a110ee31f64ef699b61d",
    "Number": "INC0010002",
    "URL": "https://dev82149.service-now.com/nav_to.do?url=incident.do?sys_id=b067a86a2f87a110ee31f64ef699b61d",
    "UpdatedOn": "2023-05-29 13:18:44",
    "TicketState": "In-Progress"
  }
]

JSON response can be converted into an array

var ticketsArray = JSON.parse(tickets);

//Array allows you to get count and loop through ticket records etc...
sendReply("Count of tickets: " + ticketsArray.length);

Script Method - createTicket('ticketJSON')

To create a ticket, in script create a ticket object as follows:

//Create ticket object
var ticket = {
    state: '0',
    short_description: 'Short Question',
    description: 'Full Question',
    caller: 'webvisitor@example.com',
    assigned_to: 'admin@example.com',
    work_notes: 'chat transcript'
};

//Convert object to JSON
var ticketJSON = JSON.stringify(ticket);

//Make API call to create ticket
var result = createTicket(ticketJSON);

createTicket call will return JSON response

{
  "SeekerEmailAddress": null,
  "AgentEmailAddress": null,
  "Question": null,
  "ChatTranscript": null,
  "Id": "8a0890212f572510ee31f64ef699b69d",
  "Number": "INC0010004",
  "URL": "https://dev82149.service-now.com/nav_to.do?url=incident.do?sys_id=8a0890212f572510ee31f64ef699b69d",
  "UpdatedOn": null
}

Script Method - updateTicket('ticketJSON', ticketId)

To update ticket, in script create a ticket object with values to update, follows:

//Create ticket object
var ticket = {
    short_description: 'Updated Short Question',
    description: 'Updated Full Question',
    work_notes: 'Updated chat transcript'
};

//Convert object to JSON
var ticketJSON = JSON.stringify(ticket);

//Make API call to create ticket
var result = updateTicket(ticketJSON, <tickedId>);

updateTicket call will return JSON response

{
  "SeekerEmailAddress": null,
  "AgentEmailAddress": null,
  "Question": null,
  "ChatTranscript": null,
  "Id": "8a0890212f572510ee31f64ef699b69d",
  "Number": "INC0010004",
  "URL": "https://dev82149.service-now.com/nav_to.do?url=incident.do?sys_id=8a0890212f572510ee31f64ef699b69d",
  "UpdatedOn": null
}

Script Editor

To build the scripts for Script Choice Module use the link https://playcode.io/970930

As changes are made to the script, result and errors are available in real time for review:

In case of an issue, error is shown in real time:

Post testing script can be used in ChimeV5 Script Pipeline Module

pipeline, javascript, Ticketing
vgarg