- One message per job: the status message is deleted once the file is
sent, or shortened to "Sent." when Telegram keeps it.
- No reply quotes in private chats; groups keep them.
- Queue status only mentions the position when the wait is above
QUEUE_NOTICE_THRESHOLD_SECONDS or somebody is ahead. Status edits are
throttled to one per 2.5 s per message; a state that arrives too soon
is shown later instead of dropped. The wait estimate is 0 with a free
slot.
- Caption with title (uploader and date when generic), uploader,
duration, a short source link without tracking parameters and an
optional "via @bot" line (CAPTION_ATTRIBUTION). Uploaded videos get a
320 px JPEG thumbnail; merged MP4s are written with faststart.
- Buttons under the result: Audio (MP3), Other quality, Thumbnail,
Delete. Requester only in groups.
- Language resolved once per update and passed in the context. A
fallback to English is logged as a bug; a test walks every screen in
Spanish and Dutch. Language picker on the first /start.
- Sizes without a zero decimal ("50 MB").
- First link of a new user shows the card once and sets the default to
"best that fits my limit".
- Errors offer the next step (Try again, Smaller quality, Upgrade, Open
app); passing lookup failures are retried once.
- Up to MULTI_LINK_MAX links per message; a link sent a moment ago gets
"already sent" with a Send again button.
- Mini App menu button, labelled per private chat in the user's
language.
- Personal /settings in private chats with an "Add me to a group"
button.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
590 lines
20 KiB
Go
590 lines
20 KiB
Go
package bot
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/mymmrac/telego"
|
|
|
|
"github.com/noderunners/dlbot/internal/billing"
|
|
"github.com/noderunners/dlbot/internal/db"
|
|
"github.com/noderunners/dlbot/internal/groups"
|
|
"github.com/noderunners/dlbot/internal/i18n"
|
|
"github.com/noderunners/dlbot/internal/media"
|
|
"github.com/noderunners/dlbot/internal/service"
|
|
)
|
|
|
|
const (
|
|
cbGroupSetting = "gs" // gs:<key>
|
|
cbGroupBuy = "gb" // gb:<product>:<chat id>[:<amount>]
|
|
cbQuality = "ql" // ql:<profile or "ask">
|
|
cbLanguage = "lg" // lg:<language or "auto">
|
|
cbWelcomeLang = "lw" // lw:<language or "auto">, under the first welcome
|
|
)
|
|
|
|
// handleMyChatMember records the groups the bot is in. Telegram sends this
|
|
// update whenever the bot is added, removed, promoted or demoted.
|
|
func (b *Bot) handleMyChatMember(ctx context.Context, u *telego.ChatMemberUpdated) {
|
|
if !groups.IsGroup(u.Chat.Type) || u.NewChatMember == nil {
|
|
return
|
|
}
|
|
g := &db.Group{
|
|
ChatID: u.Chat.ID, Title: u.Chat.Title, Type: u.Chat.Type,
|
|
BotStatus: u.NewChatMember.MemberStatus(), AddedBy: u.From.ID,
|
|
}
|
|
if err := b.store.UpsertGroup(ctx, g); err != nil {
|
|
b.log.Error("record group", "chat_id", u.Chat.ID, "err", err)
|
|
return
|
|
}
|
|
b.admins.Forget(u.Chat.ID)
|
|
b.log.Info("group status changed", "chat_id", g.ChatID, "status", g.BotStatus, "by", u.From.ID)
|
|
}
|
|
|
|
// noteGroup makes sure a group the bot is used in is on record, also when
|
|
// the my_chat_member update was missed, and notes who uses it.
|
|
func (b *Bot) noteGroup(ctx context.Context, chat telego.Chat, userID int64) {
|
|
if _, err := b.store.GetGroup(ctx, chat.ID); err != nil {
|
|
_ = b.store.UpsertGroup(ctx, &db.Group{ChatID: chat.ID, Title: chat.Title, Type: chat.Type, BotStatus: "member"})
|
|
}
|
|
if err := b.store.SeeGroupUser(ctx, chat.ID, userID); err != nil {
|
|
b.log.Debug("note group user", "err", err)
|
|
}
|
|
}
|
|
|
|
// requireAdmin checks that a command was sent in a group by one of its
|
|
// admins, and tells the sender when it was not.
|
|
func (b *Bot) requireAdmin(ctx context.Context, m *telego.Message, act actor) bool {
|
|
if !groups.IsGroup(m.Chat.Type) {
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "notice.group_only", nil), nil)
|
|
return false
|
|
}
|
|
if !b.admins.IsAdmin(ctx, m.Chat.ID, act.user.ID) {
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "notice.admins_only", nil), nil)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (b *Bot) requireGroup(ctx context.Context, m *telego.Message, act actor) bool {
|
|
if groups.IsGroup(m.Chat.Type) {
|
|
return true
|
|
}
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "notice.group_only", nil), nil)
|
|
return false
|
|
}
|
|
|
|
func (b *Bot) onOff(lang string, on bool) string {
|
|
if on {
|
|
return b.tr.T(lang, "word.on", nil)
|
|
}
|
|
return b.tr.T(lang, "word.off", nil)
|
|
}
|
|
|
|
func (b *Bot) qualityName(lang, q string) string {
|
|
if q == "" {
|
|
return b.tr.T(lang, "quality.ask", nil)
|
|
}
|
|
p, err := media.Parse(q)
|
|
if err != nil {
|
|
return q
|
|
}
|
|
return b.profileLabel(lang, p)
|
|
}
|
|
|
|
// groupSettingsLink opens the full settings of a group: the Mini App page
|
|
// when the Mini App is set up, the private chat otherwise. Web app buttons
|
|
// do not work in groups, so this is always a plain link.
|
|
func (b *Bot) groupSettingsLink(chatID int64) string {
|
|
id := strconv.FormatInt(chatID, 10)
|
|
if b.cfg.MiniAppEnabled() {
|
|
return "https://t.me/" + b.username + "?startapp=group_" + id
|
|
}
|
|
return "https://t.me/" + b.username + "?start=gs_" + id
|
|
}
|
|
|
|
func (b *Bot) groupWalletLink(chatID int64) string {
|
|
return "https://t.me/" + b.username + "?start=gw_" + strconv.FormatInt(chatID, 10)
|
|
}
|
|
|
|
// settingsView renders the quick settings of a group.
|
|
func (b *Bot) settingsView(ctx context.Context, lang string, st *db.GroupSettings) (string, *telego.InlineKeyboardMarkup) {
|
|
wallet, _ := b.bill.State(ctx, db.OwnerGroup, st.ChatID)
|
|
pass := b.tr.T(lang, "word.none", nil)
|
|
if wallet.PassUntil > 0 {
|
|
pass = b.tr.DateTime(lang, time.UnixMilli(wallet.PassUntil))
|
|
}
|
|
groupLang := st.Lang
|
|
if groupLang == "" {
|
|
groupLang = b.tr.T(lang, "settings.lang_auto", nil)
|
|
}
|
|
sites := func(list string) string {
|
|
if list == "" {
|
|
return b.tr.T(lang, "word.none", nil)
|
|
}
|
|
return strings.Join(db.Sites(list), ", ")
|
|
}
|
|
text := b.tr.HTML(lang, "settings.text", i18n.Args{
|
|
"Balance": b.tr.Sats(lang, wallet.Credits),
|
|
"Pass": pass,
|
|
"Lang": groupLang,
|
|
"Allowed": sites(st.Allowed),
|
|
"Blocked": sites(st.Blocked),
|
|
"SpendCap": b.tr.Sats(lang, st.SpendCap),
|
|
"DailyCap": b.capSats(lang, st.DailyCap),
|
|
})
|
|
row := func(key, label, value string) []telego.InlineKeyboardButton {
|
|
return []telego.InlineKeyboardButton{button(
|
|
b.tr.T(lang, "settings.button", i18n.Args{"Label": label, "Value": value}), cbData(cbGroupSetting, key))}
|
|
}
|
|
kb := keyboard(
|
|
row("en", b.tr.T(lang, "settings.enabled", nil), b.onOff(lang, st.Enabled)),
|
|
row("ad", b.tr.T(lang, "settings.auto_detect", nil), b.onOff(lang, st.AutoDetect)),
|
|
row("dc", b.tr.T(lang, "settings.delete_commands", nil), b.onOff(lang, st.DeleteCmds)),
|
|
row("wu", b.tr.T(lang, "settings.who", nil), b.tr.T(lang, "settings.who_"+st.WhoCanUse, nil)),
|
|
row("sp", b.tr.T(lang, "settings.spend", nil), b.tr.T(lang, "settings.spend_"+st.SpendRule, nil)),
|
|
row("dq", b.tr.T(lang, "settings.quality", nil), b.qualityName(lang, st.Quality)),
|
|
[]telego.InlineKeyboardButton{urlButton(b.tr.T(lang, "btn.full_settings", nil), b.groupSettingsLink(st.ChatID))},
|
|
)
|
|
return text, kb
|
|
}
|
|
|
|
func (b *Bot) capSats(lang string, n int64) string {
|
|
if n <= 0 {
|
|
return b.tr.T(lang, "account.unlimited", nil)
|
|
}
|
|
return b.tr.Sats(lang, n)
|
|
}
|
|
|
|
func (b *Bot) cmdSettings(ctx context.Context, m *telego.Message, act actor) {
|
|
if !b.requireAdmin(ctx, m, act) {
|
|
return
|
|
}
|
|
st, err := b.store.GetGroupSettings(ctx, m.Chat.ID)
|
|
if err != nil {
|
|
b.internalError(ctx, m.Chat.ID, act, err)
|
|
return
|
|
}
|
|
text, kb := b.settingsView(ctx, act.lang, st)
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), text, kb)
|
|
}
|
|
|
|
// next returns the value after cur in a cycle.
|
|
func next(cur string, cycle ...string) string {
|
|
for i, v := range cycle {
|
|
if v == cur {
|
|
return cycle[(i+1)%len(cycle)]
|
|
}
|
|
}
|
|
return cycle[0]
|
|
}
|
|
|
|
// cbSetting handles a quick toggle under /settings. The admin check runs
|
|
// on every press: the buttons are visible to everyone in the group.
|
|
func (b *Bot) cbSetting(ctx context.Context, q *telego.CallbackQuery, act actor, key string) {
|
|
if q.Message == nil || !groups.IsGroup(q.Message.GetChat().Type) {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
|
return
|
|
}
|
|
chat := q.Message.GetChat().ID
|
|
if !b.admins.IsAdmin(ctx, chat, act.user.ID) {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.admins_only", nil), true)
|
|
return
|
|
}
|
|
st, err := b.store.GetGroupSettings(ctx, chat)
|
|
if err != nil {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true)
|
|
return
|
|
}
|
|
switch key {
|
|
case "en":
|
|
st.Enabled = !st.Enabled
|
|
case "ad":
|
|
st.AutoDetect = !st.AutoDetect
|
|
case "dc":
|
|
st.DeleteCmds = !st.DeleteCmds
|
|
case "wu":
|
|
st.WhoCanUse = next(st.WhoCanUse, db.UseEveryone, db.UsePassHolders, db.UseAdmins)
|
|
case "sp":
|
|
st.SpendRule = next(st.SpendRule, db.SpendAdmins, db.SpendAnyone, db.SpendCapped)
|
|
if st.SpendRule == db.SpendCapped && st.SpendCap == 0 {
|
|
st.SpendCap = 4 * b.cfg.Prices.CreditPerDownload
|
|
}
|
|
case "dq":
|
|
st.Quality = next(st.Quality, "", string(media.Best), string(media.P720), string(media.P480), string(media.MP3))
|
|
default:
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
|
return
|
|
}
|
|
if err := b.store.SaveGroupSettings(ctx, st); err != nil {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true)
|
|
return
|
|
}
|
|
b.log.Info("group setting changed", "chat_id", chat, "key", key, "by", act.user.ID)
|
|
// The group may have its own language; the view follows it.
|
|
lang := act.lang
|
|
if st.Lang != "" {
|
|
lang = st.Lang
|
|
}
|
|
text, kb := b.settingsView(ctx, lang, st)
|
|
b.answer(ctx, q, b.tr.T(lang, "notice.saved", nil), false)
|
|
_ = b.edit(ctx, chat, int64(q.Message.GetMessageID()), false, text, kb)
|
|
}
|
|
|
|
// changeSettings loads, changes and saves a group's settings for an admin
|
|
// command, and confirms with the given message.
|
|
func (b *Bot) changeSettings(ctx context.Context, m *telego.Message, act actor, change func(*db.GroupSettings) (key string, args i18n.Args)) {
|
|
st, err := b.store.GetGroupSettings(ctx, m.Chat.ID)
|
|
if err != nil {
|
|
b.internalError(ctx, m.Chat.ID, act, err)
|
|
return
|
|
}
|
|
key, args := change(st)
|
|
if key == "" {
|
|
return
|
|
}
|
|
if strings.HasPrefix(key, "!") { // a refusal: nothing to save
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, key[1:], args), nil)
|
|
return
|
|
}
|
|
if err := b.store.SaveGroupSettings(ctx, st); err != nil {
|
|
b.internalError(ctx, m.Chat.ID, act, err)
|
|
return
|
|
}
|
|
lang := act.lang
|
|
if st.Lang != "" {
|
|
lang = st.Lang
|
|
}
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(lang, key, args), nil)
|
|
}
|
|
|
|
func (b *Bot) cmdGroupLang(ctx context.Context, m *telego.Message, act actor) {
|
|
if !b.requireAdmin(ctx, m, act) {
|
|
return
|
|
}
|
|
arg := strings.ToLower(strings.TrimSpace(commandArgs(m)))
|
|
usage := i18n.Args{"Langs": strings.Join(b.visibleLangs(act.user.ID), ", ")}
|
|
changed, newLang := false, ""
|
|
b.changeSettings(ctx, m, act, func(st *db.GroupSettings) (string, i18n.Args) {
|
|
if arg == "" {
|
|
return "!grouplang.usage", usage
|
|
}
|
|
if arg == "auto" {
|
|
st.Lang, changed = "", true
|
|
return "grouplang.auto", nil
|
|
}
|
|
if l := b.findLang(arg, act.user.ID); l != "" {
|
|
st.Lang, changed, newLang = l, true, l
|
|
return "grouplang.set", i18n.Args{"Lang": b.tr.T(l, "language.name", nil)}
|
|
}
|
|
return "!grouplang.usage", usage
|
|
})
|
|
if changed {
|
|
b.SyncGroupMenu(ctx, m.Chat.ID, newLang)
|
|
}
|
|
}
|
|
|
|
func (b *Bot) cmdAutoDetect(ctx context.Context, m *telego.Message, act actor) {
|
|
if !b.requireAdmin(ctx, m, act) {
|
|
return
|
|
}
|
|
arg := strings.ToLower(strings.TrimSpace(commandArgs(m)))
|
|
b.changeSettings(ctx, m, act, func(st *db.GroupSettings) (string, i18n.Args) {
|
|
switch arg {
|
|
case "on":
|
|
st.AutoDetect = true
|
|
return "autodetect.on", nil
|
|
case "off":
|
|
st.AutoDetect = false
|
|
return "autodetect.off", nil
|
|
}
|
|
return "!autodetect.usage", nil
|
|
})
|
|
}
|
|
|
|
// cmdSiteList handles /allow and /block: no argument shows the list, a
|
|
// host adds it, "remove <host>" removes it, "clear" empties the list.
|
|
func (b *Bot) cmdSiteList(ctx context.Context, m *telego.Message, act actor, allow bool) {
|
|
if !b.requireAdmin(ctx, m, act) {
|
|
return
|
|
}
|
|
name := "block"
|
|
if allow {
|
|
name = "allow"
|
|
}
|
|
fields := strings.Fields(commandArgs(m))
|
|
b.changeSettings(ctx, m, act, func(st *db.GroupSettings) (string, i18n.Args) {
|
|
list := &st.Blocked
|
|
if allow {
|
|
list = &st.Allowed
|
|
}
|
|
show := func() i18n.Args {
|
|
v := strings.Join(db.Sites(*list), ", ")
|
|
if v == "" {
|
|
v = b.tr.T(act.lang, "word.none", nil)
|
|
}
|
|
return i18n.Args{"Sites": v}
|
|
}
|
|
switch {
|
|
case len(fields) == 0:
|
|
return "!" + name + ".list", show()
|
|
case fields[0] == "clear":
|
|
*list = ""
|
|
return name + ".list", show()
|
|
}
|
|
remove := fields[0] == "remove"
|
|
if remove {
|
|
fields = fields[1:]
|
|
}
|
|
if len(fields) == 0 {
|
|
return "!" + name + ".list", show()
|
|
}
|
|
host := groups.CleanHost(fields[0])
|
|
if host == "" {
|
|
return "!sites.bad_host", nil
|
|
}
|
|
var kept []string
|
|
for _, h := range db.Sites(*list) {
|
|
if h != host {
|
|
kept = append(kept, h)
|
|
}
|
|
}
|
|
if !remove && len(kept) < 50 {
|
|
kept = append(kept, host)
|
|
}
|
|
*list = strings.Join(kept, ",")
|
|
return name + ".list", show()
|
|
})
|
|
}
|
|
|
|
// cmdSpending handles /spending anyone | admins | capped <sats> and
|
|
// /spending daily <sats>.
|
|
func (b *Bot) cmdSpending(ctx context.Context, m *telego.Message, act actor) {
|
|
if !b.requireAdmin(ctx, m, act) {
|
|
return
|
|
}
|
|
fields := strings.Fields(strings.ToLower(commandArgs(m)))
|
|
b.changeSettings(ctx, m, act, func(st *db.GroupSettings) (string, i18n.Args) {
|
|
amount := func(i int) (int64, bool) {
|
|
if len(fields) <= i {
|
|
return 0, false
|
|
}
|
|
n, err := strconv.ParseInt(fields[i], 10, 64)
|
|
return n, err == nil && n >= 0 && n <= billing.MaxAmount
|
|
}
|
|
if len(fields) == 0 {
|
|
return "!spending.usage", nil
|
|
}
|
|
switch fields[0] {
|
|
case db.SpendAnyone, db.SpendAdmins:
|
|
st.SpendRule = fields[0]
|
|
case db.SpendCapped:
|
|
n, ok := amount(1)
|
|
if !ok || n == 0 {
|
|
return "!spending.usage", nil
|
|
}
|
|
st.SpendRule, st.SpendCap = db.SpendCapped, n
|
|
case "daily":
|
|
n, ok := amount(1)
|
|
if !ok {
|
|
return "!spending.usage", nil
|
|
}
|
|
st.DailyCap = n
|
|
default:
|
|
return "!spending.usage", nil
|
|
}
|
|
return "spending.set", i18n.Args{
|
|
"Rule": b.tr.T(act.lang, "settings.spend_"+st.SpendRule, nil),
|
|
"SpendCap": b.tr.Sats(act.lang, st.SpendCap),
|
|
"DailyCap": b.capSats(act.lang, st.DailyCap),
|
|
}
|
|
})
|
|
}
|
|
|
|
// cmdGroupWallet shows the group's balance and pass to anyone, with links
|
|
// to top it up. Paying happens in the private chat.
|
|
func (b *Bot) cmdGroupWallet(ctx context.Context, m *telego.Message, act actor, passOnly bool) {
|
|
if !b.requireGroup(ctx, m, act) {
|
|
return
|
|
}
|
|
lang := act.lang
|
|
wallet, err := b.bill.State(ctx, db.OwnerGroup, m.Chat.ID)
|
|
if err != nil {
|
|
b.internalError(ctx, m.Chat.ID, act, err)
|
|
return
|
|
}
|
|
st, err := b.store.GetGroupSettings(ctx, m.Chat.ID)
|
|
if err != nil {
|
|
b.internalError(ctx, m.Chat.ID, act, err)
|
|
return
|
|
}
|
|
pass := b.tr.T(lang, "groupwallet.pass_none", nil)
|
|
if wallet.PassUntil > 0 {
|
|
pass = b.tr.T(lang, "groupwallet.pass_until", i18n.Args{"Until": b.tr.DateTime(lang, time.UnixMilli(wallet.PassUntil))})
|
|
}
|
|
key := "groupwallet.text"
|
|
if passOnly {
|
|
key = "grouppass.text"
|
|
}
|
|
text := b.tr.HTML(lang, key, i18n.Args{
|
|
"Balance": b.tr.Sats(lang, wallet.Credits),
|
|
"Pass": pass,
|
|
"Rule": b.tr.T(lang, "settings.spend_"+st.SpendRule, nil),
|
|
})
|
|
var kb *telego.InlineKeyboardMarkup
|
|
if b.bill.Enabled() {
|
|
label := "btn.add_group_credits"
|
|
if passOnly {
|
|
label = "btn.buy_group_pass"
|
|
}
|
|
kb = keyboard([]telego.InlineKeyboardButton{urlButton(b.tr.T(lang, label, nil), b.groupWalletLink(m.Chat.ID))})
|
|
}
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), text, kb)
|
|
}
|
|
|
|
func (b *Bot) cmdGroupStats(ctx context.Context, m *telego.Message, act actor) {
|
|
if !b.requireAdmin(ctx, m, act) {
|
|
return
|
|
}
|
|
lang := act.lang
|
|
since := service.Day(time.Now().AddDate(0, 0, -6))
|
|
usage, err := b.store.GroupUsage(ctx, m.Chat.ID, since, 15)
|
|
if err != nil {
|
|
b.internalError(ctx, m.Chat.ID, act, err)
|
|
return
|
|
}
|
|
if len(usage) == 0 {
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(lang, "groupstats.empty", nil), nil)
|
|
return
|
|
}
|
|
var sb strings.Builder
|
|
sb.WriteString(b.tr.HTML(lang, "groupstats.header", nil))
|
|
for _, u := range usage {
|
|
name := u.Name
|
|
if name == "" {
|
|
name = strconv.FormatInt(u.UserID, 10)
|
|
}
|
|
sb.WriteString("\n")
|
|
sb.WriteString(b.tr.HTML(lang, "groupstats.line", i18n.Args{
|
|
"Name": shorten(name, 30), "Count": u.Downloads,
|
|
"Size": b.tr.Size(lang, u.Bytes), "Sats": b.tr.Sats(lang, u.Sats),
|
|
}))
|
|
}
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), sb.String(), nil)
|
|
}
|
|
|
|
// --- paying for a group, in the private chat --------------------------------
|
|
|
|
// startGroupLink handles /start gw_<chat id> and gs_<chat id>, the deep
|
|
// links from a group. The target wallet is the group's, pre-selected.
|
|
func (b *Bot) startGroupLink(ctx context.Context, m *telego.Message, act actor, arg string, settings bool) {
|
|
lang := act.lang
|
|
chatID, err := strconv.ParseInt(arg, 10, 64)
|
|
g, gerr := b.store.GetGroup(ctx, chatID)
|
|
if err != nil || gerr != nil || !g.Present() {
|
|
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(lang, "groupbuy.unknown", nil), nil)
|
|
return
|
|
}
|
|
if settings {
|
|
// Without the Mini App the full settings are the admin commands.
|
|
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(lang, "settings.commands", i18n.Args{"Group": g.Title}), nil)
|
|
return
|
|
}
|
|
if !b.bill.Enabled() {
|
|
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(lang, "buy.unavailable", nil), nil)
|
|
return
|
|
}
|
|
wallet, _ := b.bill.State(ctx, db.OwnerGroup, chatID)
|
|
gid := strconv.FormatInt(chatID, 10)
|
|
rows := b.passButtons(lang, func(product string) string { return cbData(cbGroupBuy, product, gid) })
|
|
var packs []telego.InlineKeyboardButton
|
|
for _, a := range b.cfg.Prices.CreditPacks {
|
|
packs = append(packs, button(b.tr.Sats(lang, a), cbData(cbGroupBuy, billing.ProductCredits, gid, strconv.FormatInt(a, 10))))
|
|
}
|
|
rows = append(rows, packs)
|
|
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(lang, "groupbuy.intro", i18n.Args{
|
|
"Group": g.Title, "Balance": b.tr.Sats(lang, wallet.Credits),
|
|
}), keyboard(rows...))
|
|
}
|
|
|
|
// cbGroupBuyProduct creates an invoice whose target is a group wallet.
|
|
// Anyone may pay for a group; what they buy belongs to the group.
|
|
func (b *Bot) cbGroupBuyProduct(ctx context.Context, q *telego.CallbackQuery, act actor, product, rest string) {
|
|
gidArg, amountArg, _ := strings.Cut(rest, ":")
|
|
chatID, err := strconv.ParseInt(gidArg, 10, 64)
|
|
g, gerr := b.store.GetGroup(ctx, chatID)
|
|
if err != nil || gerr != nil || !g.Present() {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
|
return
|
|
}
|
|
b.answer(ctx, q, "", false)
|
|
amount, _ := strconv.ParseInt(amountArg, 10, 64)
|
|
b.startPurchase(ctx, act.user.ID, act, product, amount, purchaseContext{groupID: chatID})
|
|
}
|
|
|
|
// --- personal preferences ---------------------------------------------------
|
|
|
|
func (b *Bot) cmdQuality(ctx context.Context, m *telego.Message, act actor) {
|
|
kb := b.qualityKeyboard(act, func(v string) string { return cbData(cbQuality, v) }, false)
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "quality.pick", nil), kb)
|
|
}
|
|
|
|
func (b *Bot) cbSetQuality(ctx context.Context, q *telego.CallbackQuery, act actor, value string) {
|
|
if !validUserQuality(value) {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
|
return
|
|
}
|
|
if err := b.store.SetUserQuality(ctx, act.user.ID, value); err != nil {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true)
|
|
return
|
|
}
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.saved", nil), false)
|
|
if q.Message != nil {
|
|
_ = b.edit(ctx, q.Message.GetChat().ID, int64(q.Message.GetMessageID()), false,
|
|
b.tr.HTML(act.lang, "quality.set", i18n.Args{"Quality": b.userQualityName(act.lang, value)}), nil)
|
|
}
|
|
}
|
|
|
|
func (b *Bot) cmdLanguage(ctx context.Context, m *telego.Message, act actor) {
|
|
kb := b.languageKeyboard(act, func(l string) string { return cbData(cbLanguage, l) }, false)
|
|
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "language.pick", nil), kb)
|
|
}
|
|
|
|
func (b *Bot) cbSetLanguage(ctx context.Context, q *telego.CallbackQuery, act actor, value string) {
|
|
shown, ok := b.applyLanguage(ctx, act, value)
|
|
if !ok {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
|
return
|
|
}
|
|
b.answer(ctx, q, b.tr.T(shown, "notice.saved", nil), false)
|
|
if q.Message != nil {
|
|
_ = b.edit(ctx, q.Message.GetChat().ID, int64(q.Message.GetMessageID()), false,
|
|
b.tr.HTML(shown, "language.set", i18n.Args{"Lang": b.tr.T(shown, "language.name", nil)}), nil)
|
|
}
|
|
}
|
|
|
|
// cbWelcomeLanguage is a language picked under the first welcome: the
|
|
// welcome is written again in that language.
|
|
func (b *Bot) cbWelcomeLanguage(ctx context.Context, q *telego.CallbackQuery, act actor, value string) {
|
|
shown, ok := b.applyLanguage(ctx, act, value)
|
|
if !ok {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
|
return
|
|
}
|
|
b.answer(ctx, q, b.tr.T(shown, "notice.saved", nil), false)
|
|
if q.Message != nil {
|
|
_ = b.edit(ctx, q.Message.GetChat().ID, int64(q.Message.GetMessageID()), false,
|
|
b.tr.HTML(shown, "start.private", i18n.Args{"Name": act.user.FirstName}), nil)
|
|
}
|
|
}
|
|
|
|
// cmdStatic answers /privacy, /terms and /support from the catalog.
|
|
func (b *Bot) cmdStatic(ctx context.Context, m *telego.Message, act actor, key string) {
|
|
contact := b.cfg.Support
|
|
if contact == "" {
|
|
contact = "@" + b.username
|
|
}
|
|
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(act.lang, key, i18n.Args{"Contact": contact}), nil)
|
|
}
|