Skip to content

Commit

Permalink
various enchantments and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mput committed Jul 9, 2024
1 parent ca65555 commit ec9086d
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 41 deletions.
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

start:
. ./.env.dev
go run app/main.go
. ./.env.dev && go run app/main.go

test:
. ./.env.test
go test -v ./...
. ./.env.test && go test -v ./...
4 changes: 2 additions & 2 deletions app/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (bot *Bot) proposeTransaction(ctx *ext.Context) (string, *gotgbot.SendMessa
}


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

inlineKeyboard := [][]gotgbot.InlineKeyboardButton{}
Expand All @@ -239,7 +239,7 @@ func (bot *Bot) showAvailableReports(ctx *ext.Context) (string, *gotgbot.SendMes
return "Available reports:", opts, nil
}

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

Expand Down
14 changes: 6 additions & 8 deletions app/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ type Report struct {
}

type Config struct {
Reports []Report `yaml:"reports"`
MainFile string `yaml:"mainFile"`
PromptTemplate string `yaml:"promptTemplate"`
StrictMode bool `yaml:"strict"`
Version string `yaml:"version"`
MainFile string `yaml:"mainFile"` // default: main.ledger, not required
StrictMode bool `yaml:"strict"` // whether to allow non existing accounts and commodities
PromptTemplate string `yaml:"promptTemplate"` // not required
Version string `yaml:"version"` // do not include in documentation
Reports []Report `yaml:"reports"` //
}

func NewLedger(rs repo.Service, gen TransactionGenerator) *Ledger {
Expand Down Expand Up @@ -205,9 +205,6 @@ func (l *Ledger) addTransaction(transaction string) error {
if err != nil {
return fmt.Errorf("unable to write main ledger file: %v", err)
}
if err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -342,6 +339,7 @@ func NewOpenAITransactionGenerator(token string) *OpenAITransactionGenerator {
}
}

//nolint:gocritic
func (b OpenAITransactionGenerator) GenerateTransaction(promptCtx PromptCtx) (Transaction, error) {
var buf bytes.Buffer
prTmp := template.Must(template.New("letter").Parse(defaultPromtpTemplate))
Expand Down
13 changes: 6 additions & 7 deletions app/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestLedger_ProposeTransaction(t *testing.T) {
var mockedTransactionGenerator *TransactionGeneratorMock

mockedTransactionGenerator = &TransactionGeneratorMock{
GenerateTransactionFunc: func(p PromptCtx) (mocktr Transaction,err error) {
GenerateTransactionFunc: func(_ PromptCtx) (mocktr Transaction,err error) {
mockCall++
dt, _ := time.Parse(time.RFC3339, "2014-11-12T11:45:26.371Z")
// On the first attempt, return transaction that is not valid
Expand All @@ -174,12 +174,12 @@ func TestLedger_ProposeTransaction(t *testing.T) {
Description: "My tr",
Comment: "invalid transaction",
Postings: []Posting{
Posting{
{
Account: "cash",
Amount: -3000.43,
Currency: "EUR",
},
Posting{
{
Account: "taxi",
Amount: 3000.43,
Currency: "EUR",
Expand All @@ -192,21 +192,20 @@ func TestLedger_ProposeTransaction(t *testing.T) {
Comment: "valid transaction\n22 multiple lines",
Description: "Tacos",
Postings: []Posting{
Posting{
{
Account: "Assets:Cash",
Amount: -3000.43,
Currency: "EUR",
},
Posting{
{
Account: "Food",
Amount: 3000.43,
Currency: "EUR",
},
},
}
}
return

return mocktr, nil
},
}

Expand Down
10 changes: 5 additions & 5 deletions app/ledger/templates/default_prompt.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Your goal is to propose a transaction in the ledger cli format.
Your responses MUST be in JSON and adhere to the Transaction struct ONLY with no additional narrative or markup, backquotes or anything.
Your goal is to propose a transaction in the Ledger CLI format.
Your responses MUST be in JSON and adhere to the Transaction struct ONLY, with no additional narrative, markup, backquotes, or anything else.

Bellow is the list of accounts you MUST use in your transaction:
Below is the list of accounts you MUST use in your transaction:
{{range .Accounts}}
"{{.}}"
{{end}}
Use {{ index .Accounts 0}} as default assets account if nothing else is specified in user request

Today is {{.Datetime}}
All descriptions should be in English.
Expand All @@ -23,11 +24,10 @@ type Posting struct {
Currency string json:"currency" // The currency of the amount
}

Assume number in user input to be a price
Assume numbers in user input to be a price, not amount.
Use {{ index .Commodities 0}} as the default currency if nothing else is specified in user request.
Another possible currency are:
{{range .Commodities}}
"{{.}}"
{{end}}

Use {{ index .Accounts 0}} as default assets account if nothing else is specified in user request
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ require (
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.11.0
github.com/joho/godotenv v1.5.1
github.com/dustin/go-humanize v1.0.1
github.com/sashabaranov/go-openai v1.26.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sashabaranov/go-openai v1.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
Expand Down
73 changes: 65 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,68 @@
## Teledger
Service which should combine powerful double-entry accounting system [ledger](https://ledger-cli.org/), with reliability of a git as a Ledger file storage, and convenience of data entry using Telegram and OpenAI LLM.
# Teledger
Service which should combine powerful double-entry accounting system [ledger](https://ledger-cli.org/), with reliability of a Git as a Ledger file storage, and convenience of data entry using Telegram and OpenAI LLM.

## How It Works

Teledger is a Telegram bot service that manages Plain-Text Accounting files stored in a Git repository.
It provides a convenient way to add transactions on the go using natural language.
Pro `ledger-cli` users can still manually edit Ledger files using a text editor and ledger-cli utility for complex transactions or other changes where the bot interface may not be suitable.

### Process Overview

- **Initiate Transaction**: You send a message to Teledger describing the transaction.
- **Data Extraction**: Teledger clones your Git repository and extracts necessary information such as available accounts and commodities from the Ledger files.
- **Generate and Validate Transaction**: Teledger constructs a prompt and consults ChatGPT to generate a new transaction entry. This transaction is validated against the data in your Ledger repository to ensure accuracy.
- **Commit Changes**: Once the transaction passes validation, it is committed and pushed back into the repository, updating your financial records automatically.

## Ledger Template Repository

A [template repository](https://github.com/mput/teledger-test) is available to help set up a new Ledger project.

## Configuration

### Service Configuration

Configure the service by setting the following environment variables or directly passing them as command-line arguments:

- **Telegram**:
- `--telegram.token=`, `$TELEGRAM_TOKEN` - Telegram bot token.

- **GitHub**:
- `--github.url=`, `$GITHUB_URL` - GitHub repository URL.
- `--github.token=`, `$GITHUB_TOKEN` - Fine-grained personal access tokens for the repository with RW Contents scope.

- **OpenAI**:
- `--openai.token=`, `$OPENAI_TOKEN` - OpenAI API token.

### Ledger File Configuration

The `teledger.yaml` configuration file may be placed in the root of your Ledger project repository.
It includes settings specific to your ledger environment. Here is the structure of the expected YAML file:

- **mainFile**: Specifies the main ledger file name, default is `main.ledger`.
- **strict**: Boolean to allow or disallow non-existing accounts and commodities.
- **promptTemplate**: Template for generating prompts, optional.
- **reports**: Array of report configurations where each report includes:
- **title**: Description of the report.
- **command**: Ledger-cli command array to generate the report.

Example configuration in [`teledger.yaml`](https://github.com/mput/teledger-test/blob/main/teledger.yaml):
```yaml
strict: true
reports:
- title: Expenses This Month
command: [bal, ^Expenses, --cleared, --period, "this month", -X, EUR]
- title: Expenses Last Month
command: [bal, ^Expenses, --cleared, --period, "last month", -X, EUR]
- title: 💶 Assets
command: [bal, ^Assets]
```
## Demo
TODO
## Deploment
A Docker image for Teledger is available at [Docker Hub](https://hub.docker.com/repository/docker/mput/teledger/general).
## Development
Create `.env.dev` by copying `.env.example` and fill in the values.
Expand All @@ -17,9 +80,3 @@ Start the service
go run app/main.go
```


## TODO
- [ ] Propose transactions with openAI
- [ ] Commit transaction with openAI (By pressing confirm btn)
- [ ] Add transaction directly when a transaction typed instead of openAI
- [ ] Add templates for reports with yaml in repo
9 changes: 6 additions & 3 deletions todo.org
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
* Main
** [ ] Add Transaction To a ledger repository
- [X] Basic (transaction LLM completion and valid transaction addition)
- [ ] Add to log with chat button
- [ ] Delete with button
- [ ] Add transaction to log with chat callback button
- [ ] Add button to delete transaction
** [ ] Configuration from a repository with Ledger
- [ ] Strict mode
- [ ] Main file
- [ ] Add transactions immediately without confirmation
- [ ] Prompt Templates
** [ ] Text Reports (from configuration)
** [x] Text Reports (from configuration)
* Nice to Have
** [ ] Visual Reports

0 comments on commit ec9086d

Please sign in to comment.