From 1bf36dbf68fe9ab25d1ff373bfd5f40c9651e3cf Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Mon, 25 Jan 2016 08:24:41 +0100 Subject: [PATCH] Allow setting main channel using Slack command --- spectrum/src/frontends/slack/SlackSession.cpp | 24 +++++++++++++++++++ spectrum_manager/src/APIServer.cpp | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/spectrum/src/frontends/slack/SlackSession.cpp b/spectrum/src/frontends/slack/SlackSession.cpp index 7f961aca..a6874ee7 100644 --- a/spectrum/src/frontends/slack/SlackSession.cpp +++ b/spectrum/src/frontends/slack/SlackSession.cpp @@ -330,6 +330,20 @@ void SlackSession::handleMessageReceived(const std::string &channel, const std:: else if (args[1] == "register" && args.size() == 5) { handleRegisterMessage(message, args, quiet); } + else if (args[1] == "set_main_channel" && args.size() == 3) { + std::string slackChannel = SlackAPI::SlackObjectToPlainText(args[2], true); + + m_storageBackend->setUser(m_uinfo); + int type = (int) TYPE_STRING; + m_storageBackend->getUserSetting(m_uinfo.id, "slack_channel", type, slackChannel); + + Swift::Presence::ref presence = Swift::Presence::create(); + presence->setFrom(Swift::JID("", m_uinfo.jid, "default")); + presence->setTo(m_component->getJID()); + presence->setType(Swift::Presence::Available); + presence->addPayload(boost::shared_ptr(new Swift::MUCPayload())); + m_component->getFrontend()->onPresenceReceived(presence); + } else if (args[1] == "list.rooms" && args.size() == 2) { // .spectrum2 list.rooms std::string rooms = ""; @@ -425,6 +439,16 @@ void SlackSession::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document & presence->addPayload(boost::shared_ptr(new Swift::MUCPayload())); m_component->getFrontend()->onPresenceReceived(presence); } + else { + std::string msg; + msg = "Hi, it seems you have enabled Spectrum 2 transport for your Team. As a Team owner, you should now configure it:\\n"; + msg += "1. At first, create new channel in which you want this Spectrum 2 transport to send the messages, or choose the existing one.\\n"; + msg += "2. Invite this Spectrum 2 bot into this channel.\\n"; + msg += "3. Configure the transportation between 3rd-party network and this channel by executing following command in this chat:\\n"; + msg += "```.spectrum2 set_main_channel #SlackChannel```\\n"; + msg += "To get full list of available commands, executa `.spectrum2 help`\\n"; + m_rtm->sendMessage(m_ownerChannel, msg); + } } } diff --git a/spectrum_manager/src/APIServer.cpp b/spectrum_manager/src/APIServer.cpp index 5ea6e831..12b75c47 100644 --- a/spectrum_manager/src/APIServer.cpp +++ b/spectrum_manager/src/APIServer.cpp @@ -82,6 +82,7 @@ void APIServer::serve_instances(Server *server, Server::session *session, struct // the std::string stored out of BOOST_FOREACH scope, otherwise the // const char * returned by c_str() would be invalid during send_json. std::vector statuses; + std::vector usernames; std::vector list = show_list(m_config, false); Document json; @@ -115,8 +116,9 @@ void APIServer::serve_instances(Server *server, Server::session *session, struct int type = (int) TYPE_STRING; m_storage->getUserSetting(info.id, id, type, username); + usernames.push_back(username); instance.AddMember("registered", !username.empty(), json.GetAllocator()); - instance.AddMember("username", username.c_str(), json.GetAllocator()); + instance.AddMember("username", usernames.back().c_str(), json.GetAllocator()); instances.PushBack(instance, json.GetAllocator()); }