Slack: Support me_message and bot_message

This commit is contained in:
Jan Kaluza 2015-12-08 17:58:56 +01:00
parent 9cd7de126d
commit f823618439
2 changed files with 40 additions and 2 deletions

View file

@ -58,6 +58,18 @@ DEFINE_LOGGER(logger, "SlackAPI");
} \
bool NAME = NAME##_tmp.GetBool();
#define GET_OBJECT(FROM, NAME) rapidjson::Value &NAME = FROM[#NAME]; \
if (!NAME.IsObject()) { \
LOG4CXX_ERROR(logger, "No '" << #NAME << "' object in the reply."); \
return; \
}
#define STORE_STRING_OPTIONAL(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
std::string NAME; \
if (NAME##_tmp.IsString()) { \
NAME = NAME##_tmp.GetString(); \
}
SlackAPI::SlackAPI(Component *component, const std::string &token) : HTTPRequestQueue(component) {
m_component = component;
m_token = token;
@ -272,6 +284,13 @@ void SlackAPI::getSlackUserInfo(HTTPRequest *req, bool ok, rapidjson::Document &
ret[info.id] = info;
LOG4CXX_INFO(logger, info.id << " " << info.name);
GET_OBJECT(users[i], profile);
STORE_STRING_OPTIONAL(profile, bot_id);
if (!bot_id.empty()) {
ret[bot_id] = info;
LOG4CXX_INFO(logger, bot_id << " " << info.name);
}
}
return;

View file

@ -75,6 +75,12 @@ SlackRTM::~SlackRTM() {
} \
std::string NAME = NAME##_tmp.GetString();
#define STORE_STRING_OPTIONAL(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
std::string NAME; \
if (NAME##_tmp.IsString()) { \
NAME = NAME##_tmp.GetString(); \
}
void SlackRTM::handlePayloadReceived(const std::string &payload) {
rapidjson::Document d;
if (d.Parse<0>(payload.c_str()).HasParseError()) {
@ -87,10 +93,23 @@ void SlackRTM::handlePayloadReceived(const std::string &payload) {
if (type == "message") {
STORE_STRING(d, channel);
STORE_STRING(d, user);
STORE_STRING(d, text);
STORE_STRING(d, ts);
onMessageReceived(channel, user, text, ts);
STORE_STRING_OPTIONAL(d, subtype);
if (subtype == "bot_message") {
STORE_STRING(d, bot_id);
onMessageReceived(channel, bot_id, text, ts);
}
else if (subtype == "me_message") {
text = "/me " + text;
STORE_STRING(d, user);
onMessageReceived(channel, user, text, ts);
}
else {
STORE_STRING(d, user);
onMessageReceived(channel, user, text, ts);
}
}
}