$response
This object is used for filling in system responses.
$response.replies
is a list of responses made while processing chatbot reactions. It is an array containing strictly typed elements and utilized in order to transfer chatbot replies to platform-specific chat adapters, such as Telegram.
replies
replies
supports objects of the following types:
-
text
is a regular text response. Every element of this type is printed out as a separate message. Thetts
parameter should contain the same text with special markup rendered by speech synthesis systems. Themarkup
parameter determines text formatting style.{
"type": "text",
"text": "....",
"tts": "....",
"markup": "html|markdown|plain"
} -
buttons
— renders a regular button.{
"type": "buttons",
"buttons": [
{
"text": "button" // All the remaining attributes will be directly passed on to the messenger.
}
]
} -
inlineButtons
— renders an inline button. If aurl
attribute is present, then the specified link will be opened upon pressing the button.{
"type": "inlineButtons",
"buttons": [
{
"text": "button",
"url": "https://example.com"
}
]
} -
image
— renders an image.{
"type": "image",
"imageUrl": "http://..."
} -
raw
— is used for passing on methods specific to a particular channel. The mandatorybody
parameter contains the response body transferred into the chat system. Parameters used for user identification are set up automatically.{
"type": "raw",
"body": {...},
"method": "sendMessage"
} -
switch
— switches the dialog from the bot to a customer service agent.{
"type": "switch",
"firstMessage": "...",
"closeChatPhrases": ["..."],
"ignoreOffline":true|false,
"oneTimeMessage": true|false
} -
location
— prints out geographic coordinates. Its attributes arelat
andlon
.{
"type": "location",
"lat": 59.934280,
"lon": 30.335099
} -
timeout
— makes a transition to a specified state if the user gives no response.{
"type": "timeout",
"interval": 10,
"targetState": "/timedout"
} -
dtmf
— is a DTMF message signal (containing figures and characters typed on a touch-tone keypad). Intended for calls only.$response.replies.push({type:"dtmf"});
-
hangup
— cuts off the call. Intended for calls only.$response.replies.push({type:"hangup"});
How to use
-
Image rendering.
script:
$response.replies.push({
type: "image",
imageUrl: "https://testimageurl.jpg",
text: "Image" // This field is optional.
}); -
GIF rendering.
$response.replies = $response.replies || [];
$response.replies.push({
"type": "raw",
"body":
{
"animation": "https://www.catgifpage.com/gifs/318.gif"
},
"method": "sendAnimation"
}); -
Switching to a customer service agent.
$response.replies = $response.replies || [];
$response.replies .push({
type:"switch",
phoneNumber:74155,
closeChatPhrases: catchAll.closeChatPhrases,
firstMessage: $client.history,
destination: catchAll.operatorGroup,
}); -
Changing the reply in the post-process if the bot tries to give the same reply for the third time in a row.
init:
bind("postProcess", function(){
var $session = $jsapi.context().session;
var $response = $jsapi.context().response;
var answer = $response.replies ? $response.replies.reduce (function (answers, current) {
answers += " " + current.text;
return answers;
}, "") : "";
if ($session.lastAnswer && (answer == $session.lastAnswer)) {
$session.answerRepetition = $session.answerRepetition || 0;
$session.answerRepetition += 1;
} else {
$session.answerRepetition = 0;
}
if ($session.answerRepetition == 2) {
$response.replies = [{
"type": "text",
"text": "We are going in circles. Could you ask me something else?"
}];
}
$session.lastAnswer = answer;
});