Skip to content

Commit 12d4a92

Browse files
committed
自动向新人问候头文件
1 parent 904ac98 commit 12d4a92

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

auto_question.hpp

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/**
2+
* @file
3+
* @author
4+
* @date
5+
*
6+
* usage:
7+
* auto_question question;
8+
* auto_question::value_qq_list list;
9+
*
10+
* list.push_back("lovey599");
11+
*
12+
* questioin.add_to_list(list);
13+
* questioin.on_handle_message(group, qqclient);
14+
*/
15+
16+
#ifndef __QQBOT_AUTO_QUESTION_H__
17+
#define __QQBOT_AUTO_QUESTION_H__
18+
19+
#include <iostream>
20+
#include <fstream>
21+
#include <string>
22+
#include <vector>
23+
#include <map>
24+
25+
#include <boost/noncopyable.hpp>
26+
#include <boost/assign/list_of.hpp>
27+
#include <boost/assign.hpp>
28+
#include <boost/bind.hpp>
29+
#include <boost/foreach.hpp>
30+
#include <boost/format.hpp>
31+
#include <boost/asio.hpp>
32+
#include <boost/date_time/posix_time/posix_time.hpp>
33+
34+
#include "libwebqq/webqq.h"
35+
36+
void qq_msg_send_callback(const boost::system::error_code& ec)
37+
{
38+
// nothing to do
39+
}
40+
41+
class auto_question : public boost::noncopyable
42+
{
43+
public:
44+
typedef std::vector<std::string> value_qq_list;
45+
46+
auto_question(std::string filename = "question.txt")
47+
: _filename(filename)
48+
, _is_processing(false)
49+
{
50+
try
51+
{
52+
load_question();
53+
}
54+
catch (std::exception& ex)
55+
{
56+
std::cerr << "load questioin error." << std::endl;
57+
}
58+
}
59+
60+
void add_to_list(value_qq_list list)
61+
{
62+
if (_is_processing)
63+
{
64+
_is_processing = false;
65+
}
66+
67+
BOOST_FOREACH(std::string item, list)
68+
{
69+
_process_count.insert(std::pair<std::string, int>(item, _questioin_count));
70+
}
71+
}
72+
73+
void on_handle_message(qqGroup& group, webqq& qqclient)
74+
{
75+
handle_question(group, qqclient);
76+
}
77+
78+
protected:
79+
void load_question()
80+
{
81+
std::fstream file(_filename.c_str(), std::ios_base::in);
82+
char question[512 + 1], answer[512 + 1];
83+
84+
while (file.good())
85+
{
86+
file.getline(question, 512);
87+
file.getline(answer, 512);
88+
89+
if (strlen(question) == 0)
90+
{
91+
continue;
92+
}
93+
94+
_question.insert(std::pair<std::string, std::string>(question, answer));
95+
}
96+
97+
file.close();
98+
99+
_questioin_count = _question.size();
100+
}
101+
102+
void handle_question(qqGroup& group, webqq& qqclient)
103+
{
104+
boost::asio::io_service io;
105+
std::map<std::string, int>::iterator iter;
106+
std::string str_msg_body = build_message();
107+
108+
for (iter = _process_count.begin(); iter != _process_count.end(); ++iter)
109+
{
110+
std::string str_msg = boost::str(boost::format("@%1% 你好,欢迎你加入本群.请先回答以下问题:\n%2%谢谢\n") % iter->first % str_msg_body);
111+
112+
std::cout << str_msg << std::endl;
113+
qqclient.send_group_message(group, str_msg, qq_msg_send_callback);
114+
115+
io.reset();
116+
boost::asio::deadline_timer timer(io, boost::posix_time::seconds(1));
117+
timer.wait();
118+
}
119+
}
120+
121+
std::string build_message()
122+
{
123+
value_question::iterator iter;
124+
std::string str_msg = "";
125+
int index = 1;
126+
127+
for (iter = _question.begin(); iter != _question.end(); ++iter)
128+
{
129+
str_msg += boost::str(boost::format("问题%1%.%2%\n") % (index++) % iter->first);
130+
}
131+
132+
return str_msg;
133+
}
134+
135+
private:
136+
std::string _filename;
137+
138+
typedef std::map<std::string, std::string> value_question;
139+
value_question _question;
140+
141+
int _questioin_count;
142+
143+
bool _is_processing;
144+
145+
std::map<std::string, int> _process_count;
146+
};
147+
148+
#endif

0 commit comments

Comments
 (0)