/note, /notes (list and batch), /redeem and /lnurlmints with confirm dialogs, group posting with private receipts, claim notifications, batch export as PDF/JSON/CSV, and LNURLcash notes detected inside the existing /lnurl, paste and QR-photo flows. Plain withdraw links behave as before. Two /lnurl log lines no longer print k1. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
118 lines
4.6 KiB
Go
118 lines
4.6 KiB
Go
package telegram
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
tiperrors "github.com/LightningTipBot/LightningTipBot/internal/errors"
|
|
"github.com/LightningTipBot/LightningTipBot/internal/lnurlcash"
|
|
"github.com/LightningTipBot/LightningTipBot/internal/runtime/mutex"
|
|
"github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept"
|
|
log "github.com/sirupsen/logrus"
|
|
tb "gopkg.in/lightningtipbot/telebot.v3"
|
|
)
|
|
|
|
// LNURLCashNoteParams is what HandleLNURL returns for a withdraw link that
|
|
// publishes a mintPubkey: an LNURLcash note rather than a plain withdraw.
|
|
type LNURLCashNoteParams struct {
|
|
URL string
|
|
}
|
|
|
|
var (
|
|
btnRedeemOK = (&tb.ReplyMarkup{}).Data("", "lnc_redeem_ok")
|
|
btnRedeemCancel = (&tb.ReplyMarkup{}).Data("", "lnc_redeem_no")
|
|
)
|
|
|
|
// redeemHandler handles /redeem <note>. In a group the command is deleted
|
|
// at once because it carries the secret, and the flow continues privately.
|
|
func (bot *TipBot) redeemHandler(ctx intercept.Context) (intercept.Context, error) {
|
|
if !bot.lnurlcashReady(ctx) {
|
|
return ctx, nil
|
|
}
|
|
m := ctx.Message()
|
|
if !m.Private() {
|
|
bot.tryDeleteMessage(m)
|
|
}
|
|
user, ok := bot.noteCommandUser(ctx)
|
|
if !ok {
|
|
return ctx, tiperrors.Create(tiperrors.UserNoWalletError)
|
|
}
|
|
parts := strings.Fields(m.Text)
|
|
if len(parts) < 2 {
|
|
if _, ok := bot.sendPrivate(m.Sender, TranslateUser(ctx, "redeemHelpMessage")); !ok && !m.Private() {
|
|
bot.tryReplyMessage(m, Translate(ctx, "lnurlcashStartPrivateFirstMessage"))
|
|
}
|
|
return ctx, tiperrors.Create(tiperrors.InvalidSyntaxError)
|
|
}
|
|
return bot.redeemInput(ctx, user.Name, user.Telegram.LanguageCode, parts[1], !m.Private())
|
|
}
|
|
|
|
// redeemInput previews a pasted note and offers to add it to the balance.
|
|
// Everything goes to the user's private chat.
|
|
func (bot *TipBot) redeemInput(ctx intercept.Context, userName, lang, input string, fromGroup bool) (intercept.Context, error) {
|
|
sender := ctx.Sender()
|
|
preview, err := bot.LNURLCash.RedeemInfo(ctx, userName, lang, input)
|
|
if err != nil {
|
|
if errors.Is(err, lnurlcash.ErrNotANote) && !fromGroup {
|
|
// the caller decides (the /lnurl flow falls back to a plain withdraw)
|
|
return ctx, err
|
|
}
|
|
text := lnurlcashErrorText(ctx, err)
|
|
if _, ok := bot.sendPrivate(sender, text); !ok && fromGroup {
|
|
bot.tryReplyMessage(ctx.Message(), Translate(ctx, "lnurlcashStartPrivateFirstMessage"))
|
|
}
|
|
return ctx, err
|
|
}
|
|
text := fmt.Sprintf(TranslateUser(ctx, "redeemPreviewMessage"), formatSat(preview.ValueMsat), preview.MintHost)
|
|
if !preview.KnownMint {
|
|
text += "\n\n" + TranslateUser(ctx, "redeemUnknownMintWarning")
|
|
}
|
|
menu := &tb.ReplyMarkup{}
|
|
menu.Inline(menu.Row(
|
|
menu.Data(TranslateUser(ctx, "redeemAddButton"), "lnc_redeem_ok", preview.Redeem.ID),
|
|
menu.Data(TranslateUser(ctx, "noteCancelButton"), "lnc_redeem_no", preview.Redeem.ID)))
|
|
if _, ok := bot.sendPrivate(sender, text, menu); !ok && fromGroup {
|
|
bot.tryReplyMessage(ctx.Message(), Translate(ctx, "lnurlcashStartPrivateFirstMessage"))
|
|
return ctx, tiperrors.Create(tiperrors.NoPrivateChatError)
|
|
}
|
|
return ctx, nil
|
|
}
|
|
|
|
// redeemConfirmHandler runs when Add to balance was pressed.
|
|
func (bot *TipBot) redeemConfirmHandler(ctx intercept.Context) (intercept.Context, error) {
|
|
if bot.LNURLCash == nil {
|
|
return ctx, nil
|
|
}
|
|
mutex.LockWithContext(ctx, ctx.Data())
|
|
defer mutex.UnlockWithContext(ctx, ctx.Data())
|
|
user := LoadUser(ctx)
|
|
msg := ctx.Callback().Message
|
|
bot.tryEditMessage(msg, TranslateUser(ctx, "redeemInFlightMessage"), &tb.ReplyMarkup{})
|
|
r, err := bot.LNURLCash.Redeem(ctx, ctx.Data(), user.Name)
|
|
if err != nil {
|
|
bot.tryEditMessage(msg, lnurlcashErrorText(ctx, err), &tb.ReplyMarkup{})
|
|
return ctx, err
|
|
}
|
|
log.Infof("[lnurlcash] %s redeems %s (%d msat)", GetUserStr(ctx.Sender()), r.ID, r.ReportedMsat)
|
|
bot.tryEditMessage(msg, fmt.Sprintf(TranslateUser(ctx, "redeemRequestedMessage"), formatSat(r.ReportedMsat)), &tb.ReplyMarkup{})
|
|
return ctx, nil
|
|
}
|
|
|
|
// redeemCancelHandler runs when Cancel was pressed on a preview.
|
|
func (bot *TipBot) redeemCancelHandler(ctx intercept.Context) (intercept.Context, error) {
|
|
if bot.LNURLCash == nil {
|
|
return ctx, nil
|
|
}
|
|
r, err := bot.LNURLCash.Store().Redeem(ctx.Data())
|
|
if err != nil || r.UserName != LoadUser(ctx).Name {
|
|
return ctx, tiperrors.Create(tiperrors.UnknownError)
|
|
}
|
|
_ = bot.LNURLCash.Store().TransitionRedeem(r.ID, lnurlcash.RedeemInfo, lnurlcash.RedeemFailed, map[string]interface{}{"last_error": "cancelled"})
|
|
bot.tryEditMessage(ctx.Callback().Message, TranslateUser(ctx, "redeemCancelledMessage"), &tb.ReplyMarkup{})
|
|
return ctx, nil
|
|
}
|
|
|
|
// LNURLKind satisfies go-lnurl's LNURLParams so HandleLNURL can return a note.
|
|
func (LNURLCashNoteParams) LNURLKind() string { return "lnurlcash-note" }
|