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.

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();
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.