Skip to content

Commit

Permalink
Force start2 (#21)
Browse files Browse the repository at this point in the history
* force-init only on / commands

* return statemet

* anytext handler in all commands

* comment

* warning

* get rid of init bot wallet error
  • Loading branch information
LightningTipBot authored Aug 25, 2021
1 parent 4bf0e05 commit 3fe903b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 17 deletions.
3 changes: 2 additions & 1 deletion balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions help.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,30 @@ 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."
)

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)
}
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 9 additions & 7 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3fe903b

Please sign in to comment.