|
1 | 1 | #include "mirai_bot.hpp" |
2 | 2 | #include "events/events.hpp" |
| 3 | +#include "httplib.h" |
3 | 4 |
|
4 | 5 | namespace Cyan |
5 | 6 | { |
@@ -41,51 +42,52 @@ namespace Cyan |
41 | 42 |
|
42 | 43 | bool NewFriendRequestEvent::Respose(int operate, const string& message) |
43 | 44 | { |
44 | | - static const string api_url = bot_->GetApiUrlPrefix() + "/resp/newFriendRequestEvent"; |
45 | | - |
46 | | - json j; |
47 | | - j["sessionKey"] = bot_->GetSessionKey(); |
48 | | - j["eventId"] = this->EventId; |
49 | | - j["fromId"] = (int64_t)this->FromId; |
50 | | - j["groupId"] = (int64_t)this->GroupId; |
51 | | - j["operate"] = operate; |
52 | | - j["message"] = message; |
53 | | - |
54 | | - string pData = j.dump(); |
55 | | - HTTP http; http.SetContentType("application/json;charset=UTF-8"); |
56 | | - auto res = http.Post(api_url, pData); |
57 | | - |
58 | | - if (res.Ready) |
| 45 | + json data = |
| 46 | + { |
| 47 | + { "sessionKey", bot_->GetSessionKey() }, |
| 48 | + { "eventId", this->EventId}, |
| 49 | + { "fromId", (int64_t)this->FromId}, |
| 50 | + { "groupId", (int64_t)this->GroupId}, |
| 51 | + { "operate", operate}, |
| 52 | + { "message", message}, |
| 53 | + }; |
| 54 | + |
| 55 | + httplib::Client& http_client = *(bot_->GetHttpClient()); |
| 56 | + auto res = http_client.Post("/resp/newFriendRequestEvent", data.dump(), "application/json;charset=UTF-8"); |
| 57 | + if (res) |
59 | 58 | { |
| 59 | + if (res->status != 200) |
| 60 | + throw std::runtime_error("[mirai-api-http error]: " + res->body); |
60 | 61 | return true; |
61 | 62 | } |
62 | 63 | else |
63 | | - throw runtime_error(res.ErrorMsg); |
| 64 | + throw runtime_error("网络错误"); |
64 | 65 |
|
65 | 66 | } |
66 | 67 |
|
67 | 68 | bool MemberJoinRequestEvent::Respose(int operate, const string& message) |
68 | 69 | { |
69 | | - static const string api_url = bot_->GetApiUrlPrefix() + "/resp/memberJoinRequestEvent"; |
70 | 70 |
|
71 | | - json j; |
72 | | - j["sessionKey"] = bot_->GetSessionKey(); |
73 | | - j["eventId"] = this->EventId; |
74 | | - j["fromId"] = (int64_t)this->FromId; |
75 | | - j["groupId"] = (int64_t)this->GroupId; |
76 | | - j["operate"] = operate; |
77 | | - j["message"] = message; |
78 | | - |
79 | | - string pData = j.dump(); |
80 | | - HTTP http; http.SetContentType("application/json;charset=UTF-8"); |
81 | | - auto res = http.Post(api_url, pData); |
82 | | - |
83 | | - if (res.Ready) |
| 71 | + json data = |
| 72 | + { |
| 73 | + { "sessionKey", bot_->GetSessionKey() }, |
| 74 | + { "eventId", this->EventId}, |
| 75 | + { "fromId", (int64_t)this->FromId}, |
| 76 | + { "groupId", (int64_t)this->GroupId}, |
| 77 | + { "operate", operate}, |
| 78 | + { "message", message}, |
| 79 | + }; |
| 80 | + |
| 81 | + httplib::Client& http_client = *(bot_->GetHttpClient()); |
| 82 | + auto res = http_client.Post("/resp/memberJoinRequestEvent", data.dump(), "application/json;charset=UTF-8"); |
| 83 | + if (res) |
84 | 84 | { |
| 85 | + if (res->status != 200) |
| 86 | + throw std::runtime_error("[mirai-api-http error]: " + res->body); |
85 | 87 | return true; |
86 | 88 | } |
87 | 89 | else |
88 | | - throw runtime_error(res.ErrorMsg); |
| 90 | + throw runtime_error("网络错误"); |
89 | 91 |
|
90 | 92 | } |
91 | 93 |
|
|
0 commit comments