Advertisement
Guest User

Untitled

a guest
Oct 28th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.14 KB | None | 0 0
  1. using UnityEngine;
  2. using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
  3. using IBM.Watson.DeveloperCloud.Utilities;
  4. using IBM.Watson.DeveloperCloud.Logging;
  5. using System.Collections;
  6. using FullSerializer;
  7. using System.Collections.Generic;
  8. using IBM.Watson.DeveloperCloud.Connection;
  9. using UnityEngine.UI;
  10. using System.Timers;
  11.  
  12. public class ConversationDialog : MonoBehaviour
  13. {
  14.  
  15.     // Configurable Text
  16.     public Text Response;
  17.  
  18.     private TextToSpeechService textToSpeechService;
  19.  
  20.     // Private variables for internal use only
  21.     private string _username = "fb5185e8-f374-48aa-82dc-170bc48e6e44";
  22.     private string _password = "actAdS7O2vMk";
  23.     private string _url = "https://gateway.watsonplatform.net/assistant/api";
  24.     private string _workspaceId = "31495ba4-7faa-477c-886a-2f070d079c65";
  25.  
  26.     private Conversation _conversation;
  27.     private string _conversationVersionDate = "2017-05-26";
  28.  
  29.     private fsSerializer _serializer = new fsSerializer();
  30.     private Dictionary<string, object> _context = null;
  31.  
  32.     void Start()
  33.     {
  34.         LogSystem.InstallDefaultReactors();
  35.  
  36.         //  Create credential and instantiate service
  37.         Credentials credentials = new Credentials(_username, _password, _url);
  38.  
  39.         _conversation = new Conversation(credentials);
  40.         _conversation.VersionDate = _conversationVersionDate;
  41.  
  42.         textToSpeechService = TextToSpeechService.FindObjectOfType<TextToSpeechService>();
  43.     }
  44.  
  45.     public void SendMessageRequest(string message)
  46.     {
  47.         MessageRequest messageRequest = new MessageRequest()
  48.         {
  49.             input = new Dictionary<string, object>()
  50.             {
  51.                 {"text", message}
  52.             },
  53.             context = _context
  54.         };
  55.  
  56.         if (!_conversation.Message(OnMessage, OnFail, _workspaceId, messageRequest))
  57.         {
  58.             Log.Debug("SendMessageRequest()", "Error al enviar el mensaje");
  59.         }
  60.  
  61.     }
  62.  
  63.     private void OnMessage(object resp, Dictionary<string, object> customData)
  64.     {
  65.         Log.Debug("ExampleConversation.OnMessage()", "Conversation: Message Response: {0}", customData["json"].ToString());
  66.  
  67.         //  Convert resp to fsdata
  68.         fsData fsdata = null;
  69.         fsResult r = _serializer.TrySerialize(resp.GetType(), resp, out fsdata);
  70.         if (!r.Succeeded)
  71.             throw new WatsonException(r.FormattedMessages);
  72.  
  73.         //  Convert fsdata to MessageResponse
  74.         MessageResponse messageResponse = new MessageResponse();
  75.         object obj = messageResponse;
  76.         r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
  77.         if (!r.Succeeded)
  78.             throw new WatsonException(r.FormattedMessages);
  79.  
  80.         //  Set context for next round of messaging
  81.         object _tempContext = null;
  82.         (resp as Dictionary<string, object>).TryGetValue("context", out _tempContext);
  83.  
  84.         if (_tempContext != null)
  85.             _context = _tempContext as Dictionary<string, object>;
  86.         else
  87.             Log.Debug("ExampleConversation.OnMessage()", "Failed to get context");
  88.  
  89.         if (resp != null && messageResponse.intents.Length > 0)
  90.         {
  91.             Response.text = string.Join(", ", messageResponse.output.text);
  92.             string send = Response.text;
  93.             textToSpeechService.UpdateTestString(send);
  94.             HandleIntents(messageResponse.intents[0]);
  95.         }
  96.     }
  97.  
  98.     private void HandleIntents(RuntimeIntent runTimeIntent)
  99.     {
  100.         switch (runTimeIntent.intent)
  101.         {
  102.             case "pulmones":
  103.                 break;
  104.  
  105.             case "alveolos":
  106.                 break;
  107.  
  108.             case "bronquios":
  109.                 break;
  110.  
  111.             case "bronquiolos":
  112.                 break;
  113.  
  114.             case "cartilago":
  115.                 break;
  116.  
  117.             case "glandula":
  118.                 break;
  119.  
  120.             case "hueso":
  121.                 break;
  122.  
  123.             default:
  124.                 break;
  125.         }
  126.     }
  127.  
  128.     private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
  129.     {
  130.         Log.Error("ExampleConversation.OnFail()", "Error received: {0}", error.ToString());
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement