C#制作簡易QQ聊天機器人


最近對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了  自己想加什么就加什么

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM