-
Notifications
You must be signed in to change notification settings - Fork 7
02 Manage your conversation
You can use SAP Conversational AI's API to bring your bot to life!
Once you have created your bot on the platform and built your conversation flow in the Build tab, you can use this SDK to make it interact with the world.
Jump at the end of this page if you're looking for more details on the Conversation returned by a call to the dialog method.
Start by instantiating either a Client or a Build object, as shown below:
require 'sapcai'
client = new Sapcai::Client.new('YOUR_TOKEN')
request = client.request
# ...is the same as...
request = Sapcai::Request.new('YOUR_TOKEN')
You can then interact with it using the dialog method, as follows:
response = build.dialog({type: "text", content: "YOUR_TEXT"}, "YOUR_CONVERSATION_ID")
# Do your code
Each time you want to start a conversation, simply call dialog with a new conversation_id parameter and the conversation will be created automatically.
The dialog method can take optional parameters, like memory, proxy and log_level. One or more option can be passed as parameters.
You can pass memory object to the method as follows:
response = build.dialog({type: "text", content: "YOUR_TEXT"}, "A_CONVERSATION_ID", nil, memory: {MEMORY_KEY: "MEMORY_VALUE"})
# Do your code
You can also pass log_level option as follows:
response = buil.dialog({type: "text", content: "YOUR_TEXT"}, "A_CONVERSATION_ID", nil, log_level: "LOG_LEVEL")
# Do your code
You can add a proxy option as follows:
response = build.dialog({type: "text", content: "YOUR_TEXT"}, "A_CONVERSATION_ID", nil, proxy: { host: '10.100.10.100', port: '8000' })
# Do your code
Each conversation has an field called 'memory' used to store the data extracted from the input it receives. For example, if your user starts the conversation by telling his name, and you need it later on in the conversation, you don't need to ask him again, it will be stored in the 'memory' object.
You can access the current state of the memory as follows:
response = build.dialog({type: "text", content: "YOUR_TEXT"}, "A_CONVERSATION_ID)
conversation = response.conversation
memory = conversation.memory
# Do your code
The response you receive after a call to the dialog method is an object composed of three parts:
- nlp, containing all the NLP information
- conversation, the text analysis of the input
- messages, containing the messages your bot can send at this stage of the conversation
The conversation object contains the following attributes:
Attributes | Type |
---|---|
id | String: the id of the conversation |
language | String: the current language of the conversation |
memory | Object: the current memory of the conversation |
skill | String: the current active skill |
skill_occurences | Number: the number of time the same skill was triggered |
The message object contains the following attributes:
Attributes | Type |
---|---|
type | String: the type of the message |
content | String |
For more information about message types and formats, see Bot Connector doc.
The NPL object contains the following attributes:
Attributes | Type |
---|---|
uuid | String: the uuid of the request |
source | String: the user input |
intents | Array[object]: all the matched intents |
act | String: the act of the processed sentence |
type | String: the type of the processed sentence |
sentiment | String: the sentiment of the processed sentence |
entities | Object[Key: String (Entity Name), Value: Entity]: the array of entities |
processing_language | String: the language used to process the input |
language | String: the language of the input |
version | String: the version of the json |
timestamp | String: the timestamp at the end of the processing |
status | String: the status of the response |
The build client provides several methods to manage you conversations. This API requires the bot version that should be targeted. If versioning is not enabled in the bot, v1
should be passed. Otherwise, see our versioning documentation for more information.
Parameters | Type |
---|---|
user | String: your user slug or id |
bot | String: your bot slug or id |
version | String: you bot's version; v1 if versioning not enabled |
conversationId | String: the conversation id |
This method returns a Conversation object.
Usage:
response = build.get_conversation("USER_SLUG", "BOT_SLUG", "VERSION_SLUG", "A_CONVERSATION_ID")
# Do your code
Parameters | Type |
---|---|
user | String: your user slug or id |
bot | String: your bot slug or id |
version | String: you bot's version; v1 if versioning not enabled |
conversationId | String: the conversation id |
opts | Hash: the conversation attributes |
The opts
hash can contain the following keys:
- memory
- skill_occurences
- language
This method returns a Conversation object.
Usage:
response = build.update_conversation("USER_SLUG", "BOT_SLUG", "VERSION_SLUG", "A_CONVERSATION_ID", { language: "en"})
# Do your code
Parameters | Type |
---|---|
user | String: your user slug or id |
bot | String: your bot slug or id |
version | String: you bot's version; v1 if versioning not enabled |
conversationId | String: the conversation id |
This method returns true if the conversation is properly deleted.
Usage:
response = build.delete_conversation("USER_SLUG", "BOT_SLUG", "VERSION_SLUG", "A_CONVERSATION_ID")
# Do your code