From 3fe903bc51ba8d3b1cd64f63a4bbaf8d6980dc21 Mon Sep 17 00:00:00 2001 From: LightningTipBot <88730856+LightningTipBot@users.noreply.github.com> Date: Wed, 25 Aug 2021 02:13:42 +0200 Subject: [PATCH] Force start2 (#21) * force-init only on / commands * return statemet * anytext handler in all commands * comment * warning * get rid of init bot wallet error --- balance.go | 3 ++- help.go | 7 ++++--- invoice.go | 3 ++- pay.go | 3 ++- send.go | 3 ++- start.go | 15 +++++++++++++-- text.go | 16 +++++++++------- tip.go | 3 ++- 8 files changed, 36 insertions(+), 17 deletions(-) diff --git a/balance.go b/balance.go index 7d7b4d2c..f7aef6eb 100644 --- a/balance.go +++ b/balance.go @@ -9,7 +9,8 @@ import ( ) func (bot TipBot) balanceHandler(m *tb.Message) { - log.Infof("[%s:%d %s:%d] %s", m.Chat.Title, m.Chat.ID, GetUserStr(m.Sender), m.Sender.ID, m.Text) + // check and print all commands + bot.anyTextHandler(m) // reply only in private message if m.Chat.Type != tb.ChatPrivate { // delete message diff --git a/help.go b/help.go index 8f56ac63..a39b8ff1 100644 --- a/help.go +++ b/help.go @@ -1,7 +1,6 @@ package main import ( - log "github.com/sirupsen/logrus" tb "gopkg.in/tucnak/telebot.v2" ) @@ -36,7 +35,8 @@ const ( ) func (bot TipBot) helpHandler(m *tb.Message) { - log.Infof("[%s:%d %s:%d] %s", m.Chat.Title, m.Chat.ID, GetUserStr(m.Sender), m.Sender.ID, m.Text) + // check and print all commands + bot.anyTextHandler(m) if !m.Private() { // delete message NewMessage(m).Dispose(0, bot.telegram) @@ -46,7 +46,8 @@ func (bot TipBot) helpHandler(m *tb.Message) { } func (bot TipBot) infoHandler(m *tb.Message) { - log.Infof("[%s:%d %s:%d] %s", m.Chat.Title, m.Chat.ID, GetUserStr(m.Sender), m.Sender.ID, m.Text) + // check and print all commands + bot.anyTextHandler(m) if !m.Private() { // delete message NewMessage(m).Dispose(0, bot.telegram) diff --git a/invoice.go b/invoice.go index 7263f8e8..1b73220d 100644 --- a/invoice.go +++ b/invoice.go @@ -41,7 +41,8 @@ func helpInvoiceUsage(errormsg string) string { // } func (bot TipBot) invoiceHandler(m *tb.Message) { - log.Infof("[%s:%d %s:%d] %s", m.Chat.Title, m.Chat.ID, GetUserStr(m.Sender), m.Sender.ID, m.Text) + // check and print all commands + bot.anyTextHandler(m) if m.Chat.Type != tb.ChatPrivate { // delete message NewMessage(m).Dispose(0, bot.telegram) diff --git a/pay.go b/pay.go index 6e516ffa..3e32de91 100644 --- a/pay.go +++ b/pay.go @@ -37,7 +37,8 @@ func helpPayInvoiceUsage(errormsg string) string { // confirmPaymentHandler invoked on "/pay lnbc..." command func (bot TipBot) confirmPaymentHandler(m *tb.Message) { - log.Infof("[%s:%d %s:%d] %s", m.Chat.Title, m.Chat.ID, GetUserStr(m.Sender), m.Sender.ID, m.Text) + // check and print all commands + bot.anyTextHandler(m) if m.Chat.Type != tb.ChatPrivate { // delete message NewMessage(m).Dispose(0, bot.telegram) diff --git a/send.go b/send.go index 9f2fe3d3..49bbfa1c 100644 --- a/send.go +++ b/send.go @@ -52,7 +52,8 @@ func SendCheckSyntax(m *tb.Message) (bool, string) { // confirmPaymentHandler invoked on "/send 123 @user" command func (bot *TipBot) confirmSendHandler(m *tb.Message) { - log.Infof("[%s:%d %s:%d] %s", m.Chat.Title, m.Chat.ID, GetUserStr(m.Sender), m.Sender.ID, m.Text) + // check and print all commands + bot.anyTextHandler(m) // If the send is a reply, then trigger /tip handler if m.IsReply() { bot.tipHandler(m) diff --git a/start.go b/start.go index 02d1d769..d9f17fb9 100644 --- a/start.go +++ b/start.go @@ -15,6 +15,7 @@ import ( const ( startSettingWalletMessage = "🧮 Setting up your wallet..." startWalletReadyMessage = "✅ *Your wallet is ready.*" + startWalletErrorMessage = "🚫 Error initializing your wallet. Try again later." startNoUsernameMessage = "☝️ It looks like you don't have a Telegram @username yet. That's ok, you don't need one to use this bot. However, to make better use of your wallet, set up a username in the [Telegram settings](https://telegram.org/faq#q-what-are-usernames-how-do-i-get-one). Then, enter /balance so the bot can update its record of you." ) @@ -22,18 +23,22 @@ func (bot TipBot) startHandler(m *tb.Message) { if !m.Private() { return } - bot.helpHandler(m) + // ATTENTION: DO NOT CALL ANY HANDLER BEFORE THE WALLET IS CREATED + // WILL RESULT IN AN ENDLESS LOOP OTHERWISE + // bot.helpHandler(m) + bot.telegram.Send(m.Sender, helpMessage, tb.NoPreview) log.Printf("[/start] User: %s (%d)\n", m.Sender.Username, m.Sender.ID) - walletCreationMsg, err := bot.telegram.Send(m.Sender, startSettingWalletMessage) err = bot.initWallet(m.Sender) if err != nil { log.Errorln(fmt.Sprintf("[startHandler] Error with initWallet: %s", err.Error())) + bot.telegram.Edit(walletCreationMsg, startWalletErrorMessage) return } bot.telegram.Edit(walletCreationMsg, startWalletReadyMessage) bot.balanceHandler(m) + // send the user a warning about the fact that they need to set a username if len(m.Sender.Username) == 0 { bot.telegram.Send(m.Sender, startNoUsernameMessage, tb.NoPreview) } @@ -63,6 +68,12 @@ func (bot TipBot) initWallet(tguser *tb.User) error { log.Errorln(fmt.Sprintf("[initWallet] error updating user: %s", err.Error())) return err } + } else if user.Initialized { + // wallet is already initialized + return nil + } else { + err = fmt.Errorf("could not initialize wallet") + return err } return nil } diff --git a/text.go b/text.go index 5fa3c924..1fa7072d 100644 --- a/text.go +++ b/text.go @@ -9,18 +9,20 @@ import ( tb "gopkg.in/tucnak/telebot.v2" ) +const ( + initWalletMessage = "You don't have a wallet yet. Enter */start*" +) + func (bot TipBot) anyTextHandler(m *tb.Message) { log.Infof("[%s:%d %s:%d] %s", m.Chat.Title, m.Chat.ID, GetUserStr(m.Sender), m.Sender.ID, m.Text) if m.Chat.Type != tb.ChatPrivate { return } - if strings.HasPrefix(m.Text, "/") { - // check if user is in database, if not, initialize wallet - if !bot.UserHasWallet(m.Sender) { - log.Infof("User %s has no wallet, force-initializing", GetUserStr(m.Sender)) - bot.startHandler(m) - return - } + + // check if user is in database, if not, initialize wallet + if !bot.UserHasWallet(m.Sender) { + bot.startHandler(m) + return } // could be an invoice diff --git a/tip.go b/tip.go index 119014bf..201328d9 100644 --- a/tip.go +++ b/tip.go @@ -40,7 +40,8 @@ func TipCheckSyntax(m *tb.Message) (bool, string) { } func (bot *TipBot) tipHandler(m *tb.Message) { - log.Infof("[%s:%d %s:%d] %s", m.Chat.Title, m.Chat.ID, GetUserStr(m.Sender), m.Sender.ID, m.Text) + // check and print all commands + bot.anyTextHandler(m) // only if message is a reply if !m.IsReply() { NewMessage(m).Dispose(0, bot.telegram)