Skip to content

Commit 3836213

Browse files
committed
自己CleverBot
1 parent b3110f8 commit 3836213

File tree

11 files changed

+195
-0
lines changed

11 files changed

+195
-0
lines changed

SapiClient/Assets/ChatRobot/Cleverbot.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using UnityEngine;
2+
3+
namespace Assets.ChatRobot.Cleverbot
4+
{
5+
public class CleverBot : WebChatBot
6+
{
7+
public override string Url
8+
{
9+
get { return "https://www.cleverbot.com/getreply"; }
10+
}
11+
12+
public override string Key
13+
{
14+
get { return "CC2me_S7B1Qh1diYLI9CJBZxXgQ"; }
15+
}
16+
17+
public string Cs { get; set; }
18+
19+
protected override WWW ProcessWWW(string req)
20+
{
21+
var url = string.Format("{0}?key={1}&input={2}&cs={3}", Url, Key, req, Cs);
22+
var www = new WWW(url);
23+
return www;
24+
}
25+
}
26+
27+
internal class CleverBotResponseBase : IWebChatBotResponseBase
28+
{
29+
public string cs;
30+
public int interaction_count;
31+
public string input;
32+
public string output;
33+
public string conversation_id;
34+
35+
public string Text
36+
{
37+
get { return output; }
38+
}
39+
}
40+
}

SapiClient/Assets/ChatRobot/Cleverbot/CleverBot.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
16.9 KB
Binary file not shown.

SapiClient/Assets/ChatRobot/Cleverbot/CleverBot.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using UnityEngine;
2+
3+
namespace Assets.ChatRobot.Cleverbot
4+
{
5+
public class CleverBotManager : WebChatBotManager<CleverBot>
6+
{
7+
protected override void OnGetResponse(string res)
8+
{
9+
var response = JsonUtility.FromJson<CleverBotResponseBase>(res);
10+
Speecher.Speech(response.Text);
11+
ChatBot.Cs = response.cs;
12+
}
13+
}
14+
}

SapiClient/Assets/ChatRobot/Cleverbot/CleverBotManager.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Collections;
2+
using UnityEngine;
3+
4+
namespace Assets.ChatRobot
5+
{
6+
public delegate void WebChatResponse(string res);
7+
8+
public abstract class WebChatBot : MonoBehaviour
9+
{
10+
public abstract string Url { get; }
11+
public abstract string Key { get; }
12+
13+
public void Require(string req)
14+
{
15+
StartCoroutine(_Require(req));
16+
}
17+
18+
public WebChatResponse OnGetResponse;
19+
20+
private IEnumerator _Require(string req)
21+
{
22+
var www = ProcessWWW(req);
23+
yield return www;
24+
if (www.error != null)
25+
{
26+
Debug.Log(www.error);
27+
yield break;
28+
}
29+
var text = www.text;
30+
Debug.Log(text);
31+
if (OnGetResponse != null)
32+
OnGetResponse.Invoke(text);
33+
}
34+
35+
protected abstract WWW ProcessWWW(string req);
36+
}
37+
38+
public interface IWebChatBotResponseBase
39+
{
40+
string Text { get; }
41+
}
42+
}

SapiClient/Assets/ChatRobot/WebChatBot.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using UnityEngine;
2+
3+
namespace Assets.ChatRobot
4+
{
5+
public abstract class WebChatBotManager<T> : SpeecherManager where T : WebChatBot
6+
{
7+
private string m_req = "";
8+
protected T ChatBot;
9+
10+
protected override void Awake()
11+
{
12+
base.Awake();
13+
ChatBot = GetComponent<T>();
14+
Speecher.OnGetRecognize = ChatBot.Require;
15+
ChatBot.OnGetResponse = OnGetResponse;
16+
}
17+
18+
protected abstract void OnGetResponse(string res);
19+
20+
public override void OnGUI()
21+
{
22+
base.OnGUI();
23+
24+
m_req = GUILayout.TextField(m_req);
25+
if (GUILayout.Button("ChatBot"))
26+
{
27+
if (!string.IsNullOrEmpty(m_req))
28+
{
29+
ChatBot.Require(m_req);
30+
}
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)