This commit is contained in:
2024-04-12 13:00:28 +03:00
parent ac84e48d8f
commit f1f5e1b712
13 changed files with 303 additions and 291 deletions

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)
}
}
}