#include "mologram/ui/dialogs/ExtrasPanels.hpp" #include #include #include "mologram/ui/dialogs/DialogUtil.hpp" #include "mologram/ui/dialogs/PeopleDialogs.hpp" namespace mologram::ui { using namespace ttgui; namespace { /// State of one embedded panel: the holder is rebuilt when the sub-section changes, `gen` drops answers that arrive after that. struct Sec { UiContext* c = nullptr; Window* win = nullptr; std::shared_ptr alive; VBox* holder = nullptr; int gen = 0; }; bool live(const std::shared_ptr& st, int gen) { return st->alive && *st->alive && st->gen == gen; } std::string trim(const std::string& s) { size_t a = s.find_first_not_of(" \t"), b = s.find_last_not_of(" \t"); return a == std::string::npos ? "" : s.substr(a, b - a + 1); } js::Value recipients(bool existing, bool fresh, bool contacts, bool nonContacts) { js::Object r; r["existing_chats"] = js::Value(existing); r["new_chats"] = js::Value(fresh); r["contacts"] = js::Value(contacts); r["non_contacts"] = js::Value(nonContacts); return js::Value(std::move(r)); } /// Runs a Business write: Premium gate first (lock + calm panel without Premium), then the request, then `done`. void businessWrite(UiContext& c, const char* what, const char* method, js::Object params, std::function done) { UiContext* cp = &c; c.premium.use("business", [cp, what, method, params, done]() { cp->client.request(what, method, params, [cp, done](const RpcResult&) { if (cp->toast) cp->toast("Business", "Saved."); if (done) done(); }, [cp, what](const RpcError& e) { // Premium may have lapsed since the last refresh (FR-64): flip the state and explain, nothing else is lost. if (e.code == ErrorCode::PremiumRequired) cp->premium.refused("business"); else if (cp->client.onError) cp->client.onError(what, e); }); }); } // -------------------------------------------------------------------------------- Business sub-sections void buildQuick(std::shared_ptr st, DialogKit::Form& f, int gen) { UiContext& c = *st->c; UiContext* cp = &c; auto* table = c.kit.table(f, {"Shortcut", "Messages", "Id"}, {}, {22, 10, 8}); auto ids = std::make_shared>(); auto reload = std::make_shared>(); *reload = [st, gen, table, ids, cp]() { cp->client.request("load quick replies", "business.quick_replies", {}, [st, gen, table, ids](const RpcResult& r) { if (!live(st, gen)) return; ids->clear(); std::vector> rows; if (const js::Array* a = js::getArr(r.value, "replies")) for (const auto& q : *a) { ids->push_back(js::getInt(q, "id")); rows.push_back({"/" + js::getStr(q, "shortcut"), std::to_string(js::getInt(q, "count")), std::to_string(js::getInt(q, "id"))}); } table->rows = rows; }); }; auto* shortcut = c.kit.edit(f, "Shortcut (without /)"); auto* text = c.kit.edit(f, "Message text"); auto* row = c.kit.row(f); c.kit.rowButton(row, c.premium.label("Add", c.cfg.flag("ascii_mode")), [cp, shortcut, text, reload]() { if (trim(shortcut->text).empty() || trim(text->text).empty()) return; js::Object p; p["shortcut"] = js::Value(trim(shortcut->text)); p["text"] = js::Value(text->text); businessWrite(*cp, "add a quick reply", "business.quick_reply_add", p, [reload, text]() { text->text.clear(); (*reload)(); }); }); c.kit.rowButton(row, "Delete", [cp, table, ids, reload]() { int i = table->selected; if (i < 0 || i >= (int)ids->size()) return; js::Object p; p["id"] = js::Value((*ids)[(size_t)i]); businessWrite(*cp, "delete a quick reply", "business.quick_reply_delete", p, [reload]() { (*reload)(); }); }); c.kit.rowButton(row, "Send to open chat", [cp, table, ids]() { int i = table->selected; ChatSession* s = cp->activeSession ? cp->activeSession() : nullptr; if (i < 0 || i >= (int)ids->size() || !s || !s->isOpen()) { if (cp->toast) cp->toast("Quick reply", "Select a reply and open a chat first."); return; } js::Object p; p["peer"] = s->peer().toJson(); p["id"] = js::Value((*ids)[(size_t)i]); businessWrite(*cp, "send a quick reply", "business.quick_reply_send", p, nullptr); }); (*reload)(); } /// Greeting and away message share the shape: a quick reply, recipients and a few options. void buildAutoReply(std::shared_ptr st, DialogKit::Form& f, int gen, bool away) { UiContext& c = *st->c; UiContext* cp = &c; const char* key = away ? "away" : "greeting"; const char* method = away ? "business.away_set" : "business.greeting_set"; auto* current = c.kit.label(f, "Current: loading..."); c.kit.label(f, away ? "Sent when you are away. Reply to send (quick reply shortcut):" : "Sent to new chats. Reply to send (quick reply shortcut):"); auto* reply = c.kit.combo(f, {}, 0); auto replyIds = std::make_shared>(); auto* days = away ? static_cast(nullptr) : c.kit.edit(f, "Days without activity (default 7)"); ComboBox* schedule = away ? c.kit.combo(f, {"Always", "Outside work hours"}, 0) : nullptr; CheckBox* offline = away ? c.kit.check(f, "Only when I am offline", false) : nullptr; auto* ex = c.kit.check(f, "Existing chats", true); auto* nw = c.kit.check(f, "New chats", true); auto* co = c.kit.check(f, "Contacts", true); auto* nc = c.kit.check(f, "Non-contacts", true); auto* row = c.kit.row(f); c.kit.rowButton(row, c.premium.label("Save", c.cfg.flag("ascii_mode")), [cp, key, method, reply, replyIds, days, schedule, offline, ex, nw, co, nc, away]() { int i = reply->selected; if (i < 0 || i >= (int)replyIds->size()) { if (cp->toast) cp->toast("Business", "Create a quick reply first (Quick replies section)."); return; } js::Object v; v["shortcut_id"] = js::Value((*replyIds)[(size_t)i]); v["recipients"] = recipients(ex->checked, nw->checked, co->checked, nc->checked); if (away) { v["schedule"] = js::Value(schedule && schedule->selected == 1 ? "outside_hours" : "always"); v["offline_only"] = js::Value(offline && offline->checked); } else { int d = days ? std::atoi(days->text.c_str()) : 0; v["no_activity_days"] = js::Value(d > 0 ? d : 7); } js::Object p; p[key] = js::Value(std::move(v)); businessWrite(*cp, "save the message", method, p, nullptr); }); c.kit.rowButton(row, "Clear", [cp, method]() { businessWrite(*cp, "clear the message", method, {}, nullptr); }); cp->client.request("load business settings", "business.get", {}, [st, gen, current, key](const RpcResult& r) { if (!live(st, gen)) return; const js::Value* v = r.value.find(key); current->text = "Current: " + BusinessPanel::describe(v ? *v : js::Value()); }); cp->client.request("load quick replies", "business.quick_replies", {}, [st, gen, reply, replyIds](const RpcResult& r) { if (!live(st, gen)) return; reply->items.clear(); replyIds->clear(); if (const js::Array* a = js::getArr(r.value, "replies")) for (const auto& q : *a) { reply->items.push_back("/" + js::getStr(q, "shortcut")); replyIds->push_back(js::getInt(q, "id")); } reply->selected = 0; }); } void buildHours(std::shared_ptr st, DialogKit::Form& f, int gen) { UiContext& c = *st->c; UiContext* cp = &c; auto* current = c.kit.label(f, "Current: loading..."); auto* tz = c.kit.edit(f, "Time zone (for example Europe/Amsterdam)", "UTC"); auto* from = c.kit.edit(f, "Open from (HH:MM)", "09:00"); auto* to = c.kit.edit(f, "Open until (HH:MM)", "17:00"); c.kit.label(f, "Applies Monday to Friday."); auto* note = c.kit.label(f, ""); auto* row = c.kit.row(f); c.kit.rowButton(row, c.premium.label("Save", c.cfg.flag("ascii_mode")), [cp, tz, from, to, note]() { int a = BusinessPanel::parseMinute(from->text), b = BusinessPanel::parseMinute(to->text); if (a < 0 || b < 0 || b <= a || trim(tz->text).empty()) { note->text = "Use times like 09:00 and 17:30, and a later closing time."; return; } note->text.clear(); js::Object p; p["hours"] = BusinessPanel::weeklyHours(trim(tz->text), a, b); businessWrite(*cp, "save the work hours", "business.hours_set", p, nullptr); }); c.kit.rowButton(row, "Clear", [cp]() { businessWrite(*cp, "clear the work hours", "business.hours_set", {}, nullptr); }); cp->client.request("load business settings", "business.get", {}, [st, gen, current](const RpcResult& r) { if (!live(st, gen)) return; const js::Value* v = r.value.find("hours"); current->text = "Current: " + BusinessPanel::describe(v ? *v : js::Value()); }); } void buildLocation(std::shared_ptr st, DialogKit::Form& f, int gen) { UiContext& c = *st->c; UiContext* cp = &c; auto* current = c.kit.label(f, "Current: loading..."); auto* address = c.kit.edit(f, "Address"); auto* row = c.kit.row(f); c.kit.rowButton(row, c.premium.label("Save", c.cfg.flag("ascii_mode")), [cp, address]() { if (trim(address->text).empty()) return; js::Object loc; loc["address"] = js::Value(trim(address->text)); js::Object p; p["location"] = js::Value(std::move(loc)); businessWrite(*cp, "save the location", "business.location_set", p, nullptr); }); c.kit.rowButton(row, "Clear", [cp]() { businessWrite(*cp, "clear the location", "business.location_set", {}, nullptr); }); cp->client.request("load business settings", "business.get", {}, [st, gen, current](const RpcResult& r) { if (!live(st, gen)) return; const js::Value* v = r.value.find("location"); current->text = "Current: " + BusinessPanel::describe(v ? *v : js::Value()); }); } void buildIntro(std::shared_ptr st, DialogKit::Form& f, int gen) { UiContext& c = *st->c; UiContext* cp = &c; auto* current = c.kit.label(f, "Current: loading..."); auto* title = c.kit.edit(f, "Title"); auto* desc = c.kit.edit(f, "Description"); auto* row = c.kit.row(f); c.kit.rowButton(row, c.premium.label("Save", c.cfg.flag("ascii_mode")), [cp, title, desc]() { js::Object i; i["title"] = js::Value(trim(title->text)); i["description"] = js::Value(desc->text); js::Object p; p["intro"] = js::Value(std::move(i)); businessWrite(*cp, "save the intro", "business.intro_set", p, nullptr); }); c.kit.rowButton(row, "Clear", [cp]() { businessWrite(*cp, "clear the intro", "business.intro_set", {}, nullptr); }); cp->client.request("load business settings", "business.get", {}, [st, gen, current](const RpcResult& r) { if (!live(st, gen)) return; const js::Value* v = r.value.find("intro"); current->text = "Current: " + BusinessPanel::describe(v ? *v : js::Value()); }); } void buildBots(std::shared_ptr st, DialogKit::Form& f, int gen) { UiContext& c = *st->c; UiContext* cp = &c; auto* table = c.kit.table(f, {"Connected chatbots"}, {}, {50}); auto reload = std::make_shared>(); *reload = [st, gen, table, cp]() { cp->client.request("load chatbots", "business.bots", {}, [st, gen, table](const RpcResult& r) { if (!live(st, gen)) return; std::vector> rows; if (const js::Array* a = js::getArr(r.value, "bots")) for (const auto& b : *a) rows.push_back({BusinessPanel::describe(b)}); table->rows = rows; }); }; auto* bot = c.kit.edit(f, "Bot (@username)"); auto* row = c.kit.row(f); c.kit.rowButton(row, c.premium.label("Connect", c.cfg.flag("ascii_mode")), [cp, bot, reload]() { if (trim(bot->text).empty()) return; js::Object rights; rights["reply"] = js::Value(true); js::Object p; p["bot"] = js::Value(trim(bot->text)); p["recipients"] = recipients(true, true, true, true); p["rights"] = js::Value(std::move(rights)); businessWrite(*cp, "connect the chatbot", "business.bot_set", p, [reload]() { (*reload)(); }); }); c.kit.rowButton(row, "Disconnect", [cp, bot, reload]() { if (trim(bot->text).empty()) return; js::Object p; p["bot"] = js::Value(trim(bot->text)); p["recipients"] = recipients(false, false, false, false); p["remove"] = js::Value(true); businessWrite(*cp, "disconnect the chatbot", "business.bot_set", p, [reload]() { (*reload)(); }); }); (*reload)(); } void buildLinks(std::shared_ptr st, DialogKit::Form& f, int gen) { UiContext& c = *st->c; UiContext* cp = &c; auto* table = c.kit.table(f, {"Link", "Title", "Message"}, {}, {16, 16, 28}); auto slugs = std::make_shared>(); auto reload = std::make_shared>(); *reload = [st, gen, table, slugs, cp]() { cp->client.request("load chat links", "business.chat_links", {}, [st, gen, table, slugs](const RpcResult& r) { if (!live(st, gen)) return; slugs->clear(); std::vector> rows; if (const js::Array* a = js::getArr(r.value, "links")) for (const auto& l : *a) { slugs->push_back(js::getStr(l, "slug")); rows.push_back({js::getStr(l, "slug"), js::getStr(l, "title"), js::getStr(l, "message")}); } table->rows = rows; }); }; auto* msg = c.kit.edit(f, "Prefilled message"); auto* title = c.kit.edit(f, "Title (optional)"); auto* row = c.kit.row(f); c.kit.rowButton(row, c.premium.label("Create", c.cfg.flag("ascii_mode")), [cp, msg, title, reload]() { if (trim(msg->text).empty()) return; js::Object p; p["message"] = js::Value(msg->text); if (!trim(title->text).empty()) p["title"] = js::Value(trim(title->text)); businessWrite(*cp, "create the link", "business.chat_link_create", p, [reload]() { (*reload)(); }); }); c.kit.rowButton(row, "Delete", [cp, table, slugs, reload]() { int i = table->selected; if (i < 0 || i >= (int)slugs->size()) return; js::Object p; p["slug"] = js::Value((*slugs)[(size_t)i]); businessWrite(*cp, "delete the link", "business.chat_link_delete", p, [reload]() { (*reload)(); }); }); (*reload)(); } void showBusiness(std::shared_ptr st, int idx) { int gen = ++st->gen; st->holder->clearChildren(); auto* sec = new VBox(); sec->flex = 1; DialogKit::Form f{st->win, sec}; switch (idx) { case 0: buildQuick(st, f, gen); break; case 1: buildAutoReply(st, f, gen, false); break; case 2: buildAutoReply(st, f, gen, true); break; case 3: buildHours(st, f, gen); break; case 4: buildLocation(st, f, gen); break; case 5: buildIntro(st, f, gen); break; case 6: buildBots(st, f, gen); break; default: buildLinks(st, f, gen); break; } st->holder->addChild(std::unique_ptr(sec)); st->win->layout(); } } // namespace // ------------------------------------------------------------------------------------------------ BusinessPanel const std::vector& BusinessPanel::sections() { static const std::vector v = {"Quick replies", "Greeting message", "Away message", "Work hours", "Location", "Intro", "Chatbots", "Chat links"}; return v; } int BusinessPanel::parseMinute(const std::string& s) { int h = 0, m = 0; char tail = 0; if (std::sscanf(s.c_str(), "%d:%d%c", &h, &m, &tail) != 2) return -1; if (h < 0 || h > 24 || m < 0 || m > 59 || (h == 24 && m != 0)) return -1; return h * 60 + m; } js::Value BusinessPanel::weeklyHours(const std::string& timezone, int from, int to) { js::Object o; o["timezone_id"] = js::Value(timezone); js::Array weekly; for (int d = 0; d < 5; d++) { js::Object w; w["start"] = js::Value(d * 1440 + from); w["end"] = js::Value(d * 1440 + to); weekly.push_back(js::Value(std::move(w))); } o["weekly"] = js::Value(std::move(weekly)); return js::Value(std::move(o)); } std::string BusinessPanel::describe(const js::Value& v) { if (v.isNull()) return "not set"; if (v.isObj() && v.asObj()->empty()) return "not set"; std::string s = ttgui::json::dump(v); while (!s.empty() && (s.back() == '\n' || s.back() == ' ')) s.pop_back(); return s; } void BusinessPanel::build(UiContext& c, DialogKit::Form& f) { bool ascii = c.cfg.flag("ascii_mode"); c.kit.label(f, c.premium.has() ? "Telegram Business" : c.premium.label("Telegram Business needs Premium; you can look, not change", ascii)); auto st = std::make_shared(); st->c = &c; st->win = f.win; st->alive = dlg::track(f.win); auto* combo = c.kit.combo(f, sections(), 0); st->holder = new VBox(); st->holder->flex = 1; f.box->addChild(std::unique_ptr(st->holder)); combo->onSelected.connect([st](int i) { showBusiness(st, i); }); showBusiness(st, 0); } Window* BusinessPanel::open(UiContext& c) { auto f = c.kit.open("Telegram Business", "BusinessDialog", 74, 24); build(c, f); UiContext* cp = &c; Window* win = f.win; auto* r = c.kit.row(f); c.kit.rowButton(r, c.t("btn.close"), [cp, win]() { cp->kit.close(win); }); c.kit.show(f, nullptr); return f.win; } // ------------------------------------------------------------------------------------------------ GiftsPanel void GiftsPanel::build(UiContext& c, DialogKit::Form& f) { UiContext* cp = &c; bool ascii = c.cfg.flag("ascii_mode"); auto alive = dlg::track(f.win); auto* stars = c.kit.label(f, "Stars balance: loading..."); auto* table = c.kit.table(f, {"Gift", "Stars"}, {}, {20, 14}); auto ids = std::make_shared>(); auto prices = std::make_shared>(); auto* note = c.kit.label(f, "Pick a gift, then Send. A gift costs Stars and you confirm the price first."); auto* msg = c.kit.edit(f, "Message with the gift (optional)"); auto* anon = c.kit.check(f, "Hide my name", false); auto* row = c.kit.row(f); auto pending = std::make_shared>(); c.kit.rowButton(row, c.premium.label("Send to a person", ascii), [cp, table, ids, prices, msg, anon, note, pending, alive]() { int i = table->selected; if (i < 0 || i >= (int)ids->size()) { note->text = "Select a gift first."; return; } std::string gift = (*ids)[(size_t)i], price = (*prices)[(size_t)i]; cp->premium.use("gifts", [cp, gift, price, msg, anon, note, alive]() { ContactsDialog::pick(*cp, "Send the gift to", [cp, gift, price, msg, anon, note, alive](const Peer& who) { // The price is shown and the second button spends the Stars: nothing is charged by a single click. auto f2 = cp->kit.open("Confirm gift", "GiftConfirm", 50, 10); cp->kit.label(f2, "This sends the gift and spends " + price + " Stars."); Window* w2 = f2.win; std::string text = msg->text; bool hide = anon->checked; auto* r2 = cp->kit.row(f2); cp->kit.rowButton(r2, "Spend " + price + " Stars", [cp, gift, who, text, hide, w2, note, alive]() { js::Object p; p["peer"] = who.toJson(); p["gift"] = js::Value(gift); p["confirm"] = js::Value(true); if (!text.empty()) p["message"] = js::Value(text); p["hide_name"] = js::Value(hide); cp->kit.close(w2); cp->client.request("send the gift", "gifts.send", std::move(p), [cp, note, alive](const RpcResult&) { if (cp->toast) cp->toast("Gifts", "Gift sent."); if (alive && *alive) note->text = "Gift sent."; }); }); cp->kit.rowButton(r2, "Cancel", [cp, w2]() { cp->kit.close(w2); }); cp->kit.show(f2, nullptr); }); }); }); cp->client.request("load the gift catalogue", "gifts.list", {}, [table, ids, prices, alive](const RpcResult& r) { if (!alive || !*alive) return; ids->clear(); prices->clear(); std::vector> rows; if (const js::Array* a = js::getArr(r.value, "gifts")) for (const auto& g : *a) { std::string id = js::getStr(g, "id"), st2 = js::getStr(g, "stars"); ids->push_back(id); prices->push_back(st2); rows.push_back({"Gift " + id, st2}); } table->rows = rows; }); cp->client.request("load the Stars balance", "gifts.stars", {}, [stars, alive](const RpcResult& r) { if (!alive || !*alive) return; stars->text = "Stars balance: " + js::getStr(r.value, "balance"); }); } Window* GiftsPanel::open(UiContext& c) { auto f = c.kit.open("Gifts", "GiftsDialog", 66, 22); build(c, f); UiContext* cp = &c; Window* win = f.win; auto* r = c.kit.row(f); c.kit.rowButton(r, c.t("btn.close"), [cp, win]() { cp->kit.close(win); }); c.kit.show(f, nullptr); return f.win; } // ------------------------------------------------------------------------------------------------ PassportPanel void PassportPanel::build(UiContext& c, DialogKit::Form& f) { UiContext* cp = &c; auto alive = dlg::track(f.win); c.kit.label(f, "Telegram Passport (read only): documents you stored in Telegram."); auto* table = c.kit.table(f, {"Document"}, {}, {60}); cp->client.request("load Passport", "passport.values", {}, [table, alive](const RpcResult& r) { if (!alive || !*alive) return; std::vector> rows; if (const js::Array* a = js::getArr(r.value, "values")) for (const auto& v : *a) rows.push_back({BusinessPanel::describe(v)}); if (rows.empty()) rows.push_back({"No documents stored."}); table->rows = rows; }); c.kit.label(f, "Payments (display only, nothing is charged): chat and invoice message id"); auto* peer = c.kit.edit(f, "Chat (for example user:123)"); auto* id = c.kit.edit(f, "Message id"); auto* out = c.kit.text(f, "", 1); auto* row = c.kit.row(f); auto show = [cp, peer, id, out, alive](const char* what, const char* method) { Peer p = Peer::fromKey(trim(peer->text)); if (!p.valid() || trim(id->text).empty()) { out->text = "Enter a chat like user:123 and the invoice message id."; return; } js::Object params; params["peer"] = p.toJson(); params["id"] = js::Value(std::atoi(trim(id->text).c_str())); // message ids are 32 bit numbers here cp->client.request(what, method, std::move(params), [out, alive](const RpcResult& r) { if (alive && *alive) out->text = BusinessPanel::describe(r.value); }); }; c.kit.rowButton(row, "Show form", [show]() { show("load the payment form", "payments.form"); }); c.kit.rowButton(row, "Show receipt", [show]() { show("load the receipt", "payments.receipt"); }); } Window* PassportPanel::open(UiContext& c) { auto f = c.kit.open("Passport and payments", "PassportDialog", 70, 22); build(c, f); UiContext* cp = &c; Window* win = f.win; auto* r = c.kit.row(f); c.kit.rowButton(r, c.t("btn.close"), [cp, win]() { cp->kit.close(win); }); c.kit.show(f, nullptr); return f.win; } } // namespace mologram::ui