Changeset - f823618439d9
[Not reviewed]
0 2 0
Jan Kaluza - 10 years ago 2015-12-08 17:58:56
jkaluza@redhat.com
Slack: Support me_message and bot_message
2 files changed with 40 insertions and 2 deletions:
0 comments (0 inline, 0 general)
spectrum/src/frontends/slack/SlackAPI.cpp
Show inline comments
 
@@ -49,24 +49,36 @@ DEFINE_LOGGER(logger, "SlackAPI");
 
		return; \
 
	} \
 
	std::string NAME = NAME##_tmp.GetString();
 

	
 
#define STORE_BOOL(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
 
	if (!NAME##_tmp.IsBool()) {  \
 
		LOG4CXX_ERROR(logger, "No '" << #NAME << "' string in the reply."); \
 
		LOG4CXX_ERROR(logger, data); \
 
		return; \
 
	} \
 
	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;
 
}
 

	
 
SlackAPI::~SlackAPI() {
 
}
 

	
 
void SlackAPI::handleSendMessage(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	LOG4CXX_INFO(logger, data);
 
}
 

	
 
@@ -263,24 +275,31 @@ void SlackAPI::getSlackUserInfo(HTTPRequest *req, bool ok, rapidjson::Document &
 

	
 
		STORE_STRING(users[i], id);
 
		info.id = id;
 

	
 
		STORE_STRING(users[i], name);
 
		info.name = name;
 

	
 
		STORE_BOOL(users[i], is_primary_owner);
 
		info.isPrimaryOwner = is_primary_owner;
 

	
 
		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;
 
}
 

	
 
std::string SlackAPI::SlackObjectToPlainText(const std::string &object, bool isChannel, bool returnName) {
 
	std::string ret = object;
 
	if (isChannel) {
 
		if (ret[0] == '<') {
 
			ret = ret.substr(2, ret.size() - 3);
 
		}
 
	} else {
spectrum/src/frontends/slack/SlackRTM.cpp
Show inline comments
 
@@ -66,40 +66,59 @@ SlackRTM::~SlackRTM() {
 
	delete m_api;
 
	m_pingTimer->stop();
 
}
 

	
 
#define STORE_STRING(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
 
	if (!NAME##_tmp.IsString()) {  \
 
		LOG4CXX_ERROR(logger, "No '" << #NAME << "' string in the reply."); \
 
		LOG4CXX_ERROR(logger, payload); \
 
		return; \
 
	} \
 
	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()) {
 
		LOG4CXX_ERROR(logger, "Error while parsing JSON");
 
		LOG4CXX_ERROR(logger, payload);
 
		return;
 
	}
 

	
 
	STORE_STRING(d, type);
 

	
 
	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);
 
		}
 
	}
 
}
 

	
 
void SlackRTM::sendMessage(const std::string &channel, const std::string &message) {
 
	m_counter++;
 

	
 
	std::string m = message;
 
	boost::replace_all(m, "\"", "\\\"");
 
	std::string msg = "{\"id\": " + boost::lexical_cast<std::string>(m_counter) + ", \"type\": \"message\", \"channel\":\"" + channel + "\", \"text\":\"" + m + "\"}";
 
	m_client->write(msg);
 
}
 

	
0 comments (0 inline, 0 general)