@@ -11,21 +11,9 @@ You complete these steps to implement your application:
11
11
12
12
### Message
13
13
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
- }
21
14
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
27
16
``` cs
28
- // Send a message using a MessageRequest object
29
17
private void Message ()
30
18
{
31
19
MessageRequest messageRequest = new MessageRequest ()
@@ -45,8 +33,10 @@ private void OnMessage(object resp, Dictionary<string, object> customData)
45
33
Log .Debug (" ExampleAssistant.OnMessage()" , " Assistant: Message Response: {0}" , customData [" json" ].ToString ());
46
34
}
47
35
```
36
+
37
+
38
+ - Send a message perserving conversation context
48
39
``` cs
49
- // Send a message perserving conversation context
50
40
Dictionary < string , object > _context ; // context to persist
51
41
52
42
// Initiate a conversation
@@ -91,4 +81,63 @@ private void OnMessage1(object resp, Dictionary<string, object> customData)
91
81
}
92
82
```
93
83
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
+
94
143
[ assistant ] : https://console.bluemix.net/docs/services/assistant/index.html
0 commit comments