-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (120 loc) · 6.53 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"sort"
"time"
)
type TransactionsVariables struct {
CollectiveId int `json:"CollectiveId"`
DateFrom string `json:"dateFrom"`
DateTo string `json:"dateTo"`
}
type TransactionsQuery struct {
OperationName string `json:"operationName"`
Variables TransactionsVariables `json:"variables"`
Query string `json:"query"`
}
//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 ... 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/"
func loadCurrentState() (GraphQlData, error) {
if _, err := os.Stat(currentStateFilename); os.IsNotExist(err) {
return GraphQlData{}, err
}
jsonFile, err := os.Open(currentStateFilename)
if err != nil {
log.Fatalf("File open error: %v", err)
}
defer jsonFile.Close()
data, _ := ioutil.ReadAll(jsonFile)
var state = &GraphQlData{}
if err := json.Unmarshal(data, &state); err != nil {
log.Fatalf("JSON file unmarshalling error: %v", err)
}
return *state, nil
}
func saveCurrentState(body []byte) {
if err := ioutil.WriteFile(currentStateFilename, body, 0644); err != nil {
log.Fatalf("JSON saving error: %v", err)
}
}
func main() {
if len(os.Args) < 2 {
log.Fatalf("Provide CodeX.Bot webhook token as command argument")
}
token := os.Args[1]
gqlQuery := TransactionsQuery{
OperationName: "Transactions",
Variables: TransactionsVariables{
CollectiveId: 37258,
DateFrom: "2019-02-28T21:00:00.000Z",
DateTo: time.Now().Format("2006-01-02T15:04:05.000Z"),
},
Query: query,
}
request, err := json.Marshal(&gqlQuery)
if err != nil {
log.Fatalf("JSON Marshalling error: %v", err)
}
req, err := http.NewRequest("POST", graphQlURL, bytes.NewBuffer(request))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("HTTP POST error: %v", err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
fmt.Printf("Invalid status code: %d", resp.StatusCode)
fmt.Println("response Headers:", resp.Header)
fmt.Println("response Body:", string(body))
}
var response = &GraphQlData{}
if err := json.Unmarshal(body, &response); err != nil {
log.Fatalf("JSON Unmarshalling error: %v", err)
}
currentState, err := loadCurrentState()
// Not a first run
if err == nil {
ids := make(map[int]bool)
for _, transaction := range currentState.Data.AllTransactions {
ids[transaction.Id] = true
}
newTransactions := []Transaction{}
if len(currentState.Data.AllTransactions) < len(response.Data.AllTransactions) {
for _, transaction := range response.Data.AllTransactions {
if _, ok := ids[transaction.Id]; !ok {
newTransactions = append(newTransactions, transaction)
}
}
}
sort.Sort(Transactions(newTransactions))
for _, transaction := range newTransactions {
data := url.Values{}
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",
})
if err != nil {
log.Fatalf("Webhook error: %v", err)
}
}
}
saveCurrentState(body)
}