首页 > 编程语言 >C#版的有道智云对话接口

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

时间:2024-10-29 20:15:59浏览次数:3  
标签:string C# 接口 dic topic Add new public 智云

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

标签:string,C#,接口,dic,topic,Add,new,public,智云
From: https://blog.csdn.net/withcsharp2/article/details/143349158

相关文章

  • 【OJ题解】C++ 把字符串转换成整数
    ......
  • 关于Rust使用cross进行交叉编译,openssl报错
    遇到错误详情解决:使用cross交叉编译,确保已经安装好了openssl的相关依赖,但是依然报和openssl有关的错误,此时需要在 Cargo.toml 添加openssl的依赖查看:https://github.com/sfackler/rust-openssl?tab=readme-ov-file1[dependencies]2openssl={version="0.10",feat......
  • 2024CSP-S游记 & (半?)退役记
    流水账,供自己回忆。(1)序幕2023年8月10号(±2天),中考完的我踏入了高中的校园,由于本蒟蒻自小学起就对信息竞赛有一定的兴趣,所以在2023年9月底学校开始寻找对各学科竞赛感兴趣的学生时,蒟蒻毫不犹豫的报名了物理竞赛[1]信息竞赛,自此拉开了我OIer生涯的序幕。[1]:在绿皮书物理竞赛的摧......
  • CSP-S 2024 游记
    \(\text{Day-28}\sim\text{-7}\)复习了两个星期dp,感觉状压十分强大,但是看得不是很透彻。\(\text{Day-6}\sim\text{-2}\)停课爽!模拟赛爽!云斗模拟赛总算让我见识了什么叫打表出省一。\(\text{Day-1}\)上午在和\(\texttt{TZYLT}\)和\(\texttt{QianXiquq}\)打板子,感......
  • DBA3803: Predictive Analytics in Business
    DBA3803: PredictiveAnalytics in BusinessOverviewAnalytics is best learned by applyingthe methods andtechniquesto real-world data and problems. Forthis project:1.  Identify  a  real-world  problem or an area where predicti......
  • COMP5416/4416 A CSMA/CA and CDM
    COMP5416/4416Assignment2 2024S2Duedate:27 Oct2024 at 23:59There are 7 mandatory tasks (1—7) plus one optional task (8). Each mandatory taskiswith 15 points andthe optionaltaskis with additional 10 points. However,your......
  • 【论文笔记】C$^2$RL: Content and Context Representation Learning for Gloss-free
    ......
  • 【Unity休闲风格UI资源】GUI - Casual Fantasy
    GUI-CasualFantasy是Unity的一款用户界面(GUI)插件,专为休闲幻想类游戏设计,提供了一套完整的UI资源和工具。该插件能帮助开发者快速搭建符合幻想风格的用户界面,适合各种类型的游戏,特别是带有轻松、卡通风格的RPG、冒险、策略等游戏项目。以下是它的主要功能和特点:1......
  • ThreadLocal为什么会发生内存泄漏
    ThreadLocal会发生内存泄漏的原因有:一、ThreadLocal的生命周期与线程的生命周期相关;二、长时间不使用ThreadLocal可能导致内存泄漏等。ThreadLocal的生命周期与线程的生命周期相关是指,如果ThreadLocal没有被正确地清除,就会导致ThreadLocalMap中的Entry长时间无法被回收,从而导致内......
  • 基于深度学习+pytorch+Flask+SpringBoot+Vue+MySQL的水果识别系统
    目录前言1.登陆注册2.水果识别3.识别记录4.用户管理5.个人中心6.模型训练7.模型测试源码获取前言本系统是一个完整的基于深度学习+pytorch+Flask+SpringBoot+Vue+MySQL的水果识别系统。包括LeNet、AlexNet、VGG、GoogLeNet、ResNet、MobileNetV2网络模型。可以直接......