标签:
最近对QQ聊天机器人比较感兴趣,奈何一直没找到C#的源码,就自己摸索,好了废话不多说了,开始正题。
首先我们要准备的是C# 的SDK下载地址:https://cqp.cc/t-24088-1-1,Newtonsoft.Json.dll插件
打开C#SDK源码后找到MyApp.cs(QQ发送消模块) 在里面找到QQ私聊消息
HttpWebResponse Response = null;
string result = null;
String _strMessage = msg(收到的QQ消息);
String INFO = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(_strMessage));
String getURL = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + INFO;
HttpWebRequest MyRequest = (HttpWebRequest)HttpWebRequest.Create(getURL);
HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();
Response = MyResponse;
using (Stream MyStream = MyResponse.GetResponseStream())
{
long ProgMaximum = MyResponse.ContentLength;
long totalDownloadedByte = 0;
byte[] by = new byte[1024];
int osize = MyStream.Read(by, 0, by.Length);
Encoding encoding = Encoding.UTF8;
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
result += encoding.GetString(by, 0, osize);
long ProgValue = totalDownloadedByte;
osize = MyStream.Read(by, 0, by.Length);
}
}
//解析json
JsonReader reader = new JsonTextReader(new StringReader(result));
while (reader.Read())
{
if (reader.Path == "content")//content是青云客传过来的字符串里面的一个字段不用修改
{
//结果赋值
result = reader.Value.ToString();//result最终机器人回答的话
}
}
把这些代码写进去之后打包成DLL文件放入酷Q的CSharpDemoTP文件夹,别忘记把Newtonsoft.Json.dll插件放入根目录,主要原理就是引入青云客的API 将QQ收到的消息
交给这个API处理。
我是新手菜鸟一枚,不喜勿喷,谢谢
标签:
原文地址:http://www.cnblogs.com/gywxhn/p/5787092.html