blob: f02b12103d740021359795ecdee20da21ccd839a [file] [log] [blame]
/**
* Copyright (C) 2020 Savoir-faire Linux Inc.
*
* Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "BotPeerChatSubscriber.h"
#include "pluglog.h"
const std::string TAG = "bot";
namespace jami {
BotPeerChatSubscriber::BotPeerChatSubscriber(const JAMI_PluginAPI* api,
const std::string& textAnswer,
const std::string& inText)
: api_ {api}
{
setAnswer(textAnswer);
setInText(inText);
}
BotPeerChatSubscriber::~BotPeerChatSubscriber()
{
std::ostringstream oss;
oss << "~botChatProcessor" << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
}
void
BotPeerChatSubscriber::setAnswer(const std::string& textAnswer)
{
textAnswer_ = textAnswer;
}
void
BotPeerChatSubscriber::setInText(const std::string& inText)
{
inText_ = inText;
}
void
BotPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessagePtr& message)
{
if (isAttached) {
if (message->direction) {
std::map<std::string, std::string> sendMsg;
for (auto& pair : message->data) {
if (pair.first == "text/plain" && pair.second == inText_) {
sendMsg[pair.first] = textAnswer_;
}
}
if (!sendMsg.empty()) {
sendText(message->accountId, message->peerId, sendMsg);
}
}
}
}
void
BotPeerChatSubscriber::attached(Observable<pluginMessagePtr>* observable)
{
if (observables_.find(observable) == observables_.end()) {
std::ostringstream oss;
oss << "::Attached ! " << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
observables_.insert(observable);
isAttached = true;
}
}
void
BotPeerChatSubscriber::detached(Observable<pluginMessagePtr>* observable)
{
auto it = observables_.find(observable);
if (it != observables_.end()) {
observables_.erase(it);
std::ostringstream oss;
oss << "::Detached()" << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
if (observables_.empty())
isAttached = false;
}
}
void
BotPeerChatSubscriber::sendText(std::string& accountId,
std::string& peerId,
std::map<std::string, std::string>& sendMsg)
{
pluginMessagePtr botAnswer = std::make_shared<JamiMessage>(accountId, peerId, false, sendMsg, true);
api_->invokeService(api_, "sendTextMessage", botAnswer.get());
}
} // namespace jami