From 3751a761a514fc67711173cd9d4cd7cc12bb59c1 Mon Sep 17 00:00:00 2001 From: mput Date: Tue, 13 Feb 2024 01:10:28 +0100 Subject: [PATCH] [WIP] respond with balance --- app/bot/bot.go | 36 ++++++++++++++++++++++++++++++++++++ app/bot/repo_test.go | 16 ++++++++-------- app/main.go | 7 ++++--- todo.org | 4 ++++ 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/app/bot/bot.go b/app/bot/bot.go index 8e22d55..20861cd 100644 --- a/app/bot/bot.go +++ b/app/bot/bot.go @@ -3,6 +3,7 @@ package bot import ( "fmt" "log" + "os" "time" "github.com/PaulSonOfLars/gotgbot/v2" @@ -16,6 +17,12 @@ type Opts struct { Token string `long:"token" env:"TOKEN" required:"true" description:"telegram bot token"` } `group:"telegram" namespace:"telegram" env-namespace:"TELEGRAM"` + Github struct { + URL string `long:"url" env:"URL" required:"true" description:"github repo url"` + Token string `long:"token" env:"TOKEN" required:"true" description:"fine-grained personal access tokens for repo with RW Contents scope"` + MainLedgerFile string `long:"main-ledger-file" env:"MAIN_LEDGER_FILE" required:"true" description:"main ledger file path from the repo root"` + } `group:"github" namespace:"github" env-namespace:"GITHUB"` + URL string `long:"url" env:"URL" required:"true" description:"bot url"` Debug bool `long:"debug" env:"DEBUG" description:"debug mode"` } @@ -41,6 +48,7 @@ func (opts *Opts) Execute() error { dispatcher.AddHandler(handlers.NewCommand("start", start)) + dispatcher.AddHandler(handlers.NewCommand("bal", opts.bal)) dispatcher.AddHandler(handlers.NewMessage(nil, message)) // Start receiving updates. @@ -72,6 +80,34 @@ func start(b *gotgbot.Bot, ctx *ext.Context) error { return nil } + +func (opts *Opts) bal(b *gotgbot.Bot, ctx *ext.Context) error { + msg := ctx.EffectiveMessage + log.Printf("[INFO] balance request. user=%s\n", msg.From.Username) + + dir, err := os.MkdirTemp("", msg.From.Username) + if err != nil { + log.Printf("[ERROR] unable to create temp file: %v\n", err) + return fmt.Errorf("unable to create temp file: %w", err) + } + log.Printf("[DEBUG] temp dir: %s\n", dir) + + defer os.RemoveAll(dir) + + balance, err := ExecLedgerCmd(opts.Github.URL, opts.Github.Token, dir, opts.Github.MainLedgerFile, "bal") + if err != nil { + log.Printf("[ERROR] unable to get balance: %v\n", err) + b.SendMessage(msg.Chat.Id, fmt.Sprintf("unable to get balance: %v", err), nil) + return nil + } + + if _, err := b.SendMessage(msg.Chat.Id, fmt.Sprintf("```%s```", balance), &gotgbot.SendMessageOpts{ParseMode: "Markdown"}); err != nil { + return fmt.Errorf("unable to send message: %w", err) + } + return nil +} + + func message(b *gotgbot.Bot, ctx *ext.Context) error { msg := ctx.EffectiveMessage log.Printf("[INFO] %s has sent a message to the bot\n", msg.From.Username) diff --git a/app/bot/repo_test.go b/app/bot/repo_test.go index 450a475..7944c1c 100644 --- a/app/bot/repo_test.go +++ b/app/bot/repo_test.go @@ -14,13 +14,13 @@ func TestCloneRepo(t *testing.T) { godotenv.Load("../../.env") tmpDir := t.TempDir() - gitURL := os.Getenv("GIT_URL") + gitURL := os.Getenv("GITHUB_URL") if gitURL == "" { t.Errorf("GIT_URL is not set") return } - gitAccessToken := os.Getenv("GIT_ACCESS_TOKEN") + gitAccessToken := os.Getenv("GITHUB_TOKEN") if gitAccessToken == "" { t.Errorf("GIT_ACCESS_TOKEN is not set") return @@ -44,21 +44,21 @@ func TestExecLedgerCmd(t *testing.T) { godotenv.Load("../../.env.test") tmpDir := t.TempDir() - gitURL := os.Getenv("GIT_URL") + gitURL := os.Getenv("GITHUB_URL") if gitURL == "" { t.Errorf("GIT_URL is not set") return } - gitAccessToken := os.Getenv("GIT_ACCESS_TOKEN") - if gitAccessToken == "" { + gitToken := os.Getenv("GITHUB_TOKEN") + if gitToken == "" { t.Errorf("GIT_ACCESS_TOKEN is not set") return } t.Run("happy path", func(t *testing.T) { - res, err := ExecLedgerCmd(gitURL, gitAccessToken, tmpDir, "main.ledger", "bal") + res, err := ExecLedgerCmd(gitURL, gitToken, tmpDir, "main.ledger", "bal") if err != nil { t.Errorf("Command execution error: %v", err) return @@ -91,7 +91,7 @@ func TestExecLedgerCmd(t *testing.T) { }) t.Run("wrong file", func(t *testing.T) { - _, err := ExecLedgerCmd(gitURL, gitAccessToken, tmpDir, "not-exists.ledger", "bal") + _, err := ExecLedgerCmd(gitURL, gitToken, tmpDir, "not-exists.ledger", "bal") if err == nil { t.Errorf("Error excpected") @@ -106,7 +106,7 @@ func TestExecLedgerCmd(t *testing.T) { t.Run("wrong command", func(t *testing.T) { - _, err := ExecLedgerCmd(gitURL, gitAccessToken, tmpDir, "main.ledger", "unknown-command") + _, err := ExecLedgerCmd(gitURL, gitToken, tmpDir, "main.ledger", "unknown-command") if err == nil { t.Errorf("Error excpected") diff --git a/app/main.go b/app/main.go index 5485958..995de1b 100644 --- a/app/main.go +++ b/app/main.go @@ -17,9 +17,10 @@ func main() { opts := bot.Opts{} _, err := flags.Parse(&opts) if err != nil { - if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp { - log.Printf("[ERROR] %v", err) - } + // if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp { + // log.Printf("[ERROR] %v", err) + // } + log.Printf("[ERROR] %v", err) os.Exit(1) } log.Printf("[DEBUG] opts: %+v", opts) diff --git a/todo.org b/todo.org index 5dbfffe..022aad1 100644 --- a/todo.org +++ b/todo.org @@ -7,3 +7,7 @@ The application which only works with supplied token and file name. The registra *** [X] Parse the resutl with either ledger-cli or go ledger implementation *** [ ] Bind that all to work as a bot balance command. *** [ ] Deploy, and use +** TODO Use slog for logs +** TODO Embed commands in menu +** TODO Add ability to add transactions through inline keyboard +** TODO Add ability to add comments