Changeset - bf9f7b7a4163
[Not reviewed]
0 1 0
Jan Kaluza - 10 years ago 2015-12-08 18:47:19
jkaluza@redhat.com
Slack: parse the bots names from RTM
1 file changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
spectrum/src/frontends/slack/SlackAPI.cpp
Show inline comments
 
@@ -113,231 +113,231 @@ void SlackAPI::setPurpose(const std::string &channel, const std::string &purpose
 
	url += "&token=" + Util::urlencode(m_token);
 

	
 
	HTTPRequest *req = new HTTPRequest(THREAD_POOL(m_component), HTTPRequest::Get, url,
 
			boost::bind(&SlackAPI::handleSendMessage, this, _1, _2, _3, _4));
 
	queueRequest(req);
 
}
 

	
 
std::string SlackAPI::getChannelId(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		LOG4CXX_ERROR(logger, data);
 
		return "";
 
	}
 

	
 
	rapidjson::Value &channel = resp["channel"];
 
	if (!channel.IsObject()) {
 
		LOG4CXX_ERROR(logger, "No 'channel' object in the reply.");
 
		LOG4CXX_ERROR(logger, data);
 
		return "";
 
	}
 

	
 
	rapidjson::Value &id = channel["id"];
 
	if (!id.IsString()) {
 
		LOG4CXX_ERROR(logger, "No 'id' string in the reply.");
 
		LOG4CXX_ERROR(logger, data);
 
		return "";
 
	}
 

	
 
	return id.GetString();
 
}
 

	
 
void SlackAPI::channelsCreate(const std::string &name, HTTPRequest::Callback callback) {
 
	std::string url = "https://slack.com/api/channels.create?name=" + Util::urlencode(name) + "&token=" + Util::urlencode(m_token);
 
	HTTPRequest *req = new HTTPRequest(THREAD_POOL(m_component), HTTPRequest::Get, url, callback);
 
	queueRequest(req);
 
}
 

	
 
void SlackAPI::imOpen(const std::string &uid, HTTPRequest::Callback callback) {
 
	std::string url = "https://slack.com/api/im.open?user=" + Util::urlencode(uid) + "&token=" + Util::urlencode(m_token);
 
	HTTPRequest *req = new HTTPRequest(THREAD_POOL(m_component), HTTPRequest::Get, url, callback);
 
	queueRequest(req);
 
}
 

	
 
std::string SlackAPI::getOwnerId(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		return "";
 
	}
 

	
 
	rapidjson::Value &members = resp["members"];
 
	if (!members.IsArray()) {
 
		LOG4CXX_ERROR(logger, "No 'members' object in the reply.");
 
		return "";
 
	}
 

	
 
	for (int i = 0; i < members.Size(); i++) {
 
		if (!members[i].IsObject()) {
 
			continue;
 
		}
 

	
 
		rapidjson::Value &is_primary_owner = members[i]["is_primary_owner"];
 
		if (!is_primary_owner.IsBool()) {
 
			continue;
 
		}
 

	
 
		if (is_primary_owner.GetBool()) {
 
			rapidjson::Value &name = members[i]["id"];
 
			if (!name.IsString()) {
 
				LOG4CXX_ERROR(logger, "No 'name' string in the reply.");
 
				return "";
 
			}
 
			return name.GetString();
 
		}
 
	}
 

	
 
	return "";
 
}
 

	
 
void SlackAPI::usersList(HTTPRequest::Callback callback) {
 
	std::string url = "https://slack.com/api/users.list?presence=0&token=" + Util::urlencode(m_token);
 
	HTTPRequest *req = new HTTPRequest(THREAD_POOL(m_component), HTTPRequest::Get, url, callback);
 
	queueRequest(req);
 
}
 

	
 
void SlackAPI::getSlackChannelInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map<std::string, SlackChannelInfo> &ret) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		return;
 
	}
 

	
 
	GET_ARRAY(resp, channels);
 

	
 
	for (int i = 0; i < channels.Size(); i++) {
 
		if (!channels[i].IsObject()) {
 
			continue;
 
		}
 

	
 
		SlackChannelInfo info;
 

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

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

	
 
		rapidjson::Value &members = channels[i]["members"];
 
		for (int y = 0; members.IsArray() && y < members.Size(); y++) {
 
			if (!members[y].IsString()) {
 
				continue;
 
			}
 

	
 
			info.members.push_back(members[y].GetString());
 
		}
 

	
 
		ret[info.name] = info;
 
	}
 

	
 
	return;
 
}
 

	
 
void SlackAPI::getSlackImInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map<std::string, SlackImInfo> &ret) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		return;
 
	}
 

	
 
	GET_ARRAY(resp, ims);
 

	
 
	for (int i = 0; i < ims.Size(); i++) {
 
		if (!ims[i].IsObject()) {
 
			continue;
 
		}
 

	
 
		SlackImInfo info;
 

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

	
 
		STORE_STRING(ims[i], user);
 
		info.user = user;
 

	
 
		ret[info.id] = info;
 
		LOG4CXX_INFO(logger, info.id << " " << info.user);
 
	}
 

	
 
	return;
 
}
 

	
 
void SlackAPI::getSlackUserInfo(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data, std::map<std::string, SlackUserInfo> &ret) {
 
	if (!ok) {
 
		LOG4CXX_ERROR(logger, req->getError());
 
		return;
 
	}
 

	
 
	GET_ARRAY(resp, users);
 

	
 
	for (int i = 0; i < users.Size(); i++) {
 
		if (!users[i].IsObject()) {
 
			continue;
 
		}
 

	
 
		SlackUserInfo info;
 

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

	
 
	GET_ARRAY(resp, bots);
 

	
 
	for (int i = 0; i < bots.Size(); i++) {
 
		if (!bots[i].IsObject()) {
 
			continue;
 
		}
 

	
 
		SlackUserInfo info;
 

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

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

	
 
		info.isPrimaryOwner = 0;
 

	
 
		ret[info.id] = info;
 
		LOG4CXX_INFO(logger, info.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 {
 
		if (ret[0] == '<') {
 
			ret = ret.substr(1, ret.size() - 2);
 
			if (returnName) {
 
				ret = ret.substr(0, ret.find("|"));
 
			}
 
			else {
 
				ret = ret.substr(ret.find("|") + 1);
 
			}
 
		}
 
	}
 

	
 
	return ret;
 
}
 

	
 

	
 

	
 
}
0 comments (0 inline, 0 general)