Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
- using IBM.Watson.DeveloperCloud.Utilities;
- using IBM.Watson.DeveloperCloud.Logging;
- using System.Collections;
- using FullSerializer;
- using System.Collections.Generic;
- using IBM.Watson.DeveloperCloud.Connection;
- using UnityEngine.UI;
- using System.Timers;
- public class ConversationDialog : MonoBehaviour
- {
- // Configurable Text
- public Text Response;
- private TextToSpeechService textToSpeechService;
- // Private variables for internal use only
- private string _username = "fb5185e8-f374-48aa-82dc-170bc48e6e44";
- private string _password = "actAdS7O2vMk";
- private string _url = "https://gateway.watsonplatform.net/assistant/api";
- private string _workspaceId = "31495ba4-7faa-477c-886a-2f070d079c65";
- private Conversation _conversation;
- private string _conversationVersionDate = "2017-05-26";
- private fsSerializer _serializer = new fsSerializer();
- private Dictionary<string, object> _context = null;
- void Start()
- {
- LogSystem.InstallDefaultReactors();
- // Create credential and instantiate service
- Credentials credentials = new Credentials(_username, _password, _url);
- _conversation = new Conversation(credentials);
- _conversation.VersionDate = _conversationVersionDate;
- textToSpeechService = TextToSpeechService.FindObjectOfType<TextToSpeechService>();
- }
- public void SendMessageRequest(string message)
- {
- MessageRequest messageRequest = new MessageRequest()
- {
- input = new Dictionary<string, object>()
- {
- {"text", message}
- },
- context = _context
- };
- if (!_conversation.Message(OnMessage, OnFail, _workspaceId, messageRequest))
- {
- Log.Debug("SendMessageRequest()", "Error al enviar el mensaje");
- }
- }
- private void OnMessage(object resp, Dictionary<string, object> customData)
- {
- Log.Debug("ExampleConversation.OnMessage()", "Conversation: Message Response: {0}", customData["json"].ToString());
- // Convert resp to fsdata
- fsData fsdata = null;
- fsResult r = _serializer.TrySerialize(resp.GetType(), resp, 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;
- (resp as Dictionary<string, object>).TryGetValue("context", out _tempContext);
- if (_tempContext != null)
- _context = _tempContext as Dictionary<string, object>;
- else
- Log.Debug("ExampleConversation.OnMessage()", "Failed to get context");
- if (resp != null && messageResponse.intents.Length > 0)
- {
- Response.text = string.Join(", ", messageResponse.output.text);
- string send = Response.text;
- textToSpeechService.UpdateTestString(send);
- HandleIntents(messageResponse.intents[0]);
- }
- }
- private void HandleIntents(RuntimeIntent runTimeIntent)
- {
- switch (runTimeIntent.intent)
- {
- case "pulmones":
- break;
- case "alveolos":
- break;
- case "bronquios":
- break;
- case "bronquiolos":
- break;
- case "cartilago":
- break;
- case "glandula":
- break;
- case "hueso":
- break;
- default:
- break;
- }
- }
- private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
- {
- Log.Error("ExampleConversation.OnFail()", "Error received: {0}", error.ToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement