2024-03-25 11:55:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-04-11 14:13:20 +03:00
|
|
|
"context"
|
2024-03-30 16:49:51 +02:00
|
|
|
"flag"
|
2024-03-25 11:55:01 +02:00
|
|
|
"fmt"
|
2024-04-11 14:13:20 +03:00
|
|
|
"gitea.stuzer.link/stuzer05/go-firefly3"
|
2024-04-10 23:24:16 +03:00
|
|
|
"gitea.stuzer.link/stuzer05/go-monobank"
|
2024-04-11 14:13:20 +03:00
|
|
|
"github.com/antihax/optional"
|
2024-03-25 11:55:01 +02:00
|
|
|
"github.com/joho/godotenv"
|
2024-03-30 16:49:51 +02:00
|
|
|
"io"
|
2024-03-25 11:55:01 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-03-30 16:49:51 +02:00
|
|
|
"net/http/httptest"
|
2024-03-25 11:55:01 +02:00
|
|
|
"os"
|
2024-03-30 16:49:51 +02:00
|
|
|
"strings"
|
2024-03-25 11:55:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2024-04-05 17:12:20 +03:00
|
|
|
// 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"}'
|
2024-03-25 11:55:01 +02:00
|
|
|
|
2024-04-05 17:12:20 +03:00
|
|
|
// curl -X POST https://monobank-firefly3.io.stuzer.link/webhook -H 'Content-Type: application/json' -d '{"test":123}'
|
2024-03-25 11:55:01 +02:00
|
|
|
|
|
|
|
func main() {
|
2024-03-27 20:12:24 +02:00
|
|
|
// load .env
|
2024-03-25 11:55:01 +02:00
|
|
|
err := godotenv.Load(".env")
|
|
|
|
if err != nil {
|
2024-04-05 16:51:49 +03:00
|
|
|
log.Fatalf("error loading .env file")
|
2024-03-25 11:55:01 +02:00
|
|
|
}
|
|
|
|
|
2024-03-27 21:22:51 +02:00
|
|
|
// test config read
|
|
|
|
_, err = ReadConfig(os.Getenv("CONFIG_PATH"))
|
|
|
|
if err != nil {
|
2024-04-05 16:50:00 +03:00
|
|
|
log.Fatalf("cannot read config - " + err.Error())
|
2024-03-27 21:22:51 +02:00
|
|
|
}
|
|
|
|
|
2024-03-30 16:49:51 +02:00
|
|
|
// flags
|
2024-04-11 14:13:20 +03:00
|
|
|
flagMonobankDoTransaction := flag.String("monobank-transaction", "", "run monobank transaction JSON manually")
|
|
|
|
flagMonobankListAccounts := flag.Bool("monobank-list-accounts", false, "list monobank accounts")
|
|
|
|
flagFirefly3ListAccounts := flag.Bool("firefly3-list-accounts", false, "list firefly3 accounts")
|
2024-03-26 19:06:31 +02:00
|
|
|
|
2024-03-30 16:49:51 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2024-04-11 14:13:20 +03:00
|
|
|
// 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)
|
|
|
|
|
2024-03-30 16:49:51 +02:00
|
|
|
// manual transaction
|
2024-04-11 14:13:20 +03:00
|
|
|
if *flagMonobankListAccounts {
|
|
|
|
// get monobank accounts
|
|
|
|
req := monobank.ApiPersonalClientInfoGetRequest{}
|
|
|
|
req = req.XToken(os.Getenv("MONOBANK_TOKEN"))
|
|
|
|
res, _, err := monobankClient.DefaultApi.PersonalClientInfoGetExecute(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// list accounts
|
|
|
|
for _, row := range res.Accounts {
|
|
|
|
fmt.Printf("%v\t%v\n", *row.Id, (*row.MaskedPan)[0])
|
|
|
|
}
|
|
|
|
} else if *flagFirefly3ListAccounts {
|
|
|
|
// get firefly3 accounts
|
|
|
|
req := firefly3.AccountsApiListAccountOpts{
|
|
|
|
Limit: optional.NewInt32(9999),
|
|
|
|
}
|
|
|
|
res, _, err := firefly3Client.AccountsApi.ListAccount(context.Background(), &req)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// list accounts
|
|
|
|
for _, row := range res.Data {
|
|
|
|
if row.Attributes.Active && (*row.Attributes.Type_ == firefly3.ASSET_ShortAccountTypeProperty) {
|
|
|
|
fmt.Printf("%v\t%v\n", row.Id, row.Attributes.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if len(*flagMonobankDoTransaction) > 0 {
|
2024-03-30 16:49:51 +02:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
r := &http.Request{
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Header: make(http.Header),
|
2024-04-11 14:13:20 +03:00
|
|
|
Body: io.NopCloser(strings.NewReader(*flagMonobankDoTransaction)),
|
2024-03-30 16:49:51 +02:00
|
|
|
}
|
|
|
|
r.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
handleWebhook(w, r)
|
|
|
|
|
|
|
|
// @todo error logging
|
|
|
|
//response := w.Result()
|
|
|
|
//if response.StatusCode != http.StatusOK {
|
|
|
|
// os.Exit(1)
|
|
|
|
//}
|
|
|
|
} else {
|
2024-04-10 23:29:33 +03:00
|
|
|
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"))
|
2024-04-05 17:12:20 +03:00
|
|
|
|
|
|
|
// register monobank webhook
|
2024-04-10 23:24:16 +03:00
|
|
|
req := monobank.ApiPersonalWebhookPostRequest{}
|
|
|
|
req = req.XToken(os.Getenv("MONOBANK_TOKEN"))
|
|
|
|
req = req.SetWebHook(monobank.SetWebHook{WebHookUrl: &webhookUrl})
|
|
|
|
_, err := monobankClient.DefaultApi.PersonalWebhookPostExecute(req)
|
2024-04-05 17:12:20 +03:00
|
|
|
if err != nil {
|
2024-04-10 16:21:55 +03:00
|
|
|
log.Fatalln("failed to register monobank webhook")
|
2024-04-05 17:12:20 +03:00
|
|
|
}
|
|
|
|
|
2024-03-30 16:49:51 +02:00
|
|
|
// set webhook
|
2024-04-05 17:12:20 +03:00
|
|
|
http.HandleFunc(webhookLocalUrl, handleWebhook)
|
2024-03-30 16:49:51 +02:00
|
|
|
|
|
|
|
// listen server
|
2024-04-05 16:51:49 +03:00
|
|
|
fmt.Println("webhook server listening on " + os.Getenv("LISTEN"))
|
2024-04-05 17:12:20 +03:00
|
|
|
fmt.Println("webhook url " + webhookUrl)
|
2024-03-30 16:49:51 +02:00
|
|
|
err = http.ListenAndServe(os.Getenv("LISTEN"), nil)
|
|
|
|
if err != nil {
|
2024-04-10 16:21:55 +03:00
|
|
|
log.Fatalln(err.Error())
|
2024-03-30 16:49:51 +02:00
|
|
|
}
|
2024-03-26 19:06:31 +02:00
|
|
|
}
|
2024-03-25 11:55:01 +02:00
|
|
|
}
|