繁体   English   中英

我怎样才能让我的聊天机器人先说话?

[英]How can I make my chatbot talk first?

我使用 Unity3d 和 IBM watson-conversation 制作了一个聊天机器人。

它工作得很好,就像我的网络工作区一样。

在我的网络工作区中,聊天机器人通过识别“欢迎”来与我交谈。

但是在 unity 中,我看不到欢迎文本,所以我制作了一段假文本来开始对话,但我更改了一些进度,我想先让它说话。 我怎样才能让聊天机器人先说话?

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

class Watson : MonoBehaviour{    
static Credentials credentials;
static Conversation _conversation;
void Start()
{
    credentials = new Credentials("xx-xx-xx-xx-xx", "xx", "https://gateway.watsonplatform.net/conversation/api");
  //  credentials.Url = "";
    _conversation = new Conversation(credentials);

}
static Action<string, ManagerChat.Feel, bool> Act;
public static void GoMessage(string _str,Action<string, ManagerChat.Feel,bool> _act)
{

    if (!_conversation.Message(OnMessage, "xx-xx-xx-xx-xx", _str))
        Debug.Log("ExampleConversation Failed to message!");

    Act = _act;
}

static bool GetIntent(Dictionary<string, object> respDict)
{
    object intents;
    respDict.TryGetValue("intents", out intents);

    object intentString = new object();

    object confidenceString = new object();

    foreach (var intentObj in (intents as List<object>))
    {
        Dictionary<string, object> intentDict = intentObj as Dictionary<string, object>;

        intentDict.TryGetValue("intent", out intentString);


        intentDict.TryGetValue("confidence", out confidenceString);
    }
    string str = intentString as string;

    if (str == "6사용자_마무리")
        return true;

    return false;

}

static string GetOutput(Dictionary<string, object> respDict)
{
    object outputs;
    respDict.TryGetValue("output", out outputs);

    object output;
    (outputs as Dictionary<string, object>).TryGetValue("text", out output);

    string var = (output as List<object>)[0] as string;

    return var;
}

static ManagerChat.Feel GetEntities(Dictionary<string, object> respDict)
{
    object entities;
    respDict.TryGetValue("entities", out entities);

    List<object> entitieList = (entities as List<object>);

    if(entitieList.Count == 0)
    {
        return ManagerChat.Feel.Normal;
    }
    else
    {
        object entitie;
        (entitieList[0] as Dictionary<string, object>).TryGetValue("value", out entitie);
        ManagerChat.Feel feel = ManagerChat.Feel.NONE;

        string str = entitie as string;

        switch (str)
        {
            case "Happy":
                feel = ManagerChat.Feel.Happy;
                break;
            case "Expect":
                feel = ManagerChat.Feel.Expect;
                break;
            case "Born":
                feel = ManagerChat.Feel.Born;
                break;
            case "Sad":
                feel = ManagerChat.Feel.Sad;
                break;
            case "Surprise":
                feel = ManagerChat.Feel.Surprise;
                break;
            case "Normal":
                feel = ManagerChat.Feel.Normal;
                break;
            default:
                break;
        }

        return feel;
    }
}

static void OnMessage(object resp, string data)
{
    Dictionary<string, object> respDict = resp as Dictionary<string, object>;

  bool flag = (GetIntent(respDict));

    string output = (GetOutput(respDict));

   ManagerChat.Feel feel = GetEntities(respDict);
  //      Debug.Log(resp);
  //      Debug.Log(data);
        Act(output,feel, flag);
    }
}

我刚刚解决了这个 问题 现在,您可以将一个空字符串传递到 Conversation 服务以发起对话。

//  Test initate with empty string
if (!_conversation.Message(OnMessage, _workspaceId, ""))
    Log.Debug("ExampleConversation", "Failed to message!");

private void OnMessage(object resp, string data)
{
    Log.Debug("ExampleConversation", "Conversation: Message Response: {0}", data);
}

为什么不使用相同的方法发送假文本到对话中? 可能您的应用需要检查是否有新会话,发送假文本。 或者更好的是,您的应用程序只需发送您自己的欢迎信息,而无需进行对话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM