Skip to content

Commit

Permalink
add delete btn
Browse files Browse the repository at this point in the history
  • Loading branch information
mput committed Jul 25, 2024
1 parent ba01852 commit 96830fc
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (bot *Bot) Start() error {
dispatcher.AddHandler(handlers.NewCommand("/", wrapUserResponse(bot.comment, "comment")))
dispatcher.AddHandler(handlers.NewMessage(nil, wrapUserResponse(bot.proposeTransaction, "propose-transaction")))
dispatcher.AddHandler(handlers.NewCallback(isConfirmCallback, bot.confirmTransaction))
dispatcher.AddHandler(handlers.NewCallback(isDeleteCallback, bot.deleteTransaction))

// Start receiving updates.
err = updater.StartPolling(bot.bot, &ext.PollingOpts{
Expand Down Expand Up @@ -266,6 +267,10 @@ func isConfirmCallback(cb *gotgbot.CallbackQuery) bool {
return strings.HasPrefix(cb.Data, confirmPrefix)
}

func isDeleteCallback(cb *gotgbot.CallbackQuery) bool {
return strings.HasPrefix(cb.Data, deletePrefix)
}

func (bot *Bot) confirmTransaction(_ *gotgbot.Bot, ctx *ext.Context) error {
cq := ctx.CallbackQuery

Expand Down Expand Up @@ -348,6 +353,42 @@ func (bot *Bot) confirmTransaction(_ *gotgbot.Bot, ctx *ext.Context) error {
return nil
}

func (bot *Bot) deleteTransaction(_ *gotgbot.Bot, ctx *ext.Context) error {
cq := ctx.CallbackQuery

key := strings.TrimPrefix(cq.Data, deletePrefix)
err := bot.teledger.DeleteTransaction(key)

if err != nil {
_, _ = bot.bot.AnswerCallbackQuery(cq.Id, &gotgbot.AnswerCallbackQueryOpts{
ShowAlert: true,
Text: fmt.Sprintf("🛑️ Error!\n%s", err) ,
})

return nil
}

_, err = bot.bot.DeleteMessage(cq.Message.GetChat().Id, cq.Message.GetMessageId(), nil)

if err != nil {
slog.Error("unable to edit message", "error", err)
_, _ = bot.bot.AnswerCallbackQuery(cq.Id, &gotgbot.AnswerCallbackQueryOpts{
ShowAlert: true,
Text: fmt.Sprintf("Can't delete message: %v", err),
})
return nil
}

_, err = bot.bot.AnswerCallbackQuery(cq.Id, &gotgbot.AnswerCallbackQueryOpts{
Text: "✔️ confirmed",
})

if err != nil {
slog.Error("unable to answer callback query", "error", err)
}
return nil
}

func (bot *Bot) showReport(ctx *ext.Context) (string, *gotgbot.SendMessageOpts, error) {
cq := ctx.CallbackQuery
_, err := bot.bot.AnswerCallbackQuery(cq.Id, &gotgbot.AnswerCallbackQueryOpts{
Expand Down

0 comments on commit 96830fc

Please sign in to comment.