Skip to main content

Response generation

Synchronous call

During a synchronous call, the bot cannot perform other reactions or respond to user requests until it gets a response from the knowledge base.

Within chat

state: Answer
intent!: /question
scriptEs6:
try {
// If there is no chat yet, the bot creates a new chat in the knowledge base
$client.chat = $client.chat || await $rag.chat.create("MyKnowledgeHub");
// The bot gets response from the knowledge base
var chatResponse = await $rag.chat.processQuery(
"MyKnowledgeHub", $client.chat.id, $request.query
)
if (chatResponse.status === "FINISHED") {
// Send response to the user
$reactions.answer(chatResponse.response);
} else {
// Error message if the status is not FINISHED
$reactions.answer("Sorry, something went wrong. Status: " + chatResponse.status);
}
} catch (error) {
// Error message if the bot failed to send the request
$reactions.answer("An error occurred: " + error);
}

In this example, the bot:

  1. If there is no chat yet, creates a new chat using $rag.chat.create.

  2. Gets the response using the $rag.chat.processQuery method. The bot calls the method synchronously using await.

    caution

    If the knowledge base generates a response for more than 25 seconds, then the bot will not receive it. In this case, call the method asynchronously.

  3. Sends a response or error message.

Single request

state: Answer
intent!: /question
scriptEs6:
try {
// The bot gets the response from the knowledge base
var ragResponse = await $rag.query.generateAnswer("MyKnowledgeHub", $request.query);
if (ragResponse.status === "FINISHED") {
// Send the response to the user
$reactions.answer(ragResponse.response);
} else {
// Error message if the status is not FINISHED
$reactions.answer("Sorry, something went wrong. Status: " + ragResponse.status);
}
} catch (error) {
// Error message if the bot failed to send the request
$reactions.answer("An error occurred: " + error);
}

In this example, the bot:

  1. Gets the response using the $rag.query.generateAnswer method. The bot calls the method synchronously using await.

    caution

    If the knowledge base generates a response for more than 25 seconds, then the bot will not receive it. In this case, call the method asynchronously.

  2. Sends a response or error message.

Asynchronous call

During an asynchronous call, the bot can continue to respond to user requests even if it has not yet received a response from the knowledge base.

Within chat

require: knowledgeHub.js
name = knowledgeHub
type = scriptEs6

theme: /

state: Answer
intent!: /question
scriptEs6:
knowledgeHub.sendRequest("MyKnowledgeHub", $request.query);

# If the user asks a question while waiting, the bot will be able to answer it instantly
state: Waiting
q!: No answer yet?
a: Just one more minute…

In this example, the bot:

  1. If there is no chat yet, creates a new chat using $rag.chat.create.
  2. Gets the response using the $rag.chat.processQuery method. The method is called asynchronously, inside the asynchronous sendRequest function.
  3. Sends a response using $conversationApi.sendTextToClient.
Restrictions
  • This script is not supported in the phone channel, voice assistants, and Webim (Custom Channel API).
  • If the knowledge base does not generate an answer within 2 minutes, an error will occur.

Single request

require: knowledgeHub.js
name = knowledgeHub
type = scriptEs6

theme: /

state: Answer
intent!: /question
scriptEs6:
knowledgeHub.sendRequest("MyKnowledgeHub", $request.query);

# If the user asks a question while waiting, the bot will be able to answer it instantly
state: Waiting
q!: No answer yet?
a: Just one more minute…

In this example, the bot:

  1. Gets the response using the $rag.query.generateAnswer method. The method is called asynchronously, inside the asynchronous sendRequest function.

  2. Sends a response using $conversationApi.sendTextToClient.

Restrictions
  • This script is not supported in the phone channel, voice assistants, and Webim (Custom Channel API).
  • If the knowledge base does not generate an answer within 2 minutes, an error will occur.