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(); 
    }     
相关推荐
酒鬼猿11 分钟前
C++初阶(七)--类和对象(4)
开发语言·c++
你不讲 wood16 分钟前
预览 PDF 文档
开发语言·前端·javascript·pdf·html·node·文件预览
蜡笔小新星22 分钟前
PyTorch的基础教程
开发语言·人工智能·pytorch·经验分享·python·深度学习·学习
gorgor在码农23 分钟前
Lua 从基础入门到精通(非常详细)
开发语言·lua
houyawei_NO129 分钟前
QT相机连接与拍照
开发语言·qt
Yaml434 分钟前
Spring Boot整合EasyExcel:实现大规模数据的并行导出与压缩下载
java·开发语言·spring boot
无敌最俊朗@37 分钟前
unity3d————三角函数练习题
开发语言·学习·unity·c#·游戏引擎
编程修仙39 分钟前
Java三大特性之继承
java·开发语言
bitenum1 小时前
qsort函数的学习与使用
c语言·开发语言·学习·算法·visualstudio·1024程序员节
架构师ZYL1 小时前
python之数据结构与算法(数据结构篇)-- 元组
开发语言·javascript·python·信息可视化·数据结构与算法