2024-02-20 12:02:02 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "data/business/data_business_chatbots.h"
|
|
|
|
|
2024-03-05 16:52:14 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "data/business/data_business_common.h"
|
|
|
|
#include "data/business/data_business_info.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_user.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
|
2024-02-20 12:02:02 +00:00
|
|
|
namespace Data {
|
|
|
|
|
2024-03-05 16:52:14 +00:00
|
|
|
Chatbots::Chatbots(not_null<Session*> owner)
|
|
|
|
: _owner(owner) {
|
2024-02-20 12:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Chatbots::~Chatbots() = default;
|
|
|
|
|
2024-03-05 16:52:14 +00:00
|
|
|
void Chatbots::preload() {
|
|
|
|
if (_loaded || _requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_requestId = _owner->session().api().request(
|
|
|
|
MTPaccount_GetConnectedBots()
|
|
|
|
).done([=](const MTPaccount_ConnectedBots &result) {
|
|
|
|
_requestId = 0;
|
|
|
|
_loaded = true;
|
|
|
|
|
|
|
|
const auto &data = result.data();
|
|
|
|
_owner->processUsers(data.vusers());
|
|
|
|
const auto &list = data.vconnected_bots().v;
|
|
|
|
if (!list.isEmpty()) {
|
|
|
|
const auto &bot = list.front().data();
|
|
|
|
const auto botId = bot.vbot_id().v;
|
|
|
|
_settings = ChatbotsSettings{
|
|
|
|
.bot = _owner->session().data().user(botId),
|
|
|
|
.recipients = FromMTP(_owner, bot.vrecipients()),
|
|
|
|
.repliesAllowed = bot.is_can_reply(),
|
|
|
|
};
|
2024-03-08 08:28:53 +00:00
|
|
|
} else {
|
|
|
|
_settings.force_assign(ChatbotsSettings());
|
2024-03-05 16:52:14 +00:00
|
|
|
}
|
2024-03-08 08:28:53 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
_requestId = 0;
|
|
|
|
LOG(("API Error: Could not get connected bots %1 (%2)"
|
|
|
|
).arg(error.code()
|
|
|
|
).arg(error.type()));
|
2024-03-05 16:52:14 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2024-03-08 08:28:53 +00:00
|
|
|
bool Chatbots::loaded() const {
|
|
|
|
return _loaded;
|
|
|
|
}
|
|
|
|
|
2024-02-20 12:02:02 +00:00
|
|
|
const ChatbotsSettings &Chatbots::current() const {
|
|
|
|
return _settings.current();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<ChatbotsSettings> Chatbots::changes() const {
|
|
|
|
return _settings.changes();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<ChatbotsSettings> Chatbots::value() const {
|
|
|
|
return _settings.value();
|
|
|
|
}
|
|
|
|
|
2024-03-05 16:52:14 +00:00
|
|
|
void Chatbots::save(
|
|
|
|
ChatbotsSettings settings,
|
|
|
|
Fn<void()> done,
|
|
|
|
Fn<void(QString)> fail) {
|
|
|
|
const auto was = _settings.current();
|
|
|
|
if (was == settings) {
|
|
|
|
return;
|
|
|
|
} else if (was.bot || settings.bot) {
|
|
|
|
using Flag = MTPaccount_UpdateConnectedBot::Flag;
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
api->request(MTPaccount_UpdateConnectedBot(
|
|
|
|
MTP_flags(!settings.bot
|
|
|
|
? Flag::f_deleted
|
|
|
|
: settings.repliesAllowed
|
|
|
|
? Flag::f_can_reply
|
|
|
|
: Flag()),
|
|
|
|
(settings.bot ? settings.bot : was.bot)->inputUser,
|
2024-03-18 11:02:12 +00:00
|
|
|
ForBotsToMTP(settings.recipients)
|
2024-03-05 16:52:14 +00:00
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
api->applyUpdates(result);
|
|
|
|
if (done) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
_settings = was;
|
|
|
|
if (fail) {
|
|
|
|
fail(error.type());
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
2024-02-20 12:02:02 +00:00
|
|
|
_settings = settings;
|
|
|
|
}
|
|
|
|
|
2024-03-18 11:02:12 +00:00
|
|
|
void Chatbots::togglePaused(not_null<PeerData*> peer, bool paused) {
|
|
|
|
const auto type = paused
|
|
|
|
? SentRequestType::Pause
|
|
|
|
: SentRequestType::Unpause;
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
const auto i = _sentRequests.find(peer);
|
|
|
|
if (i != end(_sentRequests)) {
|
|
|
|
const auto already = i->second.type;
|
|
|
|
if (already == SentRequestType::Remove || already == type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
api->request(i->second.requestId).cancel();
|
|
|
|
_sentRequests.erase(i);
|
|
|
|
}
|
|
|
|
const auto id = api->request(MTPaccount_ToggleConnectedBotPaused(
|
|
|
|
peer->input,
|
|
|
|
MTP_bool(paused)
|
|
|
|
)).done([=] {
|
|
|
|
if (_sentRequests[peer].type != type) {
|
|
|
|
return;
|
|
|
|
} else if (const auto settings = peer->barSettings()) {
|
|
|
|
peer->setBarSettings(paused
|
|
|
|
? (*settings | PeerBarSetting::BusinessBotPaused)
|
|
|
|
: (*settings & ~PeerBarSetting::BusinessBotPaused));
|
|
|
|
} else {
|
|
|
|
api->requestPeerSettings(peer);
|
|
|
|
}
|
|
|
|
_sentRequests.remove(peer);
|
|
|
|
}).fail([=] {
|
|
|
|
if (_sentRequests[peer].type != type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
api->requestPeerSettings(peer);
|
|
|
|
_sentRequests.remove(peer);
|
|
|
|
}).send();
|
|
|
|
_sentRequests[peer] = SentRequest{ type, id };
|
|
|
|
}
|
|
|
|
|
|
|
|
void Chatbots::removeFrom(not_null<PeerData*> peer) {
|
|
|
|
const auto type = SentRequestType::Remove;
|
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
const auto i = _sentRequests.find(peer);
|
|
|
|
if (i != end(_sentRequests)) {
|
|
|
|
const auto already = i->second.type;
|
|
|
|
if (already == type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
api->request(i->second.requestId).cancel();
|
|
|
|
_sentRequests.erase(i);
|
|
|
|
}
|
|
|
|
const auto id = api->request(MTPaccount_DisablePeerConnectedBot(
|
|
|
|
peer->input
|
|
|
|
)).done([=] {
|
|
|
|
if (_sentRequests[peer].type != type) {
|
|
|
|
return;
|
|
|
|
} else if (const auto settings = peer->barSettings()) {
|
|
|
|
peer->clearBusinessBot();
|
|
|
|
} else {
|
|
|
|
api->requestPeerSettings(peer);
|
|
|
|
}
|
|
|
|
_sentRequests.remove(peer);
|
2024-03-20 12:58:52 +00:00
|
|
|
reload();
|
2024-03-18 11:02:12 +00:00
|
|
|
}).fail([=] {
|
|
|
|
api->requestPeerSettings(peer);
|
|
|
|
_sentRequests.remove(peer);
|
|
|
|
}).send();
|
|
|
|
_sentRequests[peer] = SentRequest{ type, id };
|
|
|
|
}
|
|
|
|
|
2024-03-20 12:58:52 +00:00
|
|
|
void Chatbots::reload() {
|
|
|
|
_loaded = false;
|
|
|
|
_owner->session().api().request(base::take(_requestId)).cancel();
|
|
|
|
preload();
|
|
|
|
}
|
|
|
|
|
2024-02-20 12:02:02 +00:00
|
|
|
} // namespace Data
|