Skip to content

Update README.md - update how to send a message to Watson Assistant #451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 63 additions & 14 deletions Scripts/Services/Assistant/v1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,9 @@ You complete these steps to implement your application:

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

private void OnMessage(object resp, Dictionary<string, object> customData)
{
Log.Debug("ExampleAssistant.OnMessage()", "Assistant: Message Response: {0}", customData["json"].ToString());
}
```
- Send a message using a MessageRequest object
```cs
// Send a message using a MessageRequest object
private void Message()
{
MessageRequest messageRequest = new MessageRequest()
Expand All @@ -45,8 +33,10 @@ private void OnMessage(object resp, Dictionary<string, object> customData)
Log.Debug("ExampleAssistant.OnMessage()", "Assistant: Message Response: {0}", customData["json"].ToString());
}
```


- Send a message perserving conversation context
```cs
// Send a message perserving conversation context
Dictionary<string, object> _context; // context to persist

// Initiate a conversation
Expand Down Expand Up @@ -91,4 +81,63 @@ private void OnMessage1(object resp, Dictionary<string, object> customData)
}
```


- 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)
```cs

private void Message()
{
MessageRequest messageRequest = new MessageRequest()
{
input = new Dictionary<string, object>()
{
{ "text", <input-string> }
}
};

if (!_assistant.Message(OnMessage, OnFail, <workspace-id>, messageRequest))
Log.Debug("ExampleAssistant.Message()", "Failed to message!");
}

private void OnMessage(object response, Dictionary<string, object> customData)
{
Log.Debug("ExampleAssistant.OnMessage()", "Response: {0}", customData["json"].ToString());

// Convert resp to fsdata
fsData fsdata = null;
fsResult r = _serializer.TrySerialize(response.GetType(), response, out fsdata);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);

// Convert fsdata to MessageResponse
MessageResponse messageResponse = new MessageResponse();
object obj = messageResponse;
r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);

// Set context for next round of messaging
object _tempContext = null;
(response as Dictionary<string, object>).TryGetValue("context", out _tempContext);

if (_tempContext != null)
_context = _tempContext as Dictionary<string, object>;
else
Log.Debug("ExampleAssistant.OnMessage()", "Failed to get context");

// Get intent
object tempIntentsObj = null;
(response as Dictionary<string, object>).TryGetValue("intents", out tempIntentsObj);
object tempIntentObj = (tempIntentsObj as List<object>)[0];
object tempIntent = null;
(tempIntentObj as Dictionary<string, object>).TryGetValue("intent", out tempIntent);
string intent = tempIntent.ToString();

Log.Debug("ExampleAssistant.OnMessage()", "intent: {0}", intent);

_messageTested = true;
}

```

[assistant]: https://console.bluemix.net/docs/services/assistant/index.html