码迷,mamicode.com
首页 > 其他好文 > 详细

简单的调用图灵机器人

时间:2019-06-20 12:56:12      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:sel   gzip   default   tty   完成   zip   toe   sage   handler   

1、去http://www.tuling123.com网址创建账号,创建机器人

重点技术图片

2、上代码

 

技术图片

winform界面如上

HttpRequestHelper.PostAsync方法具体如下

    /// <summary>
    /// 使用post方法异步请求
    /// </summary>
    /// <param name="url">目标链接</param>
    /// <param name="data">发送的参数字符串</param>
    /// <returns>返回的字符串</returns>
    public static async Task<string> PostAsync(string url, string data, Dictionary<string, string> header = null, bool Gzip = false)
    {
        using (HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }))
        {
            HttpContent content = new StringContent(data,System.Text.Encoding.UTF8);
            if (header != null)
            {
                client.DefaultRequestHeaders.Clear();
                foreach (var item in header)
                {
                    client.DefaultRequestHeaders.Add(item.Key, item.Value);
                }
            }
            HttpResponseMessage response = await client.PostAsync(url, content);
            response.EnsureSuccessStatusCode();
            string responseBody;
            if (Gzip)
            {
                GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync());
                responseBody = new StreamReader(inputStream).ReadToEnd();
            }
            else
            {
                responseBody = await response.Content.ReadAsStringAsync();
            }
            return responseBody;
        }
    }

 

winform后台代码如下

        public TuLingTest()
        {
            InitializeComponent();
        }

        private Action<string> ShowMsg;

        private void TuLingTest_Load(object sender, EventArgs e)
        {
            ShowMsg = new Action<string>((string msg) =>
            {
                if (Txt_Msg.TextLength > 30000) Txt_Msg.Clear();
                Txt_Msg.AppendText("\r\n-------当前时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "---------------------------------"
                    + "\r\n图灵机器人回复:" + msg + "\r\n");
                Txt_Msg.ScrollToCaret();
            });
        }

        private async void Btn_start_Click(object sender, EventArgs e)
        {
            RequestInfo request = new RequestInfo();
            UserInfo userInfo = new UserInfo();
            userInfo.apiKey = "你的apikey";

            Perception perception = new Perception();
            InputText inputText = new InputText();
            inputText.text = Txt_Reust.Text.Trim();
            perception.inputText = inputText;

            request.perception = perception;
            request.userInfo = userInfo;

            var result = await HttpRequestHelper.PostAsync("http://openapi.tuling123.com/openapi/api/v2", JsonConvert.SerializeObject(request));
            ResponseInfo response = JsonConvert.DeserializeObject<ResponseInfo>(result);
            Txt_Msg.BeginInvoke(ShowMsg, response.results[0].values.text);
        }





        #region 请求消息
        public class RequestInfo
        {
            public int reqType { get; set; }
            public Perception perception { get; set; }
            public UserInfo userInfo { get; set; }
        }
        public class UserInfo
        {
            public string apiKey { get; set; }
            public string userId { get; set; }
        }
        public class Perception
        {
            public InputText inputText { get; set; }
            public InputImage inputImage { get; set; }
            public List<Location> selfInfo { get; set; }
        }
        public class Location
        {
            public string city { get; set; }
            public string province { get; set; }
            public string street { get; set; }
        }
        public class InputText
        {
            public string text { get; set; }
        }
        public class InputImage
        {
            public string url { get; set; }
        }
        #endregion

        #region 返回消息
        public class ResponseInfo
        {
            public Intent intent { get; set; }
            public List<Results> results { get; set; }
        }
        public class Intent
        {
            public string code { get; set; }
        }

        public class Results
        {
            public int groupType { get; set; }
            public string resultType { get; set; }
            public Values values { get; set; }
        }

        public class Values
        {
            public string text { get; set; }
        }

        #endregion

到此一个简单调用图灵机器人完成。

简单的调用图灵机器人

标签:sel   gzip   default   tty   完成   zip   toe   sage   handler   

原文地址:https://www.cnblogs.com/youlicc/p/11057831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!