-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handling.go
164 lines (149 loc) · 4.4 KB
/
Handling.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"github.com/dghubble/oauth1"
)
//Struct to parse webhook load
type WebhookLoad struct {
UserId string `json:"for_user_id"`
TweetCreateEvent []Tweet `json:"tweet_create_events"`
}
//Struct to parse tweet
type Tweet struct {
Id int64
IdStr string `json:"id_str"`
User User
Text string
}
//Struct to parse user
type User struct {
Id int64
IdStr string `json:"id_str"`
Name string
Handle string `json:"screen_name"`
}
func CreateClient() *http.Client {
//Create oauth client with consumer keys and access token
config := oauth1.NewConfig(os.Getenv("CONSUMER_KEY"), os.Getenv("CONSUMER_SECRET"))
token := oauth1.NewToken(os.Getenv("ACCESS_TOKEN_KEY"), os.Getenv("ACCESS_TOKEN_SECRET"))
return config.Client(oauth1.NoContext, token)
}
func registerWebhook(writer http.ResponseWriter, _ *http.Request) {
fmt.Println("Registering webhook...")
httpClient := CreateClient()
//Set parameters
path := "https://api.twitter.com/1.1/account_activity/all/" + os.Getenv("WEBHOOK_ENV") + "/webhooks.json"
values := url.Values{}
values.Set("url", os.Getenv("APP_URL")+"/webhook/twitter")
//Make Oauth Post with parameters
resp, _ := httpClient.PostForm(path, values)
defer resp.Body.Close()
//Parse response and check response
body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(resp)
//mt.Println(body)
var data map[string]interface{}
if err := json.Unmarshal([]byte(body), &data); err != nil {
log.Println(err)
}
if resp.StatusCode == 200 {
fmt.Fprintf(writer, "Webhook id of "+data["id"].(string)+" has been registered")
log.Println("Webhook id of " + data["id"].(string) + " has been registered")
} else if resp.StatusCode != 200 {
log.Println("Could not register webhook response below:")
log.Println(string(body))
fmt.Fprintf(writer, string(body)) // convert body from json to string
}
subscribeWebhook()
}
func subscribeWebhook() {
fmt.Println("Subscribing webapp......")
client := CreateClient()
path := "https://api.twitter.com/1.1/account_activity/all/" + os.Getenv("WEBHOOK_ENV") + "/subscriptions.json"
resp, err := client.PostForm(path, nil)
if err != nil {
log.Println(err)
}
//body, _ := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
//If response code is 204 it was succesful
if resp.StatusCode == 204 {
log.Println("Subscribed succesfully")
} else if resp.StatusCode != 204 {
log.Println("Could not subscribe to webhook response below:") // convert body from json to string
log.Println(resp.StatusCode)
}
}
func CrcCheck(writer http.ResponseWriter, req *http.Request) {
// setresponse header to json type
writer.Header().Set("Content-Type", "application/json")
// get crc token in parameter
token := req.URL.Query()["crc_token"]
if len(token) < 1 {
fmt.Fprintf(writer, "No crc_token given")
log.Println("No crc_token given")
return
}
h := hmac.New(sha256.New, []byte(os.Getenv("CONSUMER_SECRET")))
h.Write([]byte(token[0]))
encoded := base64.StdEncoding.EncodeToString(h.Sum(nil))
//Geberate resoibnse strubg nao
response := make(map[string]string)
response["response_token"] = "sha256=" + encoded
//turn response map to json and sent it to writer
responseJson, err := json.Marshal(response)
if err != nil {
log.Println(err)
}
fmt.Fprintf(writer, string(responseJson))
log.Println(string(responseJson))
}
func twitterwebhookdump(writer http.ResponseWriter, req *http.Request) {
fmt.Println("Handler called")
webhookData := make(map[string]interface{})
err := json.NewDecoder(req.Body).Decode(&webhookData)
if err != nil {
fmt.Fprintln(writer, "An error occured in decoding the webhookload: "+err.Error())
log.Println("An error occured in decoding the webhookload: " + err.Error())
}
fmt.Println("got webhook payload: ")
jsonizedata, err := json.Marshal(webhookData)
if err != nil {
log.Panic(err)
return
}
if err != nil {
log.Println("An error occured: " + err.Error())
}
f, err := os.OpenFile("./data.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
//log.Println(err)
log.Panic(err)
return
}
_, err = fmt.Fprintln(f, string(jsonizedata))
if err != nil {
//log.Println(err)
log.Panic(err)
f.Close()
return
}
err = f.Close()
if err != nil {
log.Panic(err)
return
}
log.Println("Latest webhook data dumped sucessfully")
}