| /** |
| * 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, std::string& textAnswer) |
| : api_ {api} |
| , textAnswer_ {textAnswer} |
| {} |
| |
| botPeerChatSubscriber::~botPeerChatSubscriber() |
| { |
| std::ostringstream oss; |
| oss << "~botChatProcessor" << std::endl; |
| Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| } |
| |
| void |
| botPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessagePtr& message) |
| { |
| if (isAttached) { |
| if (message->direction == "0") { |
| std::map<std::string, std::string> sendMsg; |
| for (auto& pair : message->data) { |
| if (pair.first == "text/plain" && pair.second == "hi") { |
| std::ostringstream sendMsgStream; |
| sendMsgStream << "HelloWorld from bot plugin"; |
| sendMsg[pair.first] = sendMsgStream.str(); |
| } |
| } |
| 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) |
| { |
| if (observables_.find(observable) != observables_.end()) { |
| observables_.erase(observable); |
| 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, "0", sendMsg, true); |
| api_->invokeService(api_, "sendTextMessage", botAnswer.get()); |
| } |
| } // namespace jami |