Skip to content

Commit ec9086d

Browse files
committed
various enchantments and fixes
1 parent ca65555 commit ec9086d

File tree

8 files changed

+95
-41
lines changed

8 files changed

+95
-41
lines changed

Makefile

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
21
start:
3-
. ./.env.dev
4-
go run app/main.go
2+
. ./.env.dev && go run app/main.go
53

64
test:
7-
. ./.env.test
8-
go test -v ./...
5+
. ./.env.test && go test -v ./...

app/bot/bot.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (bot *Bot) proposeTransaction(ctx *ext.Context) (string, *gotgbot.SendMessa
214214
}
215215

216216

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

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

242-
func isReportCallback(cq *gotgbot.CallbackQuery) bool {
242+
func isReportCallback(_ *gotgbot.CallbackQuery) bool {
243243
return true
244244
}
245245

app/ledger/ledger.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ type Report struct {
3838
}
3939

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

4848
func NewLedger(rs repo.Service, gen TransactionGenerator) *Ledger {
@@ -205,9 +205,6 @@ func (l *Ledger) addTransaction(transaction string) error {
205205
if err != nil {
206206
return fmt.Errorf("unable to write main ledger file: %v", err)
207207
}
208-
if err != nil {
209-
return err
210-
}
211208
return nil
212209
}
213210

@@ -342,6 +339,7 @@ func NewOpenAITransactionGenerator(token string) *OpenAITransactionGenerator {
342339
}
343340
}
344341

342+
//nolint:gocritic
345343
func (b OpenAITransactionGenerator) GenerateTransaction(promptCtx PromptCtx) (Transaction, error) {
346344
var buf bytes.Buffer
347345
prTmp := template.Must(template.New("letter").Parse(defaultPromtpTemplate))

app/ledger/ledger_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func TestLedger_ProposeTransaction(t *testing.T) {
163163
var mockedTransactionGenerator *TransactionGeneratorMock
164164

165165
mockedTransactionGenerator = &TransactionGeneratorMock{
166-
GenerateTransactionFunc: func(p PromptCtx) (mocktr Transaction,err error) {
166+
GenerateTransactionFunc: func(_ PromptCtx) (mocktr Transaction,err error) {
167167
mockCall++
168168
dt, _ := time.Parse(time.RFC3339, "2014-11-12T11:45:26.371Z")
169169
// On the first attempt, return transaction that is not valid
@@ -174,12 +174,12 @@ func TestLedger_ProposeTransaction(t *testing.T) {
174174
Description: "My tr",
175175
Comment: "invalid transaction",
176176
Postings: []Posting{
177-
Posting{
177+
{
178178
Account: "cash",
179179
Amount: -3000.43,
180180
Currency: "EUR",
181181
},
182-
Posting{
182+
{
183183
Account: "taxi",
184184
Amount: 3000.43,
185185
Currency: "EUR",
@@ -192,21 +192,20 @@ func TestLedger_ProposeTransaction(t *testing.T) {
192192
Comment: "valid transaction\n22 multiple lines",
193193
Description: "Tacos",
194194
Postings: []Posting{
195-
Posting{
195+
{
196196
Account: "Assets:Cash",
197197
Amount: -3000.43,
198198
Currency: "EUR",
199199
},
200-
Posting{
200+
{
201201
Account: "Food",
202202
Amount: 3000.43,
203203
Currency: "EUR",
204204
},
205205
},
206206
}
207207
}
208-
return
209-
208+
return mocktr, nil
210209
},
211210
}
212211

app/ledger/templates/default_prompt.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
Your goal is to propose a transaction in the ledger cli format.
2-
Your responses MUST be in JSON and adhere to the Transaction struct ONLY with no additional narrative or markup, backquotes or anything.
1+
Your goal is to propose a transaction in the Ledger CLI format.
2+
Your responses MUST be in JSON and adhere to the Transaction struct ONLY, with no additional narrative, markup, backquotes, or anything else.
33

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

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

26-
Assume number in user input to be a price
27+
Assume numbers in user input to be a price, not amount.
2728
Use {{ index .Commodities 0}} as the default currency if nothing else is specified in user request.
2829
Another possible currency are:
2930
{{range .Commodities}}
3031
"{{.}}"
3132
{{end}}
3233

33-
Use {{ index .Accounts 0}} as default assets account if nothing else is specified in user request

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ require (
99
github.com/go-git/go-billy/v5 v5.5.0
1010
github.com/go-git/go-git/v5 v5.11.0
1111
github.com/joho/godotenv v1.5.1
12+
github.com/dustin/go-humanize v1.0.1
13+
github.com/sashabaranov/go-openai v1.26.0
14+
gopkg.in/yaml.v3 v3.0.1
1215
)
1316

1417
require (
1518
github.com/davecgh/go-spew v1.1.1 // indirect
16-
github.com/dustin/go-humanize v1.0.1 // indirect
1719
github.com/pmezard/go-difflib v1.0.0 // indirect
18-
github.com/sashabaranov/go-openai v1.26.0 // indirect
19-
gopkg.in/yaml.v3 v3.0.1 // indirect
2020
)
2121

2222
require (

readme.md

+65-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
1-
## Teledger
2-
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.
1+
# Teledger
2+
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.
3+
4+
## How It Works
5+
6+
Teledger is a Telegram bot service that manages Plain-Text Accounting files stored in a Git repository.
7+
It provides a convenient way to add transactions on the go using natural language.
8+
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.
9+
10+
### Process Overview
11+
12+
- **Initiate Transaction**: You send a message to Teledger describing the transaction.
13+
- **Data Extraction**: Teledger clones your Git repository and extracts necessary information such as available accounts and commodities from the Ledger files.
14+
- **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.
15+
- **Commit Changes**: Once the transaction passes validation, it is committed and pushed back into the repository, updating your financial records automatically.
16+
17+
## Ledger Template Repository
18+
19+
A [template repository](https://github.com/mput/teledger-test) is available to help set up a new Ledger project.
20+
21+
## Configuration
22+
23+
### Service Configuration
24+
25+
Configure the service by setting the following environment variables or directly passing them as command-line arguments:
26+
27+
- **Telegram**:
28+
- `--telegram.token=`, `$TELEGRAM_TOKEN` - Telegram bot token.
29+
30+
- **GitHub**:
31+
- `--github.url=`, `$GITHUB_URL` - GitHub repository URL.
32+
- `--github.token=`, `$GITHUB_TOKEN` - Fine-grained personal access tokens for the repository with RW Contents scope.
33+
34+
- **OpenAI**:
35+
- `--openai.token=`, `$OPENAI_TOKEN` - OpenAI API token.
36+
37+
### Ledger File Configuration
38+
39+
The `teledger.yaml` configuration file may be placed in the root of your Ledger project repository.
40+
It includes settings specific to your ledger environment. Here is the structure of the expected YAML file:
41+
42+
- **mainFile**: Specifies the main ledger file name, default is `main.ledger`.
43+
- **strict**: Boolean to allow or disallow non-existing accounts and commodities.
44+
- **promptTemplate**: Template for generating prompts, optional.
45+
- **reports**: Array of report configurations where each report includes:
46+
- **title**: Description of the report.
47+
- **command**: Ledger-cli command array to generate the report.
48+
49+
Example configuration in [`teledger.yaml`](https://github.com/mput/teledger-test/blob/main/teledger.yaml):
50+
```yaml
51+
strict: true
52+
reports:
53+
- title: Expenses This Month
54+
command: [bal, ^Expenses, --cleared, --period, "this month", -X, EUR]
55+
- title: Expenses Last Month
56+
command: [bal, ^Expenses, --cleared, --period, "last month", -X, EUR]
57+
- title: 💶 Assets
58+
command: [bal, ^Assets]
59+
```
60+
61+
## Demo
62+
TODO
63+
64+
## Deploment
65+
A Docker image for Teledger is available at [Docker Hub](https://hub.docker.com/repository/docker/mput/teledger/general).
366
467
## Development
568
Create `.env.dev` by copying `.env.example` and fill in the values.
@@ -17,9 +80,3 @@ Start the service
1780
go run app/main.go
1881
```
1982

20-
21-
## TODO
22-
- [ ] Propose transactions with openAI
23-
- [ ] Commit transaction with openAI (By pressing confirm btn)
24-
- [ ] Add transaction directly when a transaction typed instead of openAI
25-
- [ ] Add templates for reports with yaml in repo

todo.org

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
* Main
44
** [ ] Add Transaction To a ledger repository
55
- [X] Basic (transaction LLM completion and valid transaction addition)
6-
- [ ] Add to log with chat button
7-
- [ ] Delete with button
6+
- [ ] Add transaction to log with chat callback button
7+
- [ ] Add button to delete transaction
88
** [ ] Configuration from a repository with Ledger
9+
- [ ] Strict mode
10+
- [ ] Main file
11+
- [ ] Add transactions immediately without confirmation
912
- [ ] Prompt Templates
10-
** [ ] Text Reports (from configuration)
13+
** [x] Text Reports (from configuration)
1114
* Nice to Have
1215
** [ ] Visual Reports

0 commit comments

Comments
 (0)