Bootstrap

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

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

;