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

ops/2024/11/2 17:58:03/

   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(); 
    }     

   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(); }     


http://www.ppmy.cn/ops/130482.html

相关文章

从0开始学统计-什么是Z-score

引言 Z-score起源于19世纪后期&#xff0c;基于标准正态分布&#xff08;高斯分布&#xff09;理论。这一概念建立在标准差的基础上&#xff0c;用于衡量数据点相对于平均值的偏离程度。通过将数据点的偏离转换为标准差单位&#xff0c;便可以使用Z-score比较不同数据集中的数…

【视频】OpenCV:识别颜色、绘制轮廓

1、安装OpenCV库 sudo apt install libopencv-dev2、链接库 将 OpenCV 头文件路径和库添加到CMake中,在 CMakeLists.txt 中添加 1)查找库 find_package(OpenCV REQUIRED) 或者 find_package(OpenCV REQUIRED core imgproc highgui) 2)添加头文件路径 include_directories…

【若依框架】代码生成详细教程,15分钟搭建Springboot+Vue3前后端分离项目,基于Mysql8数据库和Redis5,管理后台前端基于Vue3和Element Plus,开发小程序数据后台

今天我们来借助若依来快速的搭建一个基于springboot的Java管理后台&#xff0c;后台网页使用vue3和 Element Plus来快速搭建。这里我们可以借助若依自动生成Java和vue3代码&#xff0c;这就是若依的强大之处&#xff0c;即便你不会Java和vue开发&#xff0c;只要跟着石头哥也可…

传感器图像分割系统:创新探讨教学

传感器图像分割系统源码&#xff06;数据集分享 [yolov8-seg-C2f-ODConv&#xff06;yolov8-seg-SPDConv等50全套改进创新点发刊_一键训练教程_Web前端展示] 1.研究背景与意义 项目参考ILSVRC ImageNet Large Scale Visual Recognition Challenge 项目来源AAAI Global Al l…

JS:列表操作

目录 1、列表截取2、列表数据包含3、列表筛选4、极值操作5、获取列表对象某一属性构建列表6、获取元素在列表中的下标7、列表去重 1、列表截取 列表截取&#xff1a;List.slice(start, end)&#xff0c;左闭右开 var dataList [1,2,3,4,5,6] var resultList dataList.slice(0…

MySQL-基础汇总

MySQL-基础汇总 数据库对于任何一个从事后台开发的人说都是永远躲不掉的&#xff0c;任何系统或程序离开了数据的支持都变的毫无意义。而管理数据的工具——数据库就显得尤为重要。本章节我们的核心就是 MySQL&#xff0c;相信很多小伙伴跟我一样&#xff0c;也沉浸在增、删、…

每日OJ题_牛客_AB20走迷宫_BFS_C++_Java

目录 牛客_AB20走迷宫_BFS 题目解析 C代码 Java代码 牛客_AB20走迷宫_BFS 走迷宫_牛客题霸_牛客网 (nowcoder.com) 描述&#xff1a; 给定一个nm的网格&#xff0c;在网格中每次在不超过边界的情况下可以选择向上、向下、向左、向右移动一格。网格中的一些格子上放置有障…

JVM 调优深度剖析:优化 Java 应用的全方位攻略(一)

这是我们就业陪跑训练营学员总结的文章&#xff0c;我觉得不错&#xff0c;和大家分享一下。 Java 应用中&#xff0c;JVM 调优至关重要。本文聚焦 JVM 调优&#xff0c;涵盖主要目标、思路与方法。目标包括减少 GC 停顿、提高吞吐量等&#xff1b; 思路有多步&#xff0c;如选…