最近對QQ聊天機器人比較感興趣,奈何一直沒找到C#的源碼,就自己摸索,好了廢話不多說了,開始正題。
首先我們要準備的是C# 的SDK下載下傳位址:http://pan.baidu.com/s/1geW0X3P,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處理。
我是新手菜鳥一枚,不喜勿噴,謝謝
詳細教程
這裡加上上文代碼就OK了 自己想加什麼就加什麼