User functions

The functions in the bot.user namespace are used to get and set user data, subscribe users to broadcasts, and schedule notifications.

getContextData()

Returns a JavaScript object of all custom data stored for the user.

//Retrieve the user’s email address
const contextData = bot.user.getContextData();

bot.channel.sendTextMessage(`Is your email address: ${contextData.email}?`, params);
done();

What's the response?

Your response data depends on what context data you have stored for your users.

saveContext​Data(data)

Store custom user data as key-value pairs. Storing a value for a key that already exists will overwrite the existing value.

Context data can only be used by scripts. If you want to make user information available to be used in chatbot content placeholder chips, use a variable.

Parameter Type Required Description
data Object Required An object containing the key-value pairs to store.
//Save user input into context data before starting the next passage.
const userInput = params.text; 
// params.text contains the value of user text input, if it is set if (userInput) { await bot.user.saveContextData({ email: userInput }); // save the user input bot.flow.startPassage('123456'); // next passage in the flow } else { bot.flow.startPassage('abcdef'); // re-enter email passage } done();

clearContextData()

Clear all stored custom user data for this user.

bot.user.clearContextData();
done();

getFlowState()

Returns a JavaScript object describing:

  • The chatbot's current conversation and passage.
  • The context that any question sent by the user will be first evaluated in.
  • Whether the chatbot is between messages (true) or has reached the end of a flow.
const flowState = bot.user.getFlowState();

if (!flowState) {
  done();
  return;
}

bot.channel.sendTextMessage(`Your last message was at  ${flowState.lastMessageTimestamp}`, params);
done();

Example response:

{
  "conversationId": "XXXXX",
  "passageId": "YYYYY",
  "questionContext": {
    "conversationId": "AAAAAA",
    "passageId": "BBBBBB"
  },
  "betweenMessages": true,
  "botData": {
    "NLP": null
  }
}

GetConversation​State()

Returns a JavaScript object describing the passage ID and conversation title of all conversations the user is currently engaged in, represented as a stack.

const conversationState = bot.user.getConversationState();

if (conversationState && conversationState.stack.length > 0) {
  const lastConversationId = conversationState.stack.pop();
  
  if (conversationState.state[lastConversationId] && conversationState.state[lastConversationId].passageId) {
    const lastPassageId = conversationState.state[lastConversationId].passageId;

    await bot.flow.switchConversation(lastConversationId);
    await bot.flow.startPassage(lastPassageId);
  }
}

done();

Example response:

{
  "state": {
    "XXXXX": {
      "passageId": "AAAAAA",
      "conversationTitle": "Conversation 1"
    },
    "YYYYY": {
      "passageId": "BBBBBB",
      "conversationTitle": "Conversation 2"
    }
  },
  "stack": ["XXXXX", "YYYYY"]
}

getQuestionState()

Returns a JavaScript object containing:

  • The question ID of each question that the user has triggered in the chatbot.
  • The number of times that question has been triggered.
  • The number of seconds until that question expires.
    Expired questions are removed from the user's Question State.
const questionState = bot.user.getQuestionState();

if (questionState && questionState.askedQuestions['catchall-question']) {
  bot.channel.sendTextMessage(`You triggered catch all ${questionState.askedQuestions['catchall-question'].count} times`, params);
}

done();

Example response:

{
  "askedQuestions": {
    "catchall-question": {
      "count": 2,
      "expiry": 1609910774.807
    },
    "XXXXXX": {
      "count": 1,
      "expiry": 1609910807.384
    },
    "YYYYYY": {
      "count": 1,
      "expiry": 1609910853.932
    }
  }
}

subscribe()

Subscribe the user to broadcasts.

bot.user.subscribe();
done();

unsubscribe()

Unsubscribe the user from broadcasts.

bot.user.unsubscribe();
done();

schedule​Notification​(notificationId, referenceDate)

Schedule a notification for the user.

Parameter Type Required Description
notificationId String Required The ID of the notification to schedule.
referenceDate Date Required The date and time from which to schedule the notification.
//Schedule a notification with the id of 123456
const appointmentDate = moment("2020-12-25T09:30:00").toDate();
bot.user.scheduleNotification('123456', appointmentDate); 
done();

You can find the ID of a notification by opening the event notification in the inGenious AI platform and copying the URL:

unschedule​Notification​(notificationId)

Remove a scheduled notification from the user.

Parameter Type Required Description
notificationId String Required The ID of the notification to remove.
//Unschedule the notification with the id 123456
bot.user.unscheduleNotification('123456’);
done();

You can find the ID of a notification by opening the event notification in the inGenious AI platform and copying the URL:

getShortLived​AccessToken(data)

Get an access token to call the chatbot’s webview endpoints from outside the chatbot. Access tokens are valid for 30 minutes. See Webviews for more information.

Parameter Type Required Description
data object Optional Any data you want to store against the access token.
//Generate an access token for another API to call the bot.
var token = bot.user.getShortLivedAccessToken();
done();

get​Constants()

Get the values of all constants in your chatbot. Constants are returned as a dictionary of the constant ID and corresponding value. Constant values are always a string.

//Get all constants in your chatbot
var botConstants = bot.user.getConstants();
done();

The constants are returned as a dictionary object:

//Response when two constants have been created
{
  OkIhAS7uWd0ror: "03 9568 4567", 
  gUZUxHM2Afct4c: "Example Company"
}
done();

Constant IDs can be copied from the menu next to the variable name in the Constants page.

clearAll​Variables()

This method is asynchronous.

Reset the values for all variables that have been set for this user. Variables are reset to their default values. Any variable that does not have a default value will be treated as blank.

//Clear all variables set for the user. This method is asynchronous.
await bot.user.clearAllVariables();
done();

get​Variables()

Get the values of all variables that have been assigned for this user. Only variables that have been set for this user or have a default value are returned. Variables are returned as a dictionary of the variable ID and corresponding value. Variable values are always a string.

//Get a dictionary of all variables assigned to the user
var uservariables = bot.user.getVariables();
done();

The variables are returned as a dictionary object:

//Response when two variables have been set.
{
  OkIhAS7uWd0ror: "Jimmy", 
  gUZUxHM2Afct4c: "37"
}
done();

Variable IDs can be copied from the menu next to the variable name in the Variables page.

set​Variables()

This method is asynchronous.

Set the value for one or more variables for a user. The variables and values to set must be provided as a dictionary of the variable ID and the value to set. Values are always strings. If one of the variables already has a value stored, it will be overwritten.

Variables must have already been created in the chatbot to be set by scripts. If you want to save user information without creating a chatbot variable, use the saveContextData() function.

Parameter Type Required Description
- object Required A dictionary of each variable ID and the value you want to store.
//Set the values of two variables.
await bot.user.setVariables({
  OkIhAS7uWd0ror: "Jimmy", 
  gUZUxHM2Afct4c: "37"
});
done();

Variable IDs can be copied from the menu next to the variable name in the Variables page.

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.