Skip to content

Commit

Permalink
Merge pull request #69 from LucaBernstein/29-suggestion-list
Browse files Browse the repository at this point in the history
Simplify suggestions listing
  • Loading branch information
LucaBernstein authored Dec 22, 2021
2 parents 4085bcb + ca14df3 commit 0662ca1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
25 changes: 25 additions & 0 deletions bot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ func (bc *BotController) AddBotAndStart(b IBot) {

b.Handle(tb.OnText, bc.handleTextState)

// Todo: Add generic callback handler
// Route callback by ID splits
bc.Bot.Handle(&btnSuggListAccFrom, func(c *tb.Callback) {
bc.Logf(DEBUG, nil, "Handling callback on button. Chat: %d", c.Message.Chat.ID)
c.Message.Text = "/suggestions list accFrom"
// TODO: What happens in group chats?
c.Message.Sender = &tb.User{ID: int(c.Message.Chat.ID)} // hack to send chat user a message (in private chats userId = chatId)
bc.suggestionsHandler(c.Message)
bc.Bot.Respond(c, &tb.CallbackResponse{}) // Always respond
})
bc.Bot.Handle(&btnSuggListAccTo, func(c *tb.Callback) {
bc.Logf(DEBUG, nil, "Handling callback on button. Chat: %d", c.Message.Chat.ID)
c.Message.Text = "/suggestions list accTo"
c.Message.Sender = &tb.User{ID: int(c.Message.Chat.ID)}
bc.suggestionsHandler(c.Message)
bc.Bot.Respond(c, &tb.CallbackResponse{}) // Always respond
})
bc.Bot.Handle(&btnSuggListTxDesc, func(c *tb.Callback) {
bc.Logf(DEBUG, nil, "Handling callback on button. Chat: %d", c.Message.Chat.ID)
c.Message.Text = "/suggestions list txDesc"
c.Message.Sender = &tb.User{ID: int(c.Message.Chat.ID)}
bc.suggestionsHandler(c.Message)
bc.Bot.Respond(c, &tb.CallbackResponse{}) // Always respond
})

bc.Logf(TRACE, nil, "Starting bot '%s'", b.Me().Username)

if bc.CronScheduler != nil {
Expand Down
3 changes: 3 additions & 0 deletions bot/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func (b *MockBot) Send(to tb.Recipient, what interface{}, options ...interface{}
b.AllLastSentWhat = append(b.AllLastSentWhat, what)
return nil, nil
}
func (b *MockBot) Respond(c *tb.Callback, resp ...*tb.CallbackResponse) error {
return nil
}
func (b *MockBot) Me() *tb.User {
return &tb.User{Username: "Test bot"}
}
Expand Down
7 changes: 6 additions & 1 deletion bot/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ func CreateBot(bc *BotController) IBot {

poller := &tb.LongPoller{Timeout: 20 * time.Second}
userGuardPoller := tb.NewMiddlewarePoller(poller, func(upd *tb.Update) bool {
message := upd.Message
if message == nil && upd.Callback != nil {
bc.Logf(TRACE, nil, "Message was nil. Seems to have been a callback. Proceeding.")
return true
}
// TODO: Start goroutine to update data?
err := bc.Repo.EnrichUserData(upd.Message)
err := bc.Repo.EnrichUserData(message)
if err != nil {
bc.Logf(ERROR, nil, "Error encountered in middlewarePoller: %s", err.Error())
}
Expand Down
12 changes: 11 additions & 1 deletion bot/suggestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import (
tb "gopkg.in/tucnak/telebot.v2"
)

var (
suggestionsMenu = &tb.ReplyMarkup{}
btnSuggListAccFrom = suggestionsMenu.Data("/suggestions list accFrom", "btnSuggestionsListAccFrom")
btnSuggListAccTo = suggestionsMenu.Data("/suggestions list accTo", "btnSuggestionsListAccTo")
btnSuggListTxDesc = suggestionsMenu.Data("/suggestions list txDesc", "btnSuggestionsListTxDesc")
)

func (bc *BotController) suggestionsHandler(m *tb.Message) {
sc := h.MakeSubcommandHandler("/"+CMD_SUGGEST, true)
sc.
Expand All @@ -26,12 +33,15 @@ func (bc *BotController) suggestionsHelp(m *tb.Message, err error) {
if err != nil {
errorMsg += fmt.Sprintf("Error executing your command: %s\n\n", err.Error())
}

suggestionsMenu.Inline(suggestionsMenu.Row(btnSuggListAccFrom, btnSuggListAccTo, btnSuggListTxDesc))

_, err = bc.Bot.Send(m.Sender, errorMsg+fmt.Sprintf(`Usage help for /suggestions:
/suggestions list <type>
/suggestions add <type> <value>
/suggestions rm <type> [value]
Parameter <type> is one from: [%s]`, suggestionTypes))
Parameter <type> is one from: [%s]`, suggestionTypes), suggestionsMenu)
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
Expand Down
5 changes: 5 additions & 0 deletions bot/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type IBot interface {
Start()
Handle(endpoint interface{}, handler interface{})
Send(to tb.Recipient, what interface{}, options ...interface{}) (*tb.Message, error)
Respond(c *tb.Callback, resp ...*tb.CallbackResponse) error
// custom by me:
Me() *tb.User
}
Expand All @@ -29,6 +30,10 @@ func (b *Bot) Send(to tb.Recipient, what interface{}, options ...interface{}) (*
return b.bot.Send(to, what, options...)
}

func (b *Bot) Respond(c *tb.Callback, resp ...*tb.CallbackResponse) error {
return b.bot.Respond(c, resp...)
}

func (b *Bot) Me() *tb.User {
return b.bot.Me
}

0 comments on commit 0662ca1

Please sign in to comment.