From fccf45161b0390830d504e3ff844789019e152e8 Mon Sep 17 00:00:00 2001 From: N0str Date: Sun, 26 May 2019 21:46:00 +0300 Subject: [PATCH] Add interval status --- .gitignore | 4 ++++ main.go | 14 ++++++++++++-- transactions.go | 5 +++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8841466 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea +current.json +opencollective-webhook +.opencollective-current.json \ No newline at end of file diff --git a/main.go b/main.go index abb2a21..36caa81 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,8 @@ type TransactionsQuery struct { } //const maximumQuery = "query Transactions($CollectiveId: Int!, $type: String, $limit: Int, $offset: Int, $dateFrom: String, $dateTo: String) {\n allTransactions(CollectiveId: $CollectiveId, type: $type, limit: $limit, offset: $offset, dateFrom: $dateFrom, dateTo: $dateTo) {\n id\n uuid\n description\n createdAt\n type\n amount\n currency\n hostCurrency\n hostCurrencyFxRate\n netAmountInCollectiveCurrency\n hostFeeInHostCurrency\n platformFeeInHostCurrency\n paymentProcessorFeeInHostCurrency\n paymentMethod {\n service\n type\n name\n data\n __typename\n }\n collective {\n id\n slug\n type\n name\n __typename\n }\n fromCollective {\n id\n name\n slug\n path\n image\n __typename\n }\n usingVirtualCardFromCollective {\n id\n slug\n name\n __typename\n }\n host {\n id\n slug\n name\n currency\n hostFeePercent\n __typename\n }\n ... on Expense {\n category\n attachment\n __typename\n }\n ... on Order {\n createdAt\n subscription {\n interval\n __typename\n }\n __typename\n }\n refundTransaction {\n id\n uuid\n description\n createdAt\n type\n amount\n currency\n hostCurrency\n hostCurrencyFxRate\n netAmountInCollectiveCurrency\n hostFeeInHostCurrency\n platformFeeInHostCurrency\n paymentProcessorFeeInHostCurrency\n paymentMethod {\n service\n type\n name\n data\n __typename\n }\n collective {\n id\n slug\n type\n name\n __typename\n }\n fromCollective {\n id\n name\n slug\n path\n image\n __typename\n }\n usingVirtualCardFromCollective {\n id\n slug\n name\n __typename\n }\n host {\n id\n slug\n name\n currency\n hostFeePercent\n __typename\n }\n ... on Expense {\n category\n attachment\n __typename\n }\n ... on Order {\n createdAt\n subscription {\n interval\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n}\n" -const query = "query Transactions($CollectiveId: Int!, $type: String, $limit: Int, $offset: Int, $dateFrom: String, $dateTo: String) {\n allTransactions(CollectiveId: $CollectiveId, type: $type, limit: $limit, offset: $offset, dateFrom: $dateFrom, dateTo: $dateTo) {\n id\n type\n amount\n currency\n netAmountInCollectiveCurrency\n collective {\n name\n }\n fromCollective {\n name\n path\n }\n }\n}\n" +const query = "query Transactions($CollectiveId: Int!, $type: String, $limit: Int, $offset: Int, $dateFrom: String, $dateTo: String) {\n allTransactions(CollectiveId: $CollectiveId, type: $type, limit: $limit, offset: $offset, dateFrom: $dateFrom, dateTo: $dateTo) {\n id\n type\n amount\n currency\n netAmountInCollectiveCurrency\n collective {\n name\n }\n fromCollective {\n name\n path\n }\n ... on Order {\n createdAt\n subscription {\n interval\n}\n}\n }\n}" + const graphQlURL = "https://opencollective.com/api/graphql" const currentStateFilename = ".opencollective-current.json" const webhookURL = "https://notify.bot.codex.so/u/" @@ -123,7 +124,16 @@ func main() { sort.Sort(Transactions(newTransactions)) for _, transaction := range newTransactions { data := url.Values{} - data.Set("message", fmt.Sprintf("💰 %d$ donation to %s from %s", transaction.Amount / 100, transaction.Collective.Name, transaction.FromCollective.Name)) + + var message string + + if transaction.Subscription.Interval != "" { + message = fmt.Sprintf("💰 %d$ %sly donation to %s from %s", transaction.Amount / 100, transaction.Subscription.Interval, transaction.Collective.Name, transaction.FromCollective.Name) + } else { + message = fmt.Sprintf("💰 %d$ donation to %s from %s", transaction.Amount / 100, transaction.Collective.Name, transaction.FromCollective.Name) + } + + data.Set("message", message) _, err := MakeHTTPRequest("POST", fmt.Sprintf("%s%s", webhookURL, token), []byte(data.Encode()), map[string]string{ "Content-Type": "application/x-www-form-urlencoded", }) diff --git a/transactions.go b/transactions.go index 86ebdbc..fcadc71 100644 --- a/transactions.go +++ b/transactions.go @@ -6,6 +6,10 @@ type Collective struct { Slug string `json:"slug"` } +type Subscription struct { + Interval string `json:"interval"` +} + type Transaction struct { Id int `json:"id"` Type string `json:"type"` @@ -14,6 +18,7 @@ type Transaction struct { NetAmountInCollectiveCurrency int `json:"netAmountInCollectiveCurrency"` Collective Collective `json:"collective"` FromCollective Collective `json:"fromCollective"` + Subscription Subscription `json:"subscription"` } type Transactions []Transaction