个人聊天机器人C#版--图灵支持

来源:互联网 发布:html后台框架模板源码 编辑:程序博客网 时间:2024/04/27 08:20

刚看见一个java调用图灵API的机器人。

我擦。。居然直接照搬官网的案例就可以了(猜测)。

这怎么可以。我也写一个,还是自己写。

为了节约时间,一切从简。。。。。(其实是我懒)

首先用一般处理程序写一个远程调用HTTP的例子,代码:

string ans = context.Request.Params["ans"];            string res = string.Empty;            if (!string.IsNullOrEmpty(ans))            {                WebRequest wq = WebRequest.Create("http://www.tuling123.com/openapi/api?key=key&info=" + ans);                WebResponse wp = wq.GetResponse();                res = new StreamReader(wp.GetResponseStream()).ReadToEnd();            }            context.Response.ContentType = "text/plain";            context.Response.Write(res);

其中的url中的key为注册的key。。。这个只是一个例子


其实就是因为js不支持跨域,自己做了一个跨域的支持。所有东西原样返回。。。。因为js处理的更方便。

然后。。。用js吧结果处理一下,发送到网页的控件中。。。。一切OK!

<html><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title>机器人自动应答0.1</title>    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>    <style>        #txt {            width:500px;            height:300px;        }    </style></head><body>    <textarea id="txt"></textarea>    <br />    <input type="text" id="ans" />    <input type="button" value="发送" id="btn" /></body></html><script type="text/javascript">    $(function () {        $("#btn").click(function () {            $.post("tuling.ashx", { ans: $("#ans").val() }, function (data) {                if (data.text) {                    $("#txt").append("机器人回复:"+data.text+"\r");                    if (data.list) {                        $("#txt").append(data.list+"\r");                    }                    $("#ans").val("");                }            }, "json");        });    })</script>

以上所有代码照搬有效。。。注意自己申请key。

这里有一个我做好的例子:猛戳我!


0 0