98 lines
3.1 KiB
Go
98 lines
3.1 KiB
Go
package telegram
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal"
|
|
tiperrors "github.com/LightningTipBot/LightningTipBot/internal/errors"
|
|
"github.com/LightningTipBot/LightningTipBot/internal/lnurlcash"
|
|
"github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept"
|
|
lnurlcashgo "github.com/lnurlcash/lnurlcash-go"
|
|
tb "gopkg.in/lightningtipbot/telebot.v3"
|
|
)
|
|
|
|
var btnSetMint = (&tb.ReplyMarkup{}).Data("", "lnc_setmint")
|
|
|
|
func bot_lnurlcash_cfg() *lnurlcash.Config { return &internal.Configuration.LNURLCash }
|
|
|
|
// lnurlmintsHandler lists the admin mints and lets the user pick a default.
|
|
func (bot *TipBot) lnurlmintsHandler(ctx intercept.Context) (intercept.Context, error) {
|
|
if !bot.lnurlcashReady(ctx) {
|
|
return ctx, nil
|
|
}
|
|
m := ctx.Message()
|
|
user, ok := bot.noteCommandUser(ctx)
|
|
if !ok {
|
|
return ctx, tiperrors.Create(tiperrors.UserNoWalletError)
|
|
}
|
|
if !m.Private() {
|
|
NewMessage(m, WithDuration(0, bot))
|
|
}
|
|
text, kb := bot.mintsText(ctx, user.Name)
|
|
if _, ok := bot.sendPrivate(m.Sender, text, kb); !ok && !m.Private() {
|
|
bot.tryReplyMessage(m, Translate(ctx, "lnurlcashStartPrivateFirstMessage"))
|
|
}
|
|
return ctx, nil
|
|
}
|
|
|
|
func (bot *TipBot) mintsText(ctx intercept.Context, user string) (string, *tb.ReplyMarkup) {
|
|
reg := bot.LNURLCash.Mints()
|
|
current, _ := reg.ForUser(user)
|
|
menu := &tb.ReplyMarkup{}
|
|
var rows []tb.Row
|
|
text := TranslateUser(ctx, "lnurlmintsHeader")
|
|
for _, mnt := range reg.All() {
|
|
state := "🟢"
|
|
switch {
|
|
case mnt.Disabled:
|
|
state = "⛔️"
|
|
case !mnt.Healthy:
|
|
state = "🔴"
|
|
}
|
|
mark := ""
|
|
if current != nil && current.ID == mnt.ID {
|
|
mark = " ⭐️"
|
|
}
|
|
fee := TranslateUser(ctx, "lnurlmintsNoFee")
|
|
if mnt.FeeBaseMsat > 0 || mnt.FeePpm > 0 {
|
|
fee = lnurlcashgo.DescribeMintFee(mnt.Fee())
|
|
}
|
|
text += fmt.Sprintf(TranslateUser(ctx, "lnurlmintsItem"), state, mnt.ID, mark, lnurlcashgo.ServerOf(mnt.PayURL), fee)
|
|
if mnt.Usable() {
|
|
rows = append(rows, menu.Row(menu.Data(fmt.Sprintf(TranslateUser(ctx, "lnurlmintsSetButton"), mnt.ID), "lnc_setmint", mnt.ID)))
|
|
}
|
|
}
|
|
menu.Inline(rows...)
|
|
return text, menu
|
|
}
|
|
|
|
// setMintHandler stores the user's default mint.
|
|
func (bot *TipBot) setMintHandler(ctx intercept.Context) (intercept.Context, error) {
|
|
if bot.LNURLCash == nil {
|
|
return ctx, nil
|
|
}
|
|
user := LoadUser(ctx)
|
|
mnt, ok := bot.LNURLCash.Mints().Get(ctx.Data())
|
|
if !ok || !mnt.Usable() {
|
|
bot.trySendMessageEditable(ctx.Sender(), TranslateUser(ctx, "noteMintUnavailableMessage"))
|
|
return ctx, nil
|
|
}
|
|
if err := bot.LNURLCash.Store().SetPref(user.Name, mnt.ID); err != nil {
|
|
return ctx, err
|
|
}
|
|
bot.trySendMessageEditable(ctx.Sender(), fmt.Sprintf(TranslateUser(ctx, "lnurlmintsSetMessage"), mnt.ID))
|
|
text, kb := bot.mintsText(ctx, user.Name)
|
|
bot.tryEditMessage(ctx.Callback().Message, text, kb)
|
|
return ctx, nil
|
|
}
|
|
|
|
// lnurlHelpHandler sends the LNURLcash notes help, privately.
|
|
func (bot *TipBot) lnurlHelpHandler(ctx intercept.Context) (intercept.Context, error) {
|
|
m := ctx.Message()
|
|
if !m.Private() {
|
|
bot.tryDeleteMessage(m)
|
|
}
|
|
bot.trySendMessage(ctx.Sender(), TranslateUser(ctx, "noteHelpMessage"), tb.NoPreview)
|
|
return ctx, nil
|
|
}
|