|
16 | 16 | */ |
17 | 17 |
|
18 | 18 | using UnityEngine; |
19 | | -using System.Collections; |
20 | 19 | using IBM.Watson.DeveloperCloud.Services.Conversation.v1; |
21 | 20 | using IBM.Watson.DeveloperCloud.Utilities; |
22 | 21 | using IBM.Watson.DeveloperCloud.Logging; |
| 22 | +using System; |
23 | 23 |
|
24 | 24 | public class ExampleConversation : MonoBehaviour |
25 | 25 | { |
26 | 26 | private Conversation m_Conversation = new Conversation(); |
27 | 27 | private string m_WorkspaceID; |
28 | | - private string m_Input = "Can you unlock the door?"; |
| 28 | + private string m_ConversationID; |
| 29 | + private bool m_UseAlternateIntents = true; |
| 30 | + private string[] questionArray = { "can you turn up the AC", "can you turn on the wipers", "can you turn off the wipers", "can you turn down the ac", "can you unlock the door"}; |
29 | 31 |
|
30 | 32 | void Start () { |
31 | 33 | LogSystem.InstallDefaultReactors(); |
32 | 34 | m_WorkspaceID = Config.Instance.GetVariableValue("ConversationV1_ID"); |
33 | | - Debug.Log("User: " + m_Input); |
34 | 35 |
|
35 | | - // Message with input only |
36 | | - //m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input); |
| 36 | + Debug.Log("**********User: Hello!"); |
| 37 | + MessageWithOnlyInput("Hello!"); |
| 38 | + } |
37 | 39 |
|
38 | | - // Message by creating message request |
39 | | - //MessageRequest messageRequest = new MessageRequest(); |
40 | | - //messageRequest.inputText = m_Input; |
41 | | - //m_Conversation.Message(OnMessage, m_WorkspaceID, messageRequest); |
| 40 | + private void MessageWithOnlyInput(string input) |
| 41 | + { |
| 42 | + if (string.IsNullOrEmpty(input)) |
| 43 | + throw new ArgumentNullException("input"); |
| 44 | + |
| 45 | + m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, input); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + private void OnMessageWithOnlyInput(MessageResponse resp, string customData) |
| 50 | + { |
| 51 | + if (resp != null) |
| 52 | + { |
| 53 | + foreach (Intent mi in resp.intents) |
| 54 | + Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence); |
| 55 | + |
| 56 | + if (resp.output != null && resp.output.text.Length > 0) |
| 57 | + foreach (string txt in resp.output.text) |
| 58 | + Debug.Log("output: " + txt); |
| 59 | + |
| 60 | + m_ConversationID = resp.context.conversation_id; |
| 61 | + |
| 62 | + string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)]; |
| 63 | + Debug.Log(string.Format("**********User: {0}", questionStr)); |
| 64 | + |
| 65 | + MessageRequest messageRequest = new MessageRequest(); |
| 66 | + messageRequest.InputText = questionStr; |
| 67 | + messageRequest.alternate_intents = m_UseAlternateIntents; |
| 68 | + messageRequest.ContextData = resp.context; |
42 | 69 |
|
43 | | - // Message by passing input, alternate intents and conversationID |
44 | | - m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input, false, null); |
| 70 | + MessageWithFullMessageRequest(messageRequest); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + Debug.Log("Failed to invoke Message();"); |
| 75 | + } |
45 | 76 | } |
46 | 77 |
|
47 | | - void OnMessage (MessageResponse resp, string customData) |
| 78 | + private void MessageWithFullMessageRequest(MessageRequest messageRequest) |
48 | 79 | { |
49 | | - if(resp != null) |
50 | | - { |
51 | | - foreach(Intent mi in resp.intents) |
52 | | - Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence); |
53 | | - |
54 | | - if(resp.output != null && resp.output.text.Length > 0) |
55 | | - foreach(string txt in resp.output.text) |
56 | | - Debug.Log("output: " + txt); |
57 | | - } |
58 | | - else |
59 | | - { |
60 | | - Debug.Log("Failed to invoke Message();"); |
61 | | - } |
| 80 | + if (messageRequest == null) |
| 81 | + throw new ArgumentNullException("messageRequest"); |
| 82 | + m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, messageRequest); |
62 | 83 | } |
63 | 84 | } |
0 commit comments