Skip to content

Commit a2b7941

Browse files
authored
Merge pull request #451 from eagostinelli/develop
Update README.md - update how to send a message to Watson Assistant
2 parents faa1408 + 62fc92c commit a2b7941

File tree

1 file changed

+63
-14
lines changed

1 file changed

+63
-14
lines changed

Scripts/Services/Assistant/v1/README.md

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,9 @@ You complete these steps to implement your application:
1111

1212
### Message
1313
Send a message to the Assistant instance
14-
```cs
15-
// Send a simple message using a string
16-
private void Message()
17-
{
18-
if (!_assistant.Message(OnMessage, OnFail, <workspace-id>, <input-string>))
19-
Log.Debug("ExampleAssistant.Message()", "Failed to message!");
20-
}
2114

22-
private void OnMessage(object resp, Dictionary<string, object> customData)
23-
{
24-
Log.Debug("ExampleAssistant.OnMessage()", "Assistant: Message Response: {0}", customData["json"].ToString());
25-
}
26-
```
15+
- Send a message using a MessageRequest object
2716
```cs
28-
// Send a message using a MessageRequest object
2917
private void Message()
3018
{
3119
MessageRequest messageRequest = new MessageRequest()
@@ -45,8 +33,10 @@ private void OnMessage(object resp, Dictionary<string, object> customData)
4533
Log.Debug("ExampleAssistant.OnMessage()", "Assistant: Message Response: {0}", customData["json"].ToString());
4634
}
4735
```
36+
37+
38+
- Send a message perserving conversation context
4839
```cs
49-
// Send a message perserving conversation context
5040
Dictionary<string, object> _context; // context to persist
5141
5242
// Initiate a conversation
@@ -91,4 +81,63 @@ private void OnMessage1(object resp, Dictionary<string, object> customData)
9181
}
9282
```
9383

84+
85+
- Send a message perserving conversation context - Extract code from [ExampleAssistant.cs](https://github.com/watson-developer-cloud/unity-sdk/blob/develop/Examples/ServiceExamples/Scripts/ExampleAssistant.cs)
86+
```cs
87+
88+
private void Message()
89+
{
90+
MessageRequest messageRequest = new MessageRequest()
91+
{
92+
input = new Dictionary<string, object>()
93+
{
94+
{ "text", <input-string> }
95+
}
96+
};
97+
98+
if (!_assistant.Message(OnMessage, OnFail, <workspace-id>, messageRequest))
99+
Log.Debug("ExampleAssistant.Message()", "Failed to message!");
100+
}
101+
102+
private void OnMessage(object response, Dictionary<string, object> customData)
103+
{
104+
Log.Debug("ExampleAssistant.OnMessage()", "Response: {0}", customData["json"].ToString());
105+
106+
// Convert resp to fsdata
107+
fsData fsdata = null;
108+
fsResult r = _serializer.TrySerialize(response.GetType(), response, out fsdata);
109+
if (!r.Succeeded)
110+
throw new WatsonException(r.FormattedMessages);
111+
112+
// Convert fsdata to MessageResponse
113+
MessageResponse messageResponse = new MessageResponse();
114+
object obj = messageResponse;
115+
r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
116+
if (!r.Succeeded)
117+
throw new WatsonException(r.FormattedMessages);
118+
119+
// Set context for next round of messaging
120+
object _tempContext = null;
121+
(response as Dictionary<string, object>).TryGetValue("context", out _tempContext);
122+
123+
if (_tempContext != null)
124+
_context = _tempContext as Dictionary<string, object>;
125+
else
126+
Log.Debug("ExampleAssistant.OnMessage()", "Failed to get context");
127+
128+
// Get intent
129+
object tempIntentsObj = null;
130+
(response as Dictionary<string, object>).TryGetValue("intents", out tempIntentsObj);
131+
object tempIntentObj = (tempIntentsObj as List<object>)[0];
132+
object tempIntent = null;
133+
(tempIntentObj as Dictionary<string, object>).TryGetValue("intent", out tempIntent);
134+
string intent = tempIntent.ToString();
135+
136+
Log.Debug("ExampleAssistant.OnMessage()", "intent: {0}", intent);
137+
138+
_messageTested = true;
139+
}
140+
141+
```
142+
94143
[assistant]: https://console.bluemix.net/docs/services/assistant/index.html

0 commit comments

Comments
 (0)