Skip to content

Commit

Permalink
[WIP] respond with balance
Browse files Browse the repository at this point in the history
  • Loading branch information
mput committed Feb 13, 2024
1 parent 47f6954 commit 3751a76
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
36 changes: 36 additions & 0 deletions app/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bot
import (
"fmt"
"log"
"os"
"time"

"github.com/PaulSonOfLars/gotgbot/v2"
Expand All @@ -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"`
}
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions app/bot/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down
7 changes: 4 additions & 3 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions todo.org
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3751a76

Please sign in to comment.