⾃定义QQ机器⼈,实现关键字回复
使⽤第三⽅,可以很轻松的实现QQ机器⼈的⾃定义:打开源代码->到相应接⼝(私聊或聊或其他)->⾃定义⾃⼰的内容->编译⽣成*.dll 动态库->放⼊下载好的酷Q相应⽂件夹->登陆酷Q(最好⽤⼩号)。
这⾥主要强调有两个⽅⾯:
1、如何实现⾃定义的对⼈友好化:即不需要编程知识也可以⾃定义实现。可以把关键字、回复内容放⼊⼀个配置⽂档内,这样只需修该配置⽂档即可。我使⽤的是xml。在源代码添加⼀个xmlLoader类即可解决问题
2、如何提⾼机器⼈处理消息的性能:给程序添加⼀个专门处理消息的线程。其中⾃⼰在使⽤互斥锁时不够熟练,应记得及时解锁,否则会出现函数堵塞。
具体代码如下:
1#pragma once
2 #include "qqSupport.h"
3 #include <list>
4 #include "tinyxml/tinyxml.h"
5
6using namespace std;
7
8class xmlLoader
9 {
10public:
11string getXmlPath()
12 {
13string xP = getPath();
14string xmlPath = xP + "l";
15
16return xmlPath;
17 }
18
19
20int loadXml(string xmlPath)
21 {
22//加载xml⽂件
23 TiXmlDocument* doc = new TiXmlDocument();
24if (doc == NULL)
25 {
26return -1;
27 }
28 doc->LoadFile(xmlPath);
29
30 TiXmlElement* xmlRoot = doc->RootElement();
31 TiXmlElement* xmlChildDef = xmlRoot->FirstChildElement("default");
32 defaultAnswer = xmlChildDef->GetText();
33
34
35 TiXmlElement* xmlChild = xmlRoot->FirstChildElement("match");
36while (xmlChild)
37 {
38 TiXmlElement* xmlKW = xmlChild->FirstChildElement("keyword");
39 TiXmlElement* xmlAS = xmlChild->FirstChildElement("answer");
40 msgContent mC;
41 mC.keyword = xmlKW->GetText();
42 mC.answer = xmlAS->GetText();
43
44 m_msgContentList.push_back(mC);
45//addLog(mC.keyword.c_str());
46//addLog(mC.anwser.c_str());
47
48 xmlChild = xmlChild->NextSiblingElement("match");
49 }
50
51return0;
52 }
53
54string findAnswer(int type,string msg)
55 {
56//根据关键字答案
57for (list<msgContent>::iterator iter = m_msgContentList.begin();
58 iter != d(); iter++)
59 {
60
61 msgContent& mC = *iter;
62 addLog(mC.keyword.c_str());
63 addLog(msg.c_str());
64 addLog(mC.answer.c_str());
65if ((int)msg.find(mC.keyword)>0)
66 {
67//addLog("有答案");
68return mC.answer;
69 }
70 }
71
72return defaultAnswer;
73 }
74
75private:
76struct msgContent //xml内容
77 {
78string keyword;
79string answer;
80 };
81 list<msgContent> m_msgContentList; //保存到列表82string defaultAnswer;
83 };
xmlLoader.h
1#pragma once
2 #include "afos/AfMutex.h"
3 #include "afos/AfThread.h"
4 #include "xmlLoader.h"
5 #include <list>
6
7using namespace std;
8
9class syncTask:public AfThread
10 {
11public:
12int startService();
13void stopService();
14int addMsg(int type, int64_t from, const char *msg); 15private:
16int ThreadMain();
17private:
18struct recvMsg //收到的信息
19 {
20int type;
21 int64_t from;
22string msg;
23 };
24
25private:
26
27 list<recvMsg> m_recvMsgList; //保存信息到列表
28 AfMutex m_mutex; //互斥锁
29bool m_quitFlag;
30 xmlLoader m_loader; //加载xml类
31 };
32
33
34void myIniti();
35
36void myEexit();
37
38void msgToThread(int type,int64_t from,const char *msg); myplugin.h
1 #include "myplugin.h"
2 #include "xmlLoader.h"
3 #include "qqSupport.h"
4
5
6 syncTask m_serviceTask;
7
8int syncTask::startService()
9 {
10 std::string xp = XmlPath();
11if (m_loader.loadXml(xp)<0)
12 {
13return -1;
14//addLog("不能加载xml");
15 }
16 m_quitFlag = false;
17
18//进⼊线程
19this->Run();
21return0;
22 }
23
24
25void syncTask::stopService()
26 {
27 m_quitFlag = true;
28 Join(this);
29 }
30
31
32int syncTask::ThreadMain()
33 {
34//addLog("进⼊线程");
35while (!m_quitFlag)
36 {
37 AfThread::Msleep(50);
38
39bool flag=false;
40 recvMsg rM;
41 m_mutex.Lock();//加锁
42if (m_recvMsgList.size()>0)
43 {
44 flag = true;
45 rM = m_recvMsgList.front();
46 addLog(rM.msg.c_str());
47 m_recvMsgList.pop_front();
48//addLog("有消息进⼊");
49 }
50 m_mutex.Unlock();
51if (!flag)
52 {
53continue;
54 }
55 std::string answer = m_loader.pe, rM.msg);
56 pe, rM.from, answer.c_str());
57 }
58return0;
59 }
60
61
62int syncTask::addMsg(int type, int64_t from, const char * msg)
63 {
64 recvMsg rM;
65 rM.type = type;
66 rM.from = from;
67 rM.msg = msg;
68
69 m_mutex.Lock();//加锁
70if (m_recvMsgList.size()<100)
71 {
72 m_recvMsgList.push_back(rM);
73 }
74 m_mutex.Unlock();
75
76return0;
77 }
78
79
80
81void myIniti()
82 {
83 m_serviceTask.startService();
84 }
85
86void myEexit()
87 {
88 m_serviceTask.stopService();
qq用户名89 }
90
91void msgToThread(int type, int64_t from, const char *msg)
92 {
93 m_serviceTask.addMsg(type, from, msg);
94 }
myplugin.cpp
1#pragma once
2 #include "stdafx.h"
3 #include <string>
4 #include "cqp.h"
5 #include "appmain.h"
7void sendMsg(int type, int64_t from, std::string msg); 8void addLog(const char* msg);
9 std::string getPath();
qqSupport.h
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论