This commit is contained in:
Illya Marchenko 2024-04-12 13:00:28 +03:00
parent ac84e48d8f
commit f1f5e1b712
Signed by: stuzer05
GPG Key ID: A6ABAAA9268F9F4F
13 changed files with 303 additions and 291 deletions

View File

@ -9,4 +9,4 @@ MONOBANK_TOKEN=
MONOBANK_WEBHOOK_DOMAIN=monobank-firefly3.io.stuzer.link
MONOBANK_WEBHOOK_SECRET=
LOG_FILE=/tmp/monobank-filefly3.log
LOG_FILE=./logs/monobank-filefly3.log

4
.gitignore vendored
View File

@ -2,4 +2,6 @@
/main
/.env
/config.json
/monobank-firefly3-bot
/monobank-firefly3-bot
/logs/*
!/logs/.gitkeep

View File

@ -55,4 +55,8 @@ to get firefly3 account ids use `--firefly3-list-accounts` command
2 Wallet cash (UAH)
3 Mono white
4 PrivatBank virtual
```
```
## API docs
- https://api-docs.firefly-iii.org
- https://api.monobank.ua/docs/index.html

46
app/app.go Normal file
View File

@ -0,0 +1,46 @@
package app
import (
"fmt"
"gitea.stuzer.link/stuzer05/go-firefly3/v2"
"gitea.stuzer.link/stuzer05/go-monobank"
"os"
"stuzer.link/monobank-firefly3-bot/config"
"sync"
)
type Config struct {
init sync.Once
Config config.Config
MonobankClient *monobank.APIClient
Firefly3Client *firefly3.APIClient
}
var app Config
func App() *Config {
return &app
}
func Init() {
app.init.Do(func() {
var err error
// read config
app.Config, err = config.Read(os.Getenv("CONFIG_PATH"))
if err != nil {
fmt.Println("cannot read config: " + err.Error())
os.Exit(1)
}
// init monobank client
monobankClientConf := monobank.NewConfiguration()
app.MonobankClient = monobank.NewAPIClient(monobankClientConf)
// init firefly3 client
clientConf := firefly3.NewConfiguration()
clientConf.BasePath = os.Getenv("FIREFLY3_API_URL")
clientConf.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("FIREFLY3_TOKEN"))
app.Firefly3Client = firefly3.NewAPIClient(clientConf)
})
}

162
app/import_transaction.go Normal file
View File

@ -0,0 +1,162 @@
package app
import (
"bytes"
"context"
"encoding/json"
"errors"
"gitea.stuzer.link/stuzer05/go-firefly3/v2"
"gitea.stuzer.link/stuzer05/go-monobank"
"github.com/antihax/optional"
"math"
"slices"
"strconv"
"time"
)
func ImportTransaction(monobankTransaction monobank.WebHookResponse) error {
firefly3TransactionTypeWithdrawal := firefly3.WITHDRAWAL_TransactionTypeProperty
firefly3TransactionTypeDeposit := firefly3.DEPOSIT_TransactionTypeProperty
firefly3TransactionTypeTransfer := firefly3.TRANSFER_TransactionTypeProperty
// get body json string (for logging)
monobankTransactionJson, err := json.Marshal(monobankTransaction)
if err != nil {
return err
}
// find accounts
account := App().Config.GetAccountByMonobankId(monobankTransaction.Data.Account)
// cancel if one of account ids is empty
if len(account.Firefly3Name) == 0 || len(account.MonobankId) == 0 {
return errors.New("cannot find firefly3 or monobank ids (" + monobankTransaction.Data.Account + ")")
}
// create firefly3 transactions list
var firefly3Transactions []firefly3.TransactionSplitStore
// match transaction with config
for _, row := range App().Config.TransactionTypes {
// is refund
if slices.Contains(row.NamesRefund, *monobankTransaction.Data.StatementItem.Description) {
opts := firefly3.TransactionsApiListTransactionOpts{
Limit: optional.NewInt32(999),
Type_: optional.NewInterface("withdrawal"),
Start: optional.NewString(time.Now().AddDate(0, 0, -7).Format("2006-01-02")), // one week before
}
oldTransactions, _, err := App().Firefly3Client.TransactionsApi.ListTransaction(context.Background(), &opts)
if err != nil {
return err
}
// find matching transaction to delete
isDeleted := false
for _, tRows := range oldTransactions.Data {
if isDeleted {
break
}
for _, tRow := range tRows.Attributes.Transactions {
// validate notes is json
notesBytes := bytes.NewBufferString(tRow.Notes).Bytes()
if !json.Valid(notesBytes) {
continue
}
// read monobank transaction
var monobankTransaction monobank.StatementItemsInner
err = json.Unmarshal(notesBytes, &monobankTransaction)
if err != nil {
continue
}
// find transaction
sum := int(math.Abs(math.Round(float64(*monobankTransaction.Amount/100)))) - int(math.Abs(math.Round(float64(*monobankTransaction.CommissionRate/100))))
sum2, _ := strconv.ParseFloat(tRow.Amount, 64)
if slices.Contains(row.Names, *monobankTransaction.Description) && sum == int(sum2) {
// delete transaction
opts := firefly3.TransactionsApiDeleteTransactionOpts{}
App().Firefly3Client.TransactionsApi.DeleteTransaction(context.Background(), tRows.Id, &opts)
isDeleted = true
}
}
}
break
} else {
// check name & mcc
if !(slices.Contains(row.Names, *monobankTransaction.Data.StatementItem.Description) || slices.Contains(row.MccCodes, int(*monobankTransaction.Data.StatementItem.Mcc))) {
continue
}
// create firefly3 transaction
firefly3Transaction := firefly3.TransactionSplitStore{
Date: time.Unix(int64(*monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
Notes: string(monobankTransactionJson),
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(*monobankTransaction.Data.StatementItem.Amount/100)))) - int(math.Abs(math.Round(float64(*monobankTransaction.Data.StatementItem.CommissionRate/100))))),
SourceName: account.Firefly3Name,
}
// check max sum
sum, _ := strconv.Atoi(firefly3Transaction.Amount)
if row.SumMax > 0 && sum > row.SumMax {
continue
}
// make transaction
switch row.Firefly3.Type {
case "withdrawal":
firefly3Transaction.Type_ = &firefly3TransactionTypeWithdrawal
break
case "deposit":
firefly3Transaction.Type_ = &firefly3TransactionTypeDeposit
break
case "transfer":
firefly3Transaction.Type_ = &firefly3TransactionTypeTransfer
break
default:
firefly3Transaction.Type_ = &firefly3TransactionTypeWithdrawal
}
firefly3Transaction.Description = row.Firefly3.Description
firefly3Transaction.DestinationName = row.Firefly3.Destination
firefly3Transaction.CategoryName = row.Firefly3.Category
// swap source and destination
if row.Firefly3.IsUseDestinationAsSource {
firefly3Transaction.SourceName, firefly3Transaction.DestinationName = firefly3Transaction.DestinationName, firefly3Transaction.SourceName
}
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
break
}
}
if *monobankTransaction.Data.StatementItem.CommissionRate > 0 {
firefly3Transactions = append(firefly3Transactions, firefly3.TransactionSplitStore{
Type_: &firefly3TransactionTypeWithdrawal,
Date: time.Unix(int64(*monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
Notes: string(monobankTransactionJson),
Description: "Transfer fee",
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(*monobankTransaction.Data.StatementItem.CommissionRate / 100))))),
SourceName: account.Firefly3Name,
})
}
// log firefly3 transactions
if len(firefly3Transactions) > 0 {
transactionOpts := firefly3.TransactionsApiStoreTransactionOpts{}
for _, transaction := range firefly3Transactions {
_, _, err = App().Firefly3Client.TransactionsApi.StoreTransaction(context.Background(), firefly3.TransactionStore{
ApplyRules: true,
Transactions: []firefly3.TransactionSplitStore{transaction},
}, &transactionOpts)
if err != nil {
return err
}
}
}
return nil
}

View File

@ -1,4 +1,4 @@
package main
package app
import (
"fmt"

View File

@ -1,10 +1,4 @@
package main
import (
"encoding/json"
"io"
"os"
)
package config
type Config struct {
Accounts []ConfigAccount `json:"accounts"`
@ -31,37 +25,3 @@ type ConfigTransactionTypeFirefly3 struct {
Category string `json:"category,omitempty"`
IsUseDestinationAsSource bool `json:"is_use_destination_as_source,omitempty"`
}
func ReadConfig(path string) (Config, error) {
var config Config
// open file
file, err := os.Open(path)
if err != nil {
return config, err
}
defer file.Close()
// read file
bytes, err := io.ReadAll(file)
if err != nil {
return config, err
}
// read file ot config struct
if err := json.Unmarshal(bytes, &config); err != nil {
return config, err
}
return config, nil
}
func ConfigGetAccountByMonobankId(config Config, q string) ConfigAccount {
for _, row := range config.Accounts {
if row.MonobankId == q {
return row
}
}
return ConfigAccount{}
}

11
config/helpers.go Normal file
View File

@ -0,0 +1,11 @@
package config
func (c *Config) GetAccountByMonobankId(q string) ConfigAccount {
for _, row := range c.Accounts {
if row.MonobankId == q {
return row
}
}
return ConfigAccount{}
}

31
config/reader.go Normal file
View File

@ -0,0 +1,31 @@
package config
import (
"encoding/json"
"io"
"os"
)
func Read(path string) (Config, error) {
var config Config
// open file
file, err := os.Open(path)
if err != nil {
return config, err
}
defer file.Close()
// read file
bytes, err := io.ReadAll(file)
if err != nil {
return config, err
}
// read file ot config struct
if err := json.Unmarshal(bytes, &config); err != nil {
return config, err
}
return config, nil
}

33
http.go
View File

@ -1,33 +0,0 @@
package main
import (
"encoding/json"
"errors"
"gitea.stuzer.link/stuzer05/go-monobank"
"io"
"net/http"
)
func readRequestBody(r *http.Request) (monobank.WebHookResponse, error) {
// read body bytes
body, err := io.ReadAll(r.Body)
if err != nil {
return monobank.WebHookResponse{}, err
}
LogString(string(body))
// check empty body
if len(string(body)) == 0 {
return monobank.WebHookResponse{}, errors.New("empty body")
}
// parse body
var transaction monobank.WebHookResponse
err = json.Unmarshal(body, &transaction)
if err != nil {
return monobank.WebHookResponse{}, err
}
return transaction, nil
}

0
logs/.gitkeep Normal file
View File

65
main.go
View File

@ -2,27 +2,19 @@ package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"gitea.stuzer.link/stuzer05/go-firefly3/v2"
"gitea.stuzer.link/stuzer05/go-monobank"
"github.com/antihax/optional"
"github.com/joho/godotenv"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"stuzer.link/monobank-firefly3-bot/app"
)
// https://api.monobank.ua/docs/index.html#tag/Kliyentski-personalni-dani/paths/~1personal~1statement~1{account}~1{from}~1{to}/get
// https://api-docs.firefly-iii.org/#/accounts/listAccount
// curl -X POST https://api.monobank.ua/personal/webhook -H 'Content-Type: application/json' -H 'X-Token: ' -d '{"webHookUrl":"https://monobank-firefly3.stuzer.link/webhook"}'
// curl -X POST https://monobank-firefly3.io.stuzer.link/webhook -H 'Content-Type: application/json' -d '{"test":123}'
func main() {
// load .env
err := godotenv.Load(".env")
@ -30,11 +22,8 @@ func main() {
log.Fatalf("error loading .env file")
}
// test config read
_, err = ReadConfig(os.Getenv("CONFIG_PATH"))
if err != nil {
log.Fatalf("cannot read config - " + err.Error())
}
// init app
app.Init()
// flags
flagMonobankDoTransaction := flag.String("monobank-transaction", "", "run monobank transaction JSON manually")
@ -43,22 +32,12 @@ func main() {
flag.Parse()
// init monobank client
monobankClientConf := monobank.NewConfiguration()
monobankClient := monobank.NewAPIClient(monobankClientConf)
// init firefly3 client
clientConf := firefly3.NewConfiguration()
clientConf.BasePath = os.Getenv("FIREFLY3_API_URL")
clientConf.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("FIREFLY3_TOKEN"))
firefly3Client := firefly3.NewAPIClient(clientConf)
// manual transaction
if *flagMonobankListAccounts {
// get monobank accounts
req := monobank.ApiPersonalClientInfoGetRequest{}
req = req.XToken(os.Getenv("MONOBANK_TOKEN"))
res, _, err := monobankClient.DefaultApi.PersonalClientInfoGetExecute(req)
res, _, err := app.App().MonobankClient.DefaultApi.PersonalClientInfoGetExecute(req)
if err != nil {
log.Fatalln(err.Error())
}
@ -72,9 +51,10 @@ func main() {
req := firefly3.AccountsApiListAccountOpts{
Limit: optional.NewInt32(9999),
}
res, _, err := firefly3Client.AccountsApi.ListAccount(context.Background(), &req)
res, _, err := app.App().Firefly3Client.AccountsApi.ListAccount(context.Background(), &req)
if err != nil {
log.Fatalln(err.Error())
fmt.Println(err.Error())
os.Exit(1)
}
// list accounts
@ -84,22 +64,18 @@ func main() {
}
}
} else if len(*flagMonobankDoTransaction) > 0 {
w := httptest.NewRecorder()
r := &http.Request{
Method: http.MethodPost,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader(*flagMonobankDoTransaction)),
var monobankTransaction monobank.WebHookResponse
err = json.Unmarshal([]byte(*flagMonobankDoTransaction), &monobankTransaction)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
r.Header.Set("Content-Type", "application/json")
handleWebhook(w, r)
// @todo error logging
//response := w.Result()
//if response.StatusCode != http.StatusOK {
// os.Exit(1)
//}
err := app.ImportTransaction(monobankTransaction)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
} else {
webhookLocalUrl := fmt.Sprintf("/webhook/%s", os.Getenv("MONOBANK_WEBHOOK_SECRET"))
webhookUrl := fmt.Sprintf("https://%s/webhook/%s", os.Getenv("MONOBANK_WEBHOOK_DOMAIN"), os.Getenv("MONOBANK_WEBHOOK_SECRET"))
@ -108,7 +84,7 @@ func main() {
req := monobank.ApiPersonalWebhookPostRequest{}
req = req.XToken(os.Getenv("MONOBANK_TOKEN"))
req = req.SetWebHook(monobank.SetWebHook{WebHookUrl: &webhookUrl})
_, err := monobankClient.DefaultApi.PersonalWebhookPostExecute(req)
_, err := app.App().MonobankClient.DefaultApi.PersonalWebhookPostExecute(req)
if err != nil {
log.Fatalln("failed to register monobank webhook")
}
@ -121,7 +97,8 @@ func main() {
fmt.Println("webhook url " + webhookUrl)
err = http.ListenAndServe(os.Getenv("LISTEN"), nil)
if err != nil {
log.Fatalln(err.Error())
fmt.Println(err.Error())
os.Exit(1)
}
}
}

View File

@ -1,195 +1,47 @@
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"gitea.stuzer.link/stuzer05/go-firefly3/v2"
"gitea.stuzer.link/stuzer05/go-monobank"
"github.com/antihax/optional"
"math"
"io"
"net/http"
"os"
"slices"
"strconv"
"time"
"stuzer.link/monobank-firefly3-bot/app"
)
func handleWebhook(w http.ResponseWriter, r *http.Request) {
firefly3TransactionTypeWithdrawal := firefly3.WITHDRAWAL_TransactionTypeProperty
firefly3TransactionTypeDeposit := firefly3.DEPOSIT_TransactionTypeProperty
firefly3TransactionTypeTransfer := firefly3.TRANSFER_TransactionTypeProperty
// read request
monobankTransaction, err := readRequestBody(r)
// read request body bytes
body, err := io.ReadAll(r.Body)
if err != nil {
LogString(err.Error())
app.LogString(err.Error())
w.WriteHeader(http.StatusOK)
return
}
// get body json string (for logging)
monobankTransactionJson, err := json.Marshal(monobankTransaction)
// log body string
app.LogString(string(body))
// check request empty body
if len(string(body)) == 0 {
app.LogString("empty body")
w.WriteHeader(http.StatusOK)
return
}
// parse request body
var monobankTransaction monobank.WebHookResponse
err = json.Unmarshal(body, &monobankTransaction)
if err != nil {
LogString(err.Error())
app.LogString(err.Error())
w.WriteHeader(http.StatusOK)
return
}
// read config
var config Config
config, err = ReadConfig(os.Getenv("CONFIG_PATH"))
err = app.ImportTransaction(monobankTransaction)
if err != nil {
LogString(err.Error())
app.LogString(err.Error())
w.WriteHeader(http.StatusOK)
return
}
// find accounts
account := ConfigGetAccountByMonobankId(config, monobankTransaction.Data.Account)
// cancel if one of account ids is empty
if len(account.Firefly3Name) == 0 || len(account.MonobankId) == 0 {
LogString("cannot find firefly3 or monobank ids (" + monobankTransaction.Data.Account + ")")
w.WriteHeader(http.StatusOK)
return
}
// init firefly3 client
clientConf := firefly3.NewConfiguration()
clientConf.BasePath = os.Getenv("FIREFLY3_API_URL")
clientConf.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("FIREFLY3_TOKEN"))
firefly3Client := firefly3.NewAPIClient(clientConf)
// create firefly3 transactions list
var firefly3Transactions []firefly3.TransactionSplitStore
// match transaction with config
for _, row := range config.TransactionTypes {
// is refund
if slices.Contains(row.NamesRefund, *monobankTransaction.Data.StatementItem.Description) {
opts := firefly3.TransactionsApiListTransactionOpts{
Limit: optional.NewInt32(999),
Type_: optional.NewInterface("withdrawal"),
Start: optional.NewString(time.Now().AddDate(0, 0, -7).Format("2006-01-02")), // one week before
}
oldTransactions, _, err := firefly3Client.TransactionsApi.ListTransaction(context.Background(), &opts)
if err != nil {
LogString(err.Error())
w.WriteHeader(http.StatusOK)
return
}
// find matching transaction to delete
isDeleted := false
for _, tRows := range oldTransactions.Data {
if isDeleted {
break
}
for _, tRow := range tRows.Attributes.Transactions {
// validate notes is json
notesBytes := bytes.NewBufferString(tRow.Notes).Bytes()
if !json.Valid(notesBytes) {
continue
}
// read monobank transaction
var monobankTransaction monobank.StatementItemsInner
err = json.Unmarshal(notesBytes, &monobankTransaction)
if err != nil {
continue
}
// find transaction
sum := int(math.Abs(math.Round(float64(*monobankTransaction.Amount/100)))) - int(math.Abs(math.Round(float64(*monobankTransaction.CommissionRate/100))))
sum2, _ := strconv.ParseFloat(tRow.Amount, 64)
if slices.Contains(row.Names, *monobankTransaction.Description) && sum == int(sum2) {
// delete transaction
opts := firefly3.TransactionsApiDeleteTransactionOpts{}
firefly3Client.TransactionsApi.DeleteTransaction(context.Background(), tRows.Id, &opts)
isDeleted = true
}
}
}
break
} else {
// check name & mcc
if !(slices.Contains(row.Names, *monobankTransaction.Data.StatementItem.Description) || slices.Contains(row.MccCodes, int(*monobankTransaction.Data.StatementItem.Mcc))) {
continue
}
// create firefly3 transaction
firefly3Transaction := firefly3.TransactionSplitStore{
Date: time.Unix(int64(*monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
Notes: string(monobankTransactionJson),
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(*monobankTransaction.Data.StatementItem.Amount/100)))) - int(math.Abs(math.Round(float64(*monobankTransaction.Data.StatementItem.CommissionRate/100))))),
SourceName: account.Firefly3Name,
}
// check max sum
sum, _ := strconv.Atoi(firefly3Transaction.Amount)
if row.SumMax > 0 && sum > row.SumMax {
continue
}
// make transaction
switch row.Firefly3.Type {
case "withdrawal":
firefly3Transaction.Type_ = &firefly3TransactionTypeWithdrawal
break
case "deposit":
firefly3Transaction.Type_ = &firefly3TransactionTypeDeposit
break
case "transfer":
firefly3Transaction.Type_ = &firefly3TransactionTypeTransfer
break
default:
firefly3Transaction.Type_ = &firefly3TransactionTypeWithdrawal
}
firefly3Transaction.Description = row.Firefly3.Description
firefly3Transaction.DestinationName = row.Firefly3.Destination
firefly3Transaction.CategoryName = row.Firefly3.Category
// swap source and destination
if row.Firefly3.IsUseDestinationAsSource {
firefly3Transaction.SourceName, firefly3Transaction.DestinationName = firefly3Transaction.DestinationName, firefly3Transaction.SourceName
}
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
break
}
}
if *monobankTransaction.Data.StatementItem.CommissionRate > 0 {
firefly3Transactions = append(firefly3Transactions, firefly3.TransactionSplitStore{
Type_: &firefly3TransactionTypeWithdrawal,
Date: time.Unix(int64(*monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
Notes: string(monobankTransactionJson),
Description: "Transfer fee",
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(*monobankTransaction.Data.StatementItem.CommissionRate / 100))))),
SourceName: account.Firefly3Name,
})
}
// log firefly3 transactions
if len(firefly3Transactions) > 0 {
transactionOpts := firefly3.TransactionsApiStoreTransactionOpts{}
for _, transaction := range firefly3Transactions {
_, _, err = firefly3Client.TransactionsApi.StoreTransaction(context.Background(), firefly3.TransactionStore{
ApplyRules: true,
Transactions: []firefly3.TransactionSplitStore{transaction},
}, &transactionOpts)
if err != nil {
fmt.Printf(err.Error())
w.WriteHeader(http.StatusOK)
return
}
}
}
w.WriteHeader(http.StatusOK)
}