欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单的调用图灵机器人

程序员文章站 2022-03-06 20:53:22
1、去http://www.tuling123.com网址创建账号,创建机器人 重点 2、上代码 winform界面如上 HttpRequestHelper.PostAsync方法具体如下 winform后台代码如下 到此一个简单调用图灵机器人完成。 ......

1、去网址创建账号,创建机器人

重点简单的调用图灵机器人

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

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