C#版的有道智云对话接口

public class YoudaoTalk

{

public static string appKey = "您的应用ID";//"您的应用ID";

public static string appSecret = "您的应用密钥";//"您的应用密钥";

//应用名称 一对一对话

//已选服务 小P老师, AI口语老师, 个性化语音定制, 语音合成, 短语音识别, 实时语音识别, 语音评测, 实时语音评测

public static string talk_generate_topic(string topic)

{

Dictionary<String, String> dic = new Dictionary<String, String>();

string url = "http://openapi.youdao.com/ai_dialog/generate_topic";

string q = "topics";

string salt = DateTime.Now.Millisecond.ToString();

TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));

long millis = (long)ts.TotalMilliseconds;

string curtime = Convert.ToString(millis / 1000);

dic.Add("curtime", curtime);

string signStr = appKey + Truncate(q) + salt + curtime + appSecret; ;

string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());

dic.Add("appKey", appKey);

dic.Add("salt", salt);

dic.Add("signType", "v3");

dic.Add("sign", sign);

dic.Add("topic", topic);

string result = PostJson(url, Newtonsoft.Json.JsonConvert.SerializeObject(dic));

return result;

}

public static string talk_generate_recommendation(string taskId, generate_topic_scene scene, string userLevel, List<Dictionary<string, string>> history, string indexArr ,string count)

{

Dictionary<String, object> dic = new Dictionary<String, object>();

string url = "http://openapi.youdao.com/ai_dialog/generate_recommendation";

string q = "topics";

string salt = DateTime.Now.Millisecond.ToString();

TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));

long millis = (long)ts.TotalMilliseconds;

string curtime = Convert.ToString(millis / 1000);

dic.Add("curtime", curtime);

string signStr = appKey + Truncate(q) + salt + curtime + appSecret; ;

string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());

dic.Add("appKey", appKey);

dic.Add("salt", salt);

dic.Add("signType", "v3");

dic.Add("sign", sign);

dic.Add("taskId", taskId);

dic.Add("scene", scene);

dic.Add("userLevel", userLevel);

dic.Add("history", history);

List<string> list_indexArr = new List<string>();

list_indexArr.Add(indexArr);

dic.Add("indexArr", list_indexArr);

dic.Add("count", count);

string result = PostJson(url, Newtonsoft.Json.JsonConvert.SerializeObject(dic));

return result;

}

public static string talk_generate_dialog(string taskId, generate_topic_scene scene, string userLevel, List<Dictionary<string, string>> history)

{

Dictionary<String, object> dic = new Dictionary<String, object>();

string url = "http://openapi.youdao.com/ai_dialog/generate_dialog";

string q = "topics";

string salt = DateTime.Now.Millisecond.ToString();

TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));

long millis = (long)ts.TotalMilliseconds;

string curtime = Convert.ToString(millis / 1000);

dic.Add("curtime", curtime);

string signStr = appKey + Truncate(q) + salt + curtime + appSecret; ;

string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());

dic.Add("appKey", appKey);

dic.Add("salt", salt);

dic.Add("signType", "v3");

dic.Add("sign", sign);

dic.Add("taskId", taskId);

dic.Add("scene", scene);

dic.Add("userLevel", userLevel);

dic.Add("history", history);

string result = PostJson(url, Newtonsoft.Json.JsonConvert.SerializeObject(dic));

return result;

}

public static string talk_get_default_topic()

{

Dictionary<String, String> dic = new Dictionary<String, String>();

string url = "http://openapi.youdao.com/ai_dialog/get_default_topic";

string q = "topics";

string salt = DateTime.Now.Millisecond.ToString();

TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));

long millis = (long)ts.TotalMilliseconds;

string curtime = Convert.ToString(millis / 1000);

dic.Add("curtime", curtime);

string signStr = appKey + Truncate(q) + salt + curtime + appSecret; ;

string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());

dic.Add("appKey", appKey);

dic.Add("salt", salt);

dic.Add("signType", "v3");

dic.Add("sign", sign);

dic.Add("q", System.Web.HttpUtility.UrlEncode(q));

string result = PostJson(url, Newtonsoft.Json.JsonConvert.SerializeObject(dic));

return result;

}

protected static string ComputeHash(string input, HashAlgorithm algorithm)

{

Byte[] inputBytes = Encoding.UTF8.GetBytes(input);

Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);

return BitConverter.ToString(hashedBytes).Replace("-", "");

}

protected static string Truncate(string q)

{

if (q == null)

{

return null;

}

int len = q.Length;

return len <= 20 ? q : (q.Substring(0, 10) + len + q.Substring(len - 10, 10));

}

protected static string LoadAsBase64(string filename)

{

try

{

FileStream filestream = new FileStream(filename, FileMode.Open);

byte[] arr = new byte[filestream.Length];

filestream.Position = 0;

filestream.Read(arr, 0, (int)filestream.Length);

filestream.Close();

return Convert.ToBase64String(arr);

}

catch (Exception ex)

{

return null;

}

}

protected static string PostJson(string url, string json)

{

string result = "";

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

req.Method = "POST";

req.ContentType = "application/json";

byte[] data = Encoding.UTF8.GetBytes(json.ToString());

req.ContentLength = data.Length;

using (Stream reqStream = req.GetRequestStream())

{

reqStream.Write(data, 0, data.Length);

reqStream.Close();

}

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

Stream stream = resp.GetResponseStream();

using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))

{

result = reader.ReadToEnd();

}

return result;

}

protected static string Post(string url, Dictionary<String, String> dic)

{

string result = "";

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

req.Method = "POST";

req.ContentType = "application/x-www-form-urlencoded";

StringBuilder builder = new StringBuilder();

int i = 0;

foreach (var item in dic)

{

if (i > 0)

builder.Append("&");

builder.AppendFormat("{0}={1}", item.Key, item.Value);

i++;

}

byte[] data = Encoding.UTF8.GetBytes(builder.ToString());

req.ContentLength = data.Length;

using (Stream reqStream = req.GetRequestStream())

{

reqStream.Write(data, 0, data.Length);

reqStream.Close();

}

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

Stream stream = resp.GetResponseStream();

using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))

{

result = reader.ReadToEnd();

}

return result;

}

}

public class get_default_topic_response

{

public string code = "";

public string msg = "";

public string requestId = "";

public get_default_topic_response_data data = new get_default_topic_response_data();

}

public class get_default_topic_response_data

{

public List<get_default_topic_response_data_topics> topicList = new List<get_default_topic_response_data_topics>();

}

public class get_default_topic_response_data_topics

{

public string tag = "";

public List<get_default_topic_response_data_topics_item> topics = new List<get_default_topic_response_data_topics_item>();

}

public class get_default_topic_response_data_topics_item

{

public string emoji = "";

public string name = "";

public string enName = "";

}

public class generate_topic_response

{

public string code = "";

public string msg = "";

public generate_topic_data data = new generate_topic_data();

}

public class generate_topic_data

{

public string taskId = "";

public generate_topic_scene scene = new generate_topic_scene();

}

public class generate_topic_scene

{

public string mainTask = "";

public string emoji = "";

public List<string> subTasks = new List<string>();

public string botGender = "";

public string botRole = "";

public string userRole = "";

public string content = "";

}

public class generate_dialog_data {

public string conendCodetent = "";

public string taskId = "";

public List<generate_dialog_data_resultArr> resultArr = new List<generate_dialog_data_resultArr>();

}

public class generate_dialog_data_resultArr

{

public List<string> result = new List<string>();

}

public class generate_dialog_response

{

public string code = "";

public string msg = "";

public generate_dialog_data data = new generate_dialog_data();

}

cs 复制代码
   public class YoudaoTalk
    {
        public static string appKey = "您的应用ID";//"您的应用ID";
        public static string appSecret = "您的应用密钥";//"您的应用密钥"; 

        //应用名称 一对一对话
        //已选服务 小P老师, AI口语老师, 个性化语音定制, 语音合成, 短语音识别, 实时语音识别, 语音评测, 实时语音评测
 

        public static string talk_generate_topic(string topic)
        {
            Dictionary<String, String> dic = new Dictionary<String, String>();
            string url = "http://openapi.youdao.com/ai_dialog/generate_topic";
            string q = "topics";
            string salt = DateTime.Now.Millisecond.ToString();
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            long millis = (long)ts.TotalMilliseconds;
            string curtime = Convert.ToString(millis / 1000);
            dic.Add("curtime", curtime);
            string signStr = appKey + Truncate(q) + salt + curtime + appSecret; ;
            string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());
            dic.Add("appKey", appKey);
            dic.Add("salt", salt);
            dic.Add("signType", "v3");
            dic.Add("sign", sign);
            dic.Add("topic", topic);

            string result = PostJson(url, Newtonsoft.Json.JsonConvert.SerializeObject(dic));
            return result;
        }
        public static string talk_generate_recommendation(string taskId, generate_topic_scene scene, string userLevel, List<Dictionary<string, string>> history, string indexArr ,string count)
        {
            Dictionary<String, object> dic = new Dictionary<String, object>();
            string url = "http://openapi.youdao.com/ai_dialog/generate_recommendation";
            string q = "topics";
            string salt = DateTime.Now.Millisecond.ToString();
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            long millis = (long)ts.TotalMilliseconds;
            string curtime = Convert.ToString(millis / 1000);
            dic.Add("curtime", curtime);
            string signStr = appKey + Truncate(q) + salt + curtime + appSecret; ;
            string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());
            dic.Add("appKey", appKey);
            dic.Add("salt", salt);
            dic.Add("signType", "v3");
            dic.Add("sign", sign);
            dic.Add("taskId", taskId);
            dic.Add("scene", scene);

            dic.Add("userLevel", userLevel);
            dic.Add("history", history);
            List<string> list_indexArr = new List<string>();
            list_indexArr.Add(indexArr);
            dic.Add("indexArr", list_indexArr);
            dic.Add("count", count);
            string result = PostJson(url, Newtonsoft.Json.JsonConvert.SerializeObject(dic));
            return result;
        }
         public static string talk_generate_dialog(string taskId, generate_topic_scene scene, string userLevel, List<Dictionary<string, string>> history)
        {
            Dictionary<String, object> dic = new Dictionary<String, object>();
            string url = "http://openapi.youdao.com/ai_dialog/generate_dialog";
            string q = "topics";
            string salt = DateTime.Now.Millisecond.ToString();
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            long millis = (long)ts.TotalMilliseconds;
            string curtime = Convert.ToString(millis / 1000);
            dic.Add("curtime", curtime);
            string signStr = appKey + Truncate(q) + salt + curtime + appSecret; ;
            string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());
            dic.Add("appKey", appKey);
            dic.Add("salt", salt);
            dic.Add("signType", "v3");
            dic.Add("sign", sign);
            dic.Add("taskId", taskId);
            dic.Add("scene", scene);

            dic.Add("userLevel", userLevel);
            dic.Add("history", history);

            string result = PostJson(url, Newtonsoft.Json.JsonConvert.SerializeObject(dic));
            return result;
        }


        public static string talk_get_default_topic()
        {
            Dictionary<String, String> dic = new Dictionary<String, String>();
            string url = "http://openapi.youdao.com/ai_dialog/get_default_topic";
            string q = "topics";
            string salt = DateTime.Now.Millisecond.ToString();
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            long millis = (long)ts.TotalMilliseconds;
            string curtime = Convert.ToString(millis / 1000);
            dic.Add("curtime", curtime);
            string signStr = appKey + Truncate(q) + salt + curtime + appSecret; ;
            string sign = ComputeHash(signStr, new SHA256CryptoServiceProvider());
            dic.Add("appKey", appKey);
            dic.Add("salt", salt);
            dic.Add("signType", "v3");
            dic.Add("sign", sign);
            dic.Add("q", System.Web.HttpUtility.UrlEncode(q));

            string result = PostJson(url, Newtonsoft.Json.JsonConvert.SerializeObject(dic));
            return result;
        }

        protected static string ComputeHash(string input, HashAlgorithm algorithm)
        {
            Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
            return BitConverter.ToString(hashedBytes).Replace("-", "");
        }

        protected static string Truncate(string q)
        {
            if (q == null)
            {
                return null;
            }
            int len = q.Length;
            return len <= 20 ? q : (q.Substring(0, 10) + len + q.Substring(len - 10, 10));
        }

        protected static string LoadAsBase64(string filename)
        {
            try
            {
                FileStream filestream = new FileStream(filename, FileMode.Open);
                byte[] arr = new byte[filestream.Length];
                filestream.Position = 0;
                filestream.Read(arr, 0, (int)filestream.Length);
                filestream.Close();
                return Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        protected static string PostJson(string url, string json)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            byte[] data = Encoding.UTF8.GetBytes(json.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
        protected static string Post(string url, Dictionary<String, String> dic)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

    }

    public class get_default_topic_response
    {
        public string code = "";
        public string msg = "";
        public string requestId = "";
        public get_default_topic_response_data data = new get_default_topic_response_data();
    }
    public class get_default_topic_response_data
    {
        public List<get_default_topic_response_data_topics> topicList = new List<get_default_topic_response_data_topics>();
    }
    public class get_default_topic_response_data_topics
    {
        public string tag = "";
        public List<get_default_topic_response_data_topics_item> topics = new List<get_default_topic_response_data_topics_item>();
    }
    public class get_default_topic_response_data_topics_item
    {
        public string emoji = "";
        public string name = "";
        public string enName = "";
    }

    public class generate_topic_response
    {
        public string code = "";
        public string msg = "";
        public generate_topic_data data = new generate_topic_data();
    }
    public class generate_topic_data
    {
        public string taskId = "";
        public generate_topic_scene scene = new generate_topic_scene();
    }
    public class generate_topic_scene
    {
        public string mainTask = "";
        public string emoji = "";
        public List<string> subTasks = new List<string>();
        public string botGender = "";
        public string botRole = "";
        public string userRole = "";
        public string content = "";
    }

    public class generate_dialog_data {
        public string conendCodetent = "";
        public string taskId = "";
        public List<generate_dialog_data_resultArr> resultArr = new List<generate_dialog_data_resultArr>();
    }
    public class generate_dialog_data_resultArr
    {
        public List<string> result = new List<string>();
    }

    public class generate_dialog_response
    {
        public string code = "";
        public string msg = "";
        public generate_dialog_data data = new generate_dialog_data(); 
    }     
相关推荐
码上淘金3 小时前
【Python】Python常用控制结构详解:条件判断、遍历与循环控制
开发语言·python
Brilliant Nemo3 小时前
四、SpringMVC实战:构建高效表述层框架
开发语言·python
格林威5 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
橙子199110165 小时前
在 Kotlin 中什么是委托属性,简要说说其使用场景和原理
android·开发语言·kotlin
androidwork5 小时前
Kotlin Android LeakCanary内存泄漏检测实战
android·开发语言·kotlin
学地理的小胖砸5 小时前
【Python 基础语法】
开发语言·python
极小狐6 小时前
极狐GitLab 通用软件包存储库功能介绍
java·数据库·c#·gitlab·maven
钢铁男儿6 小时前
C# 方法(可选参数)
数据库·mysql·c#
DanB247 小时前
Java笔记4
java·开发语言·笔记
Dddle17 小时前
C++:this指针
java·c语言·开发语言·c++