2017-02-08 11 views
4

Gefunden in Dienstbeispielen, ein funktionierendes Konversations-Skript. Danke nochmal an @Taj!IBM Watson Unity 3D-SDK-Erhaltungsdienst (fast funktionierend!)

Ich fühle mich wie ich bin sehr sehr nah, um es zur Arbeit zu bringen. Ich habe dasselbe mit Raspberry Pi mit TJBot gemacht, also habe ich alle Accounts, und ich habe alle Zugangsdaten korrekt verknüpft, einschließlich der Arbeitsplatz-ID von Conversation Tooling. Ich verwende Unity 3D 5.5.1f1 und das neueste SDK, das vor 13 Tagen aktualisiert wurde.

ich kopiert und eingefügt den Beispielcode für ein Gespräch auf SDK Github-Seite in eine brandneue C# Datei:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using IBM.Watson.DeveloperCloud.Services.Conversation.v1; 

public class test : MonoBehaviour { 
    private Conversation m_Conversation = new Conversation(); 
    private string m_WorkspaceID = "my ID on the conversation tooling site"; 
    private string m_Input = "Hi Alex"; 
    // Use this for initialization 
    void Start() { 
     Debug.Log("User: " + m_Input); 
     m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input); 
    } 

    // Update is called once per frame 
    void Update() { 

    } 

    void OnMessage(MessageResponse resp, string customData) 
    { 
     //Parsing resp here 
     //foreach (Intent mi in resp.intents) 
     //Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence); 
     //resp.output.text causes an error 
    } 
} 

Im Prozess herauszufinden, erkannte ich die onMessage Funktion einen Parameter (string fehlt custom), Fügte ich mit Hilfe meiner Freunde hinzu. II

Frage Teil:

Dank Taj für einhändig alle meine Fragen beantwortet! Dies hilft mir, den Kern meines Problems zu finden und hier ist es. Ich habe den obigen Code aktualisiert, um zu reflektieren, was ich in meiner Implementierung des Konversationsdienstes habe, basierend auf dem Beispielcodeblock, der auf der Github-Seite von IBM bereitgestellt wird. https://github.com/watson-developer-cloud/unity-sdk#conversation

Und das ist, was die Nachrichtenfunktion wie in Watson sieht/Scripts/Services/conversation.cs Datei:

/// <summary> 
/// Message the specified workspaceId, input and callback. 
/// </summary> 
/// <param name="workspaceID">Workspace identifier.</param> 
/// <param name="input">Input.</param> 
/// <param name="callback">Callback.</param> 
/// <param name="customData">Custom data.</param> 
public bool Message(OnMessage callback, string workspaceID, string input, string customData = default(string)) 
{ 
    if (string.IsNullOrEmpty(workspaceID)) 
    throw new ArgumentNullException("workspaceId"); 
    if (string.IsNullOrEmpty(input)) 
    throw new ArgumentNullException("input"); 
    if (callback == null) 
    throw new ArgumentNullException("callback"); 

    RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_MESSAGE); 
    if (connector == null) 
    return false; 

    string reqJson = "{{\"input\": {{\"text\": \"{0}\"}}}}"; 
    string reqString = string.Format(reqJson, input); 

    MessageReq req = new MessageReq(); 
    req.Callback = callback; 
    req.Headers["Content-Type"] = "application/json"; 
    req.Headers["Accept"] = "application/json"; 
    req.Parameters["version"] = Version.VERSION; 
    req.Function = "/" + workspaceID + "/message"; 
    req.Data = customData; 
    req.Send = Encoding.UTF8.GetBytes(reqString); 
    req.OnResponse = MessageResp; 

    return connector.Send(req); 
} 

Als ich anrief und es kehrte wahr, aber es ist nichts passiert danach, kein Rückruf = /.

Vielen Dank für irgendwelche Tipps! Bitte helfen Sie!

Antwort

8

Die Antwortstrings sind ein Array im resp-Objekt.

void OnMessage(MessageResponse resp, string customData) 
{ 
    foreach (Intent mi in resp.intents) 
    Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence); 

    if (resp.output != null && resp.output.text.Length > 0) 
    foreach (string txt in resp.output.text) 
     Debug.Log("Output: " + txt); 
} 
+0

Hallo Taj, danke für deine Antwort, ich gebe es jetzt eine Chance. – Ghettokon

+0

Hallo Taj, weißt du, was ist der "String customData"? beide im Parameter Message und OnMessage? – Ghettokon

+0

Die benutzerdefinierten Daten sind eine optionale Zeichenfolge, die Sie mit der Antwort senden können. Es tut nichts wirklich, es sei denn, es gibt etwas, das Sie in der Anfrage senden möchten. Zum Beispiel könnten Sie sagen: 'm_Conversation.Message (OnMessage, m_WorkspaceID, m_Input, "dies ist mein String");' und in der Callback 'customData' wird' "Das ist mein String" '. – taj

Verwandte Themen