Skip to content

Commit

Permalink
basic reports suport
Browse files Browse the repository at this point in the history
  • Loading branch information
mput committed Jul 3, 2024
1 parent 085ee6b commit f6f8bdd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
70 changes: 67 additions & 3 deletions app/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func NewBot(opts *Opts) (*Bot, error) {
ldgr := ledger.NewLedger(rs, llmGenerator)
tel := teledger.NewTeledger(ldgr)

err = tel.Init()
if err != nil {
return nil, fmt.Errorf("unable to init teledger: %v", err)
}

return &Bot{
opts: opts,
teledger: tel,
Expand All @@ -62,6 +67,7 @@ func NewBot(opts *Opts) (*Bot, error) {

func (bot *Bot) Start() error {
defaultCommands := []gotgbot.BotCommand{
{Command: "reports", Description: "Show available reports"},
{Command: "balance", Description: "Show balance"},
{Command: "version", Description: "Show version"},
}
Expand All @@ -83,12 +89,18 @@ func (bot *Bot) Start() error {
updater := ext.NewUpdater(dispatcher, nil)


dispatcher.AddHandler(handlers.NewCommand("start", wrapUserResponse(start, "start")))
dispatcher.AddHandler(handlers.NewCommand("version", wrapUserResponse(bot.vesrion, "version")))



dispatcher.AddHandler(handlers.NewCommand("reports", wrapUserResponse(bot.showAvailableReports, "reports")))
dispatcher.AddHandler(handlers.NewCallback(isReportCallback, wrapUserResponse(bot.showReport, "show-report")))

dispatcher.AddHandler(handlers.NewCommand("/", wrapUserResponse(bot.comment, "comment")))
dispatcher.AddHandler(handlers.NewCommand("balance", wrapUserResponse(bot.bal, "balance")))
dispatcher.AddHandler(handlers.NewMessage(nil, wrapUserResponse(bot.proposeTransaction, "propose-transaction")))

dispatcher.AddHandler(handlers.NewCommand("start", wrapUserResponse(start, "start")))
dispatcher.AddHandler(handlers.NewCommand("version", wrapUserResponse(bot.vesrion, "version")))
dispatcher.AddHandler(handlers.NewCommand("balance", wrapUserResponse(bot.bal, "balance")))


// Start receiving updates.
Expand Down Expand Up @@ -198,3 +210,55 @@ func (bot *Bot) proposeTransaction(ctx *ext.Context) (string, *gotgbot.SendMessa

return transaction, &gotgbot.SendMessageOpts{ParseMode: "MarkdownV2"}, nil
}


func (bot *Bot) showAvailableReports(ctx *ext.Context) (string, *gotgbot.SendMessageOpts, error) {
reports := bot.teledger.Ledger.Config.Reports

inlineKeyboard := [][]gotgbot.InlineKeyboardButton{}

for _, report := range reports {
inlineKeyboard = append(inlineKeyboard, []gotgbot.InlineKeyboardButton{
{
Text: report.Title,
CallbackData: fmt.Sprintf("report:%s", report.Title),
},
})
}

opts := &gotgbot.SendMessageOpts{
ParseMode: "MarkdownV2",
ReplyMarkup: gotgbot.InlineKeyboardMarkup{
InlineKeyboard: inlineKeyboard,
},

}

return "Available reports:", opts, nil
}

func isReportCallback(cq *gotgbot.CallbackQuery) bool {
return true
}

func (bot *Bot) showReport(ctx *ext.Context) (string, *gotgbot.SendMessageOpts, error) {
cq := ctx.CallbackQuery
_, err := bot.bot.AnswerCallbackQuery(cq.Id, &gotgbot.AnswerCallbackQueryOpts{
Text: "✔️",
})

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


reportTitle := strings.TrimPrefix(cq.Data, "report:")
report, err := bot.teledger.Report(reportTitle)

if err != nil {
return fmt.Sprintf("Error: %v", err), nil, nil
}


return fmt.Sprintf("```\n%s\n```", report), &gotgbot.SendMessageOpts{ParseMode: "MarkdownV2"}, nil
}
30 changes: 25 additions & 5 deletions app/teledger/teledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
// Teledger is the service that handles all the
// operations related to the Ledger files
type Teledger struct {
ledger *ledger.Ledger
Ledger *ledger.Ledger
}


func NewTeledger(ldgr *ledger.Ledger) *Teledger {
return &Teledger{
ledger: ldgr,
Ledger: ldgr,
}
}

Expand All @@ -41,15 +41,35 @@ func (tel *Teledger) AddComment(comment string) (string, error) {
now.Format("2006-01-02"),
)

res, err := tel.ledger.AddComment(commitLine)
res, err := tel.Ledger.AddComment(commitLine)
if err != nil {
return "", err
}
return res, nil
}

func (tel *Teledger) Balance() (string, error) {
return tel.ledger.Execute("bal")
return tel.Ledger.Execute("bal")
}

func (tel *Teledger) Report(reportTitle string) (string, error) {
var reportArgs []string
for _, report := range tel.Ledger.Config.Reports {
if report.Title == reportTitle {
reportArgs = report.Command
break
}
}
if len(reportArgs) == 0 {
return "", fmt.Errorf("Report not found")
}
return tel.Ledger.Execute(reportArgs...)
}


func (tel *Teledger) Init() error {
_, err := tel.Ledger.Execute("bal")
return err
}


Expand All @@ -63,7 +83,7 @@ func inBacktick(s string) string {
// Store the transaction in a state, so the user can confirm
// or reject it.
func (tel *Teledger) ProposeTransaction(desc string) (string, error) {
wasGenerated, tr, err := tel.ledger.AddOrProposeTransaction(desc, 2)
wasGenerated, tr, err := tel.Ledger.AddOrProposeTransaction(desc, 2)
if wasGenerated {
if err == nil {
return inBacktick(tr.Format(false)), nil
Expand Down
2 changes: 1 addition & 1 deletion app/teledger/teledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestTeledger_AddComment(t *testing.T) {
l := ledger.NewLedger(r, nil)

tldgr := &Teledger{
ledger: l,
Ledger: l,
}

_, err := tldgr.AddComment("This is a comment\n multiline")
Expand Down

0 comments on commit f6f8bdd

Please sign in to comment.