Use my monobank api package

This commit is contained in:
Illya Marchenko 2024-04-10 23:24:16 +03:00
parent 0f2a98ed07
commit bb94d868ba
Signed by: stuzer05
GPG Key ID: A6ABAAA9268F9F4F
5 changed files with 33 additions and 50 deletions

9
go.mod

@ -4,14 +4,9 @@ go 1.22.2
require ( require (
gitea.stuzer.link/stuzer05/go-firefly3 v0.0.0-20240410091657-a7ef9d02ecd9 gitea.stuzer.link/stuzer05/go-firefly3 v0.0.0-20240410091657-a7ef9d02ecd9
gitea.stuzer.link/stuzer05/go-monobank v0.0.0-20240410200931-4738cbd4284e
github.com/antihax/optional v1.0.0 github.com/antihax/optional v1.0.0
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
github.com/vtopc/go-monobank v0.21.0
) )
require ( require golang.org/x/oauth2 v0.19.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v2 v2.0.0 // indirect
github.com/vtopc/epoch v1.3.0 // indirect
github.com/vtopc/go-rest v0.3.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
)

32
go.sum

@ -1,34 +1,18 @@
gitea.stuzer.link/stuzer05/go-firefly3 v0.0.0-20240410091657-a7ef9d02ecd9 h1:YudeO7GocpdRDagqvVjbYEPXK3X2JSwYp2ZCnKyo5jQ= gitea.stuzer.link/stuzer05/go-firefly3 v0.0.0-20240410091657-a7ef9d02ecd9 h1:YudeO7GocpdRDagqvVjbYEPXK3X2JSwYp2ZCnKyo5jQ=
gitea.stuzer.link/stuzer05/go-firefly3 v0.0.0-20240410091657-a7ef9d02ecd9/go.mod h1:t1fQbrfn3dmJPLVsoX3Pof9/3xcSBIYpYl/Iz6qBH5E= gitea.stuzer.link/stuzer05/go-firefly3 v0.0.0-20240410091657-a7ef9d02ecd9/go.mod h1:t1fQbrfn3dmJPLVsoX3Pof9/3xcSBIYpYl/Iz6qBH5E=
gitea.stuzer.link/stuzer05/go-monobank v0.0.0-20240410192145-a523824e66d7 h1:he6tteQWO0mZIcmHCcofi8a60GTXeYoHjFIlZOz51rs=
gitea.stuzer.link/stuzer05/go-monobank v0.0.0-20240410192145-a523824e66d7/go.mod h1:y/jOBU1U+NNR5umvDH+scrOWk0byZGZrMb4PHEehyrk=
gitea.stuzer.link/stuzer05/go-monobank v0.0.0-20240410195707-3bb97cbca3ef h1:TZzRZ8aEAfcllzAGq3DCKECrT5KBgsqXWncCbg7xAus=
gitea.stuzer.link/stuzer05/go-monobank v0.0.0-20240410195707-3bb97cbca3ef/go.mod h1:y/jOBU1U+NNR5umvDH+scrOWk0byZGZrMb4PHEehyrk=
gitea.stuzer.link/stuzer05/go-monobank v0.0.0-20240410200638-a2776842062d h1:gNF3QyQ9kj0Sbp2NJ4Div7n1+wv7AFc1sZHjVqItqDY=
gitea.stuzer.link/stuzer05/go-monobank v0.0.0-20240410200638-a2776842062d/go.mod h1:y/jOBU1U+NNR5umvDH+scrOWk0byZGZrMb4PHEehyrk=
gitea.stuzer.link/stuzer05/go-monobank v0.0.0-20240410200931-4738cbd4284e h1:o9vg8T03c3zJNFDs4L6zbYtjcCjv/kuE6SHhGNOqyyE=
gitea.stuzer.link/stuzer05/go-monobank v0.0.0-20240410200931-4738cbd4284e/go.mod h1:y/jOBU1U+NNR5umvDH+scrOWk0byZGZrMb4PHEehyrk=
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg= github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/chaincfg/chainhash v1.0.2 h1:rt5Vlq/jM3ZawwiacWjPa+smINyLRN07EO0cNBV6DGU=
github.com/decred/dcrd/chaincfg/chainhash v1.0.2/go.mod h1:BpbrGgrPTr3YJYRN3Bm+D9NuaFd+zGyNeIKgrhCXK60=
github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0=
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/dcrec/secp256k1/v2 v2.0.0 h1:3GIJYXQDAKpLEFriGFN8SbSffak10UXHGdIcFaMPykY=
github.com/decred/dcrd/dcrec/secp256k1/v2 v2.0.0/go.mod h1:3s92l0paYkZoIHuj4X93Teg/HB7eGM9x/zokGw+u4mY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vtopc/epoch v1.3.0 h1:grB1J6FJVfIMbK6GY0LpOO86R4rQZ4oVwHa08USBPKA=
github.com/vtopc/epoch v1.3.0/go.mod h1:oQJwg81RJ2bCV01D4ImQ+cwpcRDUNc66x/9A/6I2RYU=
github.com/vtopc/go-monobank v0.21.0 h1:pW3mX7XvhJIZjJEGEzVFcPI+cD3yAIir/fFA5ei4Zlo=
github.com/vtopc/go-monobank v0.21.0/go.mod h1:gTC/lAouMpz9WfHnXF3RC0JdEPZwnl7P4vBG+htKT3c=
github.com/vtopc/go-rest v0.3.0 h1:kP+5UNeTVQxwRX3tXe/XByuntwZmKArv5/uMdEf1WKo=
github.com/vtopc/go-rest v0.3.0/go.mod h1:t7XPUz57Z+U66vsVIjEm89Uw/NZf486JVAFhq6+mZps=
golang.org/x/oauth2 v0.19.0 h1:9+E/EZBCbTLNrbN35fHv/a/d/mOBatymz1zbtQrXpIg= golang.org/x/oauth2 v0.19.0 h1:9+E/EZBCbTLNrbN35fHv/a/d/mOBatymz1zbtQrXpIg=
golang.org/x/oauth2 v0.19.0/go.mod h1:vYi7skDa1x015PmRRYZ7+s1cWyPgrPiSYRe4rnsexc8= golang.org/x/oauth2 v0.19.0/go.mod h1:vYi7skDa1x015PmRRYZ7+s1cWyPgrPiSYRe4rnsexc8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

@ -3,7 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
monobank "github.com/vtopc/go-monobank" "gitea.stuzer.link/stuzer05/go-monobank"
"io" "io"
"net/http" "net/http"
) )

12
main.go

@ -1,11 +1,10 @@
package main package main
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"gitea.stuzer.link/stuzer05/go-monobank"
"github.com/joho/godotenv" "github.com/joho/godotenv"
monobank "github.com/vtopc/go-monobank"
"io" "io"
"log" "log"
"net/http" "net/http"
@ -62,8 +61,13 @@ func main() {
webhookUrl := `https://` + os.Getenv("MONOBANK_WEBHOOK_DOMAIN") + webhookLocalUrl webhookUrl := `https://` + os.Getenv("MONOBANK_WEBHOOK_DOMAIN") + webhookLocalUrl
// register monobank webhook // register monobank webhook
monobankClient := monobank.NewPersonalClient(nil).WithAuth(monobank.NewPersonalAuthorizer(os.Getenv("MONOBANK_TOKEN"))) monobankClientConf := monobank.NewConfiguration()
err := monobankClient.SetWebHook(context.Background(), webhookUrl) monobankClient := monobank.NewAPIClient(monobankClientConf)
req := monobank.ApiPersonalWebhookPostRequest{}
req = req.XToken(os.Getenv("MONOBANK_TOKEN"))
req = req.SetWebHook(monobank.SetWebHook{WebHookUrl: &webhookUrl})
_, err := monobankClient.DefaultApi.PersonalWebhookPostExecute(req)
if err != nil { if err != nil {
log.Fatalln("failed to register monobank webhook") log.Fatalln("failed to register monobank webhook")
} }

@ -5,9 +5,9 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
firefly3 "gitea.stuzer.link/stuzer05/go-firefly3" "gitea.stuzer.link/stuzer05/go-firefly3"
"gitea.stuzer.link/stuzer05/go-monobank"
"github.com/antihax/optional" "github.com/antihax/optional"
monobank "github.com/vtopc/go-monobank"
"math" "math"
"net/http" "net/http"
"os" "os"
@ -47,11 +47,11 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
} }
// find accounts // find accounts
account := ConfigGetAccountByMonobankId(config, monobankTransaction.Data.AccountID) account := ConfigGetAccountByMonobankId(config, monobankTransaction.Data.Account)
// cancel if one of account ids is empty // cancel if one of account ids is empty
if len(account.Firefly3Name) == 0 || len(account.MonobankId) == 0 { if len(account.Firefly3Name) == 0 || len(account.MonobankId) == 0 {
LogString("cannot find firefly3 or monobank ids (" + monobankTransaction.Data.AccountID + ")") LogString("cannot find firefly3 or monobank ids (" + monobankTransaction.Data.Account + ")")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
return return
} }
@ -69,7 +69,7 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
for _, row := range config.TransactionTypes { for _, row := range config.TransactionTypes {
// is refund // is refund
if slices.Contains(row.NamesRefund, monobankTransaction.Data.Transaction.Description) { if slices.Contains(row.NamesRefund, *monobankTransaction.Data.StatementItem.Description) {
opts := firefly3.TransactionsApiListTransactionOpts{ opts := firefly3.TransactionsApiListTransactionOpts{
Limit: optional.NewInt32(999), Limit: optional.NewInt32(999),
Type_: optional.NewInterface("withdrawal"), Type_: optional.NewInterface("withdrawal"),
@ -97,16 +97,16 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
} }
// read monobank transaction // read monobank transaction
var monobankTransaction monobank.Transaction var monobankTransaction monobank.StatementItemsInner
err = json.Unmarshal(notesBytes, &monobankTransaction) err = json.Unmarshal(notesBytes, &monobankTransaction)
if err != nil { if err != nil {
continue continue
} }
// find transaction // find transaction
sum := int(math.Abs(math.Round(float64(monobankTransaction.Amount/100)))) - int(math.Abs(math.Round(float64(monobankTransaction.CommissionRate/100)))) 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) sum2, _ := strconv.ParseFloat(tRow.Amount, 64)
if slices.Contains(row.Names, monobankTransaction.Description) && sum == int(sum2) { if slices.Contains(row.Names, *monobankTransaction.Description) && sum == int(sum2) {
// delete transaction // delete transaction
opts := firefly3.TransactionsApiDeleteTransactionOpts{} opts := firefly3.TransactionsApiDeleteTransactionOpts{}
firefly3Client.TransactionsApi.DeleteTransaction(context.Background(), tRows.Id, &opts) firefly3Client.TransactionsApi.DeleteTransaction(context.Background(), tRows.Id, &opts)
@ -118,15 +118,15 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
break break
} else { } else {
// check name & mcc // check name & mcc
if !(slices.Contains(row.Names, monobankTransaction.Data.Transaction.Description) || slices.Contains(row.MccCodes, int(monobankTransaction.Data.Transaction.MCC))) { if !(slices.Contains(row.Names, *monobankTransaction.Data.StatementItem.Description) || slices.Contains(row.MccCodes, int(*monobankTransaction.Data.StatementItem.Mcc))) {
continue continue
} }
// create firefly3 transaction // create firefly3 transaction
firefly3Transaction := firefly3.TransactionSplitStore{ firefly3Transaction := firefly3.TransactionSplitStore{
Date: time.Unix(monobankTransaction.Data.Transaction.Time.Unix(), 0).Add(time.Hour * 2), Date: time.Unix(int64(*monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
Notes: string(monobankTransactionJson), Notes: string(monobankTransactionJson),
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(monobankTransaction.Data.Transaction.Amount/100)))) - int(math.Abs(math.Round(float64(monobankTransaction.Data.Transaction.CommissionRate/100))))), 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, SourceName: account.Firefly3Name,
} }
@ -163,13 +163,13 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
break break
} }
} }
if monobankTransaction.Data.Transaction.CommissionRate > 0 { if *monobankTransaction.Data.StatementItem.CommissionRate > 0 {
firefly3Transactions = append(firefly3Transactions, firefly3.TransactionSplitStore{ firefly3Transactions = append(firefly3Transactions, firefly3.TransactionSplitStore{
Type_: &firefly3TransactionTypeWithdrawal, Type_: &firefly3TransactionTypeWithdrawal,
Date: time.Unix(monobankTransaction.Data.Transaction.Time.Unix(), 0).Add(time.Hour * 2), Date: time.Unix(int64(*monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
Notes: string(monobankTransactionJson), Notes: string(monobankTransactionJson),
Description: "Transfer fee", Description: "Transfer fee",
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(monobankTransaction.Data.Transaction.CommissionRate / 100))))), Amount: strconv.Itoa(int(math.Abs(math.Round(float64(*monobankTransaction.Data.StatementItem.CommissionRate / 100))))),
SourceName: account.Firefly3Name, SourceName: account.Firefly3Name,
}) })
} }