2024-03-25 11:55:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-04-11 14:13:20 +03:00
|
|
|
"context"
|
2024-04-12 13:00:28 +03:00
|
|
|
"encoding/json"
|
2024-03-30 16:49:51 +02:00
|
|
|
"flag"
|
2024-03-25 11:55:01 +02:00
|
|
|
"fmt"
|
2024-04-12 11:46:21 +03:00
|
|
|
"gitea.stuzer.link/stuzer05/go-firefly3/v2"
|
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"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2024-04-12 13:00:28 +03:00
|
|
|
"stuzer.link/monobank-firefly3-bot/app"
|
2024-03-25 11:55:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-10-08 16:04:54 +03:00
|
|
|
// load .env
|
2024-10-08 16:58:13 +03:00
|
|
|
godotenv.Load(".env")
|
2024-03-25 11:55:01 +02:00
|
|
|
|
2024-04-12 13:00:28 +03:00
|
|
|
// init app
|
|
|
|
app.Init()
|
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()
|
|
|
|
|
|
|
|
// manual transaction
|
2024-04-11 14:13:20 +03:00
|
|
|
if *flagMonobankListAccounts {
|
|
|
|
// get monobank accounts
|
2024-05-12 12:35:18 +03:00
|
|
|
res, _, err := app.App().MonobankClient.Api.PersonalClientInfoGet(context.Background(), os.Getenv("MONOBANK_TOKEN"))
|
2024-04-11 14:13:20 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// list accounts
|
|
|
|
for _, row := range res.Accounts {
|
2024-05-12 12:35:18 +03:00
|
|
|
fmt.Printf("%v\t%v\n", row.Id, (*row.MaskedPan)[0])
|
2024-04-11 14:13:20 +03:00
|
|
|
}
|
|
|
|
} else if *flagFirefly3ListAccounts {
|
|
|
|
// get firefly3 accounts
|
|
|
|
req := firefly3.AccountsApiListAccountOpts{
|
|
|
|
Limit: optional.NewInt32(9999),
|
|
|
|
}
|
2024-04-12 13:00:28 +03:00
|
|
|
res, _, err := app.App().Firefly3Client.AccountsApi.ListAccount(context.Background(), &req)
|
2024-04-11 14:13:20 +03:00
|
|
|
if err != nil {
|
2024-04-12 13:00:28 +03:00
|
|
|
fmt.Println(err.Error())
|
|
|
|
os.Exit(1)
|
2024-04-11 14:13:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-12 13:00:28 +03:00
|
|
|
var monobankTransaction monobank.WebHookResponse
|
|
|
|
err = json.Unmarshal([]byte(*flagMonobankDoTransaction), &monobankTransaction)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
os.Exit(1)
|
2024-03-30 16:49:51 +02:00
|
|
|
}
|
|
|
|
|
2024-04-12 13:00:28 +03:00
|
|
|
err := app.ImportTransaction(monobankTransaction)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-03-30 16:49:51 +02:00
|
|
|
} 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-05-12 12:35:18 +03:00
|
|
|
_, err := app.App().MonobankClient.Api.PersonalWebhookPost(context.Background(), monobank.SetWebHook{WebHookUrl: webhookUrl}, os.Getenv("MONOBANK_TOKEN"))
|
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
|
|
|
|
2024-04-22 17:14:09 +03:00
|
|
|
// health
|
|
|
|
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(`{"status":"pass"}`))
|
|
|
|
})
|
|
|
|
|
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-12 13:00:28 +03:00
|
|
|
fmt.Println(err.Error())
|
|
|
|
os.Exit(1)
|
2024-03-30 16:49:51 +02:00
|
|
|
}
|
2024-03-26 19:06:31 +02:00
|
|
|
}
|
2024-03-25 11:55:01 +02:00
|
|
|
}
|