lightningtipbot/internal/telegram/inline_send.go

279 lines
10 KiB
Go
Raw Permalink Normal View History

package telegram
2021-09-10 08:52:44 +02:00
import (
"context"
2021-09-10 08:52:44 +02:00
"fmt"
"strings"
"time"
"github.com/LightningTipBot/LightningTipBot/internal/errors"
"github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept"
"github.com/LightningTipBot/LightningTipBot/internal/runtime/mutex"
"github.com/LightningTipBot/LightningTipBot/internal/storage"
"github.com/eko/gocache/store"
"github.com/LightningTipBot/LightningTipBot/internal/i18n"
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
"github.com/LightningTipBot/LightningTipBot/internal/lnbits"
2021-09-10 08:52:44 +02:00
log "github.com/sirupsen/logrus"
tb "gopkg.in/lightningtipbot/telebot.v3"
2021-09-10 08:52:44 +02:00
)
var (
inlineSendMenu = &tb.ReplyMarkup{ResizeKeyboard: true}
btnCancelInlineSend = inlineSendMenu.Data("🚫 Cancel", "cancel_send_inline")
btnAcceptInlineSend = inlineSendMenu.Data("✅ Receive", "confirm_send_inline")
2021-09-13 22:44:43 +02:00
)
type InlineSend struct {
*storage.Base
Message string `json:"inline_send_message"`
Amount int64 `json:"inline_send_amount"`
From *lnbits.User `json:"inline_send_from"`
To *lnbits.User `json:"inline_send_to"`
To_SpecificUser bool `json:"to_specific_user"`
Memo string `json:"inline_send_memo"`
LanguageCode string `json:"languagecode"`
2021-09-13 22:44:43 +02:00
}
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
func (bot TipBot) makeSendKeyboard(ctx context.Context, id string) *tb.ReplyMarkup {
inlineSendMenu := &tb.ReplyMarkup{ResizeKeyboard: true}
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
acceptInlineSendButton := inlineSendMenu.Data(Translate(ctx, "receiveButtonMessage"), "confirm_send_inline")
cancelInlineSendButton := inlineSendMenu.Data(Translate(ctx, "cancelButtonMessage"), "cancel_send_inline")
acceptInlineSendButton.Data = id
cancelInlineSendButton.Data = id
inlineSendMenu.Inline(
inlineSendMenu.Row(
acceptInlineSendButton,
cancelInlineSendButton),
)
return inlineSendMenu
2021-09-10 08:52:44 +02:00
}
func (bot TipBot) handleInlineSendQuery(ctx intercept.Context) (intercept.Context, error) {
q := ctx.Query()
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
// inlineSend := NewInlineSend()
// var err error
amount, err := decodeAmountFromCommand(q.Text)
2021-09-13 22:44:43 +02:00
if err != nil {
bot.inlineQueryReplyWithError(ctx, TranslateUser(ctx, "inlineQuerySendTitle"), fmt.Sprintf(TranslateUser(ctx, "inlineQuerySendDescription"), bot.Telegram.Me.Username))
return ctx, err
2021-09-13 22:44:43 +02:00
}
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
if amount < 1 {
bot.inlineQueryReplyWithError(ctx, TranslateUser(ctx, "inlineSendInvalidAmountMessage"), fmt.Sprintf(Translate(ctx, "inlineQuerySendDescription"), bot.Telegram.Me.Username))
return ctx, errors.Create(errors.InvalidAmountError)
2021-09-13 22:44:43 +02:00
}
fromUser := LoadUser(ctx)
fromUserStr := GetUserStr(q.Sender)
balance, err := bot.GetUserBalanceCached(fromUser)
2021-09-13 22:44:43 +02:00
if err != nil {
errmsg := fmt.Sprintf("could not get balance of user %s", fromUserStr)
log.Errorln(errmsg)
return ctx, err
2021-09-13 22:44:43 +02:00
}
// check if fromUser has balance
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
if balance < amount {
log.Errorf("Balance of user %s too low", fromUserStr)
bot.inlineQueryReplyWithError(ctx, TranslateUser(ctx, "inlineSendBalanceLowMessage"), fmt.Sprintf(TranslateUser(ctx, "inlineQuerySendDescription"), bot.Telegram.Me.Username))
return ctx, errors.Create(errors.InvalidAmountError)
2021-09-13 22:44:43 +02:00
}
// check whether the 3rd argument is a username
// command is "@LightningTipBot send 123 @to_user This is the memo"
memo_argn := 2 // argument index at which the memo starts, will be 3 if there is a to_username in command
toUserDb := &lnbits.User{}
to_SpecificUser := false
if len(strings.Split(q.Text, " ")) > 2 {
to_username := strings.Split(q.Text, " ")[2]
if strings.HasPrefix(to_username, "@") {
toUserDb, err = GetUserByTelegramUsername(to_username[1:], bot) // must be without the @
if err != nil {
//bot.tryDeleteMessage(m)
//bot.trySendMessage(m.Sender, fmt.Sprintf(Translate(ctx, "sendUserHasNoWalletMessage"), toUserStrMention))
bot.inlineQueryReplyWithError(ctx,
fmt.Sprintf(TranslateUser(ctx, "sendUserHasNoWalletMessage"), to_username),
fmt.Sprintf(TranslateUser(ctx, "inlineQuerySendDescription"),
bot.Telegram.Me.Username))
return ctx, err
}
memo_argn = 3 // assume that memo starts after the to_username
to_SpecificUser = true
}
}
2021-09-13 22:44:43 +02:00
// check for memo in command
memo := GetMemoFromCommand(q.Text, memo_argn)
2021-09-13 22:44:43 +02:00
urls := []string{
queryImage,
}
results := make(tb.Results, len(urls)) // []tb.Result
for i, url := range urls {
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
inlineMessage := fmt.Sprintf(Translate(ctx, "inlineSendMessage"), fromUserStr, amount)
// modify message if payment is to specific user
if to_SpecificUser {
inlineMessage = fmt.Sprintf("@%s: %s", toUserDb.Telegram.Username, inlineMessage)
}
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
if len(memo) > 0 {
inlineMessage = inlineMessage + fmt.Sprintf(Translate(ctx, "inlineSendAppendMemo"), memo)
2021-09-13 22:44:43 +02:00
}
result := &tb.ArticleResult{
// URL: url,
Text: inlineMessage,
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
Title: fmt.Sprintf(TranslateUser(ctx, "inlineResultSendTitle"), amount),
Description: fmt.Sprintf(TranslateUser(ctx, "inlineResultSendDescription"), amount),
2021-09-13 22:44:43 +02:00
// required for photos
ThumbURL: url,
}
id := fmt.Sprintf("inl-send-%d-%d-%s", q.Sender.ID, amount, RandStringRunes(5))
result.ReplyMarkup = &tb.ReplyMarkup{InlineKeyboard: bot.makeSendKeyboard(ctx, id).InlineKeyboard}
2021-09-13 22:44:43 +02:00
results[i] = result
// needed to set a unique string ID for each result
results[i].SetResultID(id)
// add data to persistent object
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
inlineSend := InlineSend{
Base: storage.New(storage.ID(id)),
Message: inlineMessage,
From: fromUser,
To: toUserDb,
To_SpecificUser: to_SpecificUser,
Memo: memo,
Amount: amount,
LanguageCode: ctx.Value("publicLanguageCode").(string),
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
}
2021-09-13 22:44:43 +02:00
// add result to persistent struct
bot.Cache.Set(inlineSend.ID, inlineSend, &store.Options{Expiration: 5 * time.Minute})
2021-09-13 22:44:43 +02:00
}
err = bot.Telegram.Answer(q, &tb.QueryResponse{
2021-09-13 22:44:43 +02:00
Results: results,
CacheTime: 1, // 60 == 1 minute, todo: make higher than 1 s in production
})
if err != nil {
log.Errorln(err)
return ctx, err
2021-09-13 22:44:43 +02:00
}
return ctx, nil
2021-09-13 22:44:43 +02:00
}
func (bot *TipBot) acceptInlineSendHandler(ctx intercept.Context) (intercept.Context, error) {
c := ctx.Callback()
to := LoadUser(ctx)
tx := &InlineSend{Base: storage.New(storage.ID(c.Data))}
mutex.LockWithContext(ctx, tx.ID)
defer mutex.UnlockWithContext(ctx, tx.ID)
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
sn, err := tx.Get(tx, bot.Bunt)
// immediatelly set intransaction to block duplicate calls
2021-09-10 08:52:44 +02:00
if err != nil {
// log.Errorf("[acceptInlineSendHandler] %s", err.Error())
return ctx, err
2021-09-13 22:44:43 +02:00
}
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
inlineSend := sn.(*InlineSend)
fromUser := inlineSend.From
2021-09-10 08:52:44 +02:00
if !inlineSend.Active {
2021-09-13 22:44:43 +02:00
log.Errorf("[acceptInlineSendHandler] inline send not active anymore")
return ctx, errors.Create(errors.NotActiveError)
2021-09-10 08:52:44 +02:00
}
2021-09-13 22:44:43 +02:00
defer inlineSend.Set(inlineSend, bot.Bunt)
2021-09-13 22:44:43 +02:00
2021-09-10 08:52:44 +02:00
amount := inlineSend.Amount
// check if this payment goes to a specific user
if inlineSend.To_SpecificUser {
if inlineSend.To.Telegram.ID != to.Telegram.ID {
// log.Infof("User %d is not User %d", inlineSend.To.Telegram.ID, to.Telegram.ID)
return ctx, errors.Create(errors.UnknownError)
}
} else {
// otherwise, we just set it to the user who has clicked
inlineSend.To = to
}
2021-09-13 22:44:43 +02:00
if fromUser.Telegram.ID == to.Telegram.ID {
bot.trySendMessage(fromUser.Telegram, Translate(ctx, "sendYourselfMessage"))
return ctx, errors.Create(errors.UnknownError)
2021-09-10 08:52:44 +02:00
}
toUserStrMd := GetUserStrMd(to.Telegram)
fromUserStrMd := GetUserStrMd(fromUser.Telegram)
toUserStr := GetUserStr(to.Telegram)
fromUserStr := GetUserStr(fromUser.Telegram)
2021-09-10 08:52:44 +02:00
// check if user exists and create a wallet if not
_, exists := bot.UserExists(to.Telegram)
2021-09-10 08:52:44 +02:00
if !exists {
log.Infof("[sendInline] User %s has no wallet.", toUserStr)
to, err = bot.CreateWalletForTelegramUser(to.Telegram)
2021-09-10 08:52:44 +02:00
if err != nil {
errmsg := fmt.Errorf("[sendInline] Error: Could not create wallet for %s", toUserStr)
log.Errorln(errmsg)
return ctx, err
2021-09-10 08:52:44 +02:00
}
}
2021-09-13 22:44:43 +02:00
// set inactive to avoid double-sends
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
inlineSend.Inactivate(inlineSend, bot.Bunt)
2021-09-10 08:52:44 +02:00
// todo: user new get username function to get userStrings
transactionMemo := fmt.Sprintf("💸 Send from %s to %s.", fromUserStr, toUserStr)
t := NewTransaction(bot, fromUser, to, amount, TransactionType("inline send"))
2021-09-10 08:52:44 +02:00
t.Memo = transactionMemo
success, err := t.Send()
if !success {
errMsg := fmt.Sprintf("[sendInline] Transaction failed: %s", err.Error())
2021-09-10 08:52:44 +02:00
log.Errorln(errMsg)
bot.tryEditMessage(c, i18n.Translate(inlineSend.LanguageCode, "inlineSendFailedMessage"), &tb.ReplyMarkup{})
return ctx, errors.Create(errors.UnknownError)
2021-09-10 08:52:44 +02:00
}
log.Infof("[💸 sendInline] Send from %s to %s (%d sat).", fromUserStr, toUserStr, amount)
2021-09-10 08:52:44 +02:00
inlineSend.Message = fmt.Sprintf("%s", fmt.Sprintf(i18n.Translate(inlineSend.LanguageCode, "inlineSendUpdateMessageAccept"), amount, fromUserStrMd, toUserStrMd))
2021-09-10 08:52:44 +02:00
memo := inlineSend.Memo
if len(memo) > 0 {
inlineSend.Message = inlineSend.Message + fmt.Sprintf(i18n.Translate(inlineSend.LanguageCode, "inlineSendAppendMemo"), memo)
2021-09-10 08:52:44 +02:00
}
if !to.Initialized {
inlineSend.Message += "\n\n" + fmt.Sprintf(i18n.Translate(inlineSend.LanguageCode, "inlineSendCreateWalletMessage"), GetUserStrMd(bot.Telegram.Me))
2021-09-10 08:52:44 +02:00
}
bot.tryEditMessage(c, inlineSend.Message, &tb.ReplyMarkup{})
2021-09-10 08:52:44 +02:00
// notify users
bot.trySendMessage(to.Telegram, fmt.Sprintf(i18n.Translate(to.Telegram.LanguageCode, "sendReceivedMessage"), fromUserStrMd, amount))
bot.trySendMessage(fromUser.Telegram, fmt.Sprintf(i18n.Translate(fromUser.Telegram.LanguageCode, "sendSentMessage"), amount, toUserStrMd))
2021-09-10 08:52:44 +02:00
if err != nil {
errmsg := fmt.Errorf("[sendInline] Error: Send message to %s: %s", toUserStr, err)
log.Warnln(errmsg)
2021-09-10 08:52:44 +02:00
}
return ctx, nil
2021-09-10 08:52:44 +02:00
}
func (bot *TipBot) cancelInlineSendHandler(ctx intercept.Context) (intercept.Context, error) {
c := ctx.Callback()
tx := &InlineSend{Base: storage.New(storage.ID(c.Data))}
mutex.LockWithContext(ctx, tx.ID)
defer mutex.UnlockWithContext(ctx, tx.ID)
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
// immediatelly set intransaction to block duplicate calls
sn, err := tx.Get(tx, bot.Bunt)
2021-09-10 08:52:44 +02:00
if err != nil {
log.Errorf("[cancelInlineSendHandler] %s", err.Error())
return ctx, err
2021-09-10 08:52:44 +02:00
}
bunt transaction generalization (#73) * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * refactor * remove balance from error * fix updated at * faucet errors * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline receive * use bunt tx for inline send * use bunt tx for pay * fix typo * readd context * some fixes * tigher error checking * add generalized bunt database transaction * use bunt tx for inline faucet and send * use bunt tx for inline send * add CreatedAt and UpdatedAt flags * check error in GetTransaction * add bunt transaction package and rename stuff * use transaction.Base as function receiver for transaction functions * add transaction.Base.Set function * fix tiptooltip bunt index * refactor * auto update updated at * fix updated at * faucet errors * refactor * remove balance from error * rebase fixes * fix imports * fix de string * fix receive and send * faucet language * remove pay constructor * release faucet * remove redundant inTransaction * remove constructor * remove constructor * transaction get pointer receiver * send race condition * inactivate in db Co-authored-by: LightningTipBot <hWOofXvq4m@mail.com>
2021-10-07 23:46:52 +02:00
inlineSend := sn.(*InlineSend)
if c.Sender.ID != inlineSend.From.Telegram.ID {
return ctx, errors.Create(errors.UnknownError)
2021-09-10 08:52:44 +02:00
}
bot.tryEditMessage(c, i18n.Translate(inlineSend.LanguageCode, "sendCancelledMessage"), &tb.ReplyMarkup{})
// set the inlineSend inactive
inlineSend.Active = false
return ctx, inlineSend.Set(inlineSend, bot.Bunt)
2021-09-10 08:52:44 +02:00
}