-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
375 lines (344 loc) · 9.25 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright 2016 Arne Roomann-Kurrik
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"github.com/kurrik/oauth1a"
"github.com/kurrik/twittergo"
"github.com/kurrik/witgo/v1/witgo"
"io/ioutil"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
)
const MINWAIT = time.Duration(10) * time.Second
type TwitterUser struct {
ID int64 `json:id`
Name string `json:name`
ScreenName string `json:screen_name`
Following bool `json:following`
}
type DirectMessage struct {
ID int64 `json:id`
Text string `json:text`
Sender TwitterUser `json:sender`
Recipient TwitterUser `json:recipient`
CreatedAt string `json:created_at`
}
type DirectMessageList []DirectMessage
func (l DirectMessageList) Len() int { return len(l) }
func (l DirectMessageList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l DirectMessageList) Less(i, j int) bool { return l[i].ID < l[j].ID }
type directMessageRequest struct {
Text string
UserID int64
}
type TwitterClient struct {
client *twittergo.Client
processedToID int64
PollInterval time.Duration
outgoing chan directMessageRequest
}
func NewTwitterClient(cred Credentials) (out *TwitterClient) {
var (
config *oauth1a.ClientConfig
user *oauth1a.UserConfig
)
config = &oauth1a.ClientConfig{
ConsumerKey: cred.TwitterConsumerKey,
ConsumerSecret: cred.TwitterConsumerSecret,
}
user = oauth1a.NewAuthorizedConfig(
cred.TwitterAccessToken,
cred.TwitterAccessTokenSecret,
)
out = &TwitterClient{
client: twittergo.NewClient(config, user),
PollInterval: time.Minute,
}
return
}
func (c *TwitterClient) SetProcessedMarkerToCurrent() (err error) {
var (
existing DirectMessageList
)
if existing, err = c.fetchDirectMessages(0, 1); err != nil {
return
}
if len(existing) > 0 {
c.processedToID = existing[0].ID
}
return
}
func (c *TwitterClient) fetchDirectMessages(sinceID int64, count int) (data DirectMessageList, err error) {
var (
req *http.Request
reqURL string
query url.Values
resp *twittergo.APIResponse
)
query = url.Values{}
query.Set("since_id", fmt.Sprintf("%v", sinceID))
query.Set("count", fmt.Sprintf("%v", count))
reqURL = fmt.Sprintf("/1.1/direct_messages.json?%v", query.Encode())
if req, err = http.NewRequest("GET", reqURL, nil); err != nil {
return
}
if resp, err = c.client.SendRequest(req); err != nil {
return
}
if err = resp.Parse(&data); err != nil {
return
}
return
}
func (c *TwitterClient) handleFetchError(err error) error {
switch e := err.(type) {
case twittergo.RateLimitError:
dur := e.Reset.Sub(time.Now()) + time.Second
if dur < MINWAIT {
dur = MINWAIT
}
fmt.Printf("Rate limited, reset at %v, waiting for %v\n", e.Reset, dur)
time.Sleep(dur)
return nil
}
return err
}
func (c *TwitterClient) makeSessionID(userID int64) witgo.SessionID {
return witgo.SessionID(fmt.Sprintf("%v-%v", userID, time.Now().Unix()))
}
func (c *TwitterClient) parseSessionID(sessionID witgo.SessionID) (userID int64, err error) {
var lines []string
lines = strings.Split(string(sessionID), "-")
userID, err = strconv.ParseInt(lines[0], 10, 64)
return
}
// Drains the channel, we don't do anything with these requests.
func (c *TwitterClient) runPending(requests <-chan witgo.SessionID) {
var req witgo.SessionID
for req = range requests {
fmt.Printf("Discarding request %v\n", req)
}
}
func (c *TwitterClient) runFetch(records chan<- witgo.InputRecord) {
var (
messages DirectMessageList
message DirectMessage
err error
tick <-chan time.Time
)
defer close(records)
tick = time.Tick(c.PollInterval)
for true {
fmt.Printf("Requesting direct messages\n")
if messages, err = c.fetchDirectMessages(c.processedToID, 100); err != nil {
if err = c.handleFetchError(err); err != nil {
fmt.Printf("ERROR: %v\n", err)
return
}
}
fmt.Printf("Got %v messages\n", len(messages))
sort.Sort(messages)
for _, message = range messages {
if message.ID > c.processedToID {
records <- witgo.InputRecord{
SessionID: c.makeSessionID(message.Sender.ID),
Query: message.Text,
}
c.processedToID = message.ID
}
}
fmt.Printf("Waiting for next poll interval\n")
<-tick
}
return
}
func (c *TwitterClient) sendMessage(msg directMessageRequest) (err error) {
var (
req *http.Request
reqURL string
query url.Values
)
query = url.Values{}
query.Set("user_id", fmt.Sprintf("%v", msg.UserID))
query.Set("text", msg.Text)
reqURL = fmt.Sprintf("/1.1/direct_messages/new.json?%v", query.Encode())
if req, err = http.NewRequest("POST", reqURL, nil); err != nil {
return
}
if _, err = c.client.SendRequest(req); err != nil {
return
}
return
}
func (c *TwitterClient) runWrite(messages <-chan directMessageRequest) {
var (
msg directMessageRequest
err error
retry bool
)
for msg = range messages {
retry = true
for retry {
fmt.Printf("Sending `%v` to user %v\n", msg.Text, msg.UserID)
if err = c.sendMessage(msg); err != nil {
if err = c.handleFetchError(err); err != nil {
fmt.Printf("ERROR: %v, will not retry write\n", err)
retry = false
}
} else {
retry = false
}
}
}
return
}
func (c *TwitterClient) Run() (chan<- witgo.SessionID, <-chan witgo.InputRecord) {
var (
requests = make(chan witgo.SessionID)
records = make(chan witgo.InputRecord)
)
c.outgoing = make(chan directMessageRequest, 10)
go c.runPending(requests)
go c.runFetch(records)
go c.runWrite(c.outgoing)
return requests, records
}
func (c *TwitterClient) Action(session *witgo.Session, action string) (response *witgo.Session, err error) {
response = session
response.Context.Set("forecast", "sunny")
return
}
func (c *TwitterClient) Say(session *witgo.Session, msg string) (response *witgo.Session, err error) {
var userID int64
response = session
if userID, err = c.parseSessionID(session.ID()); err != nil {
return
}
c.outgoing <- directMessageRequest{
UserID: userID,
Text: msg,
}
return
}
func (c *TwitterClient) Merge(session *witgo.Session, entities witgo.EntityMap) (response *witgo.Session, err error) {
var (
value string
)
response = session
if value, err = entities.FirstEntityValue("location"); err != nil {
response.Context = witgo.Context{}
err = nil
} else {
response.Context.Merge(witgo.Context{
"loc": value,
})
}
return
}
func (c *TwitterClient) Error(session *witgo.Session, msg string) {
}
var noCredentialsErr = fmt.Errorf(`You must specify a credentials path using the -credentials flag!
This must point to a 4-line file with the following format:
<Twitter consumer key>
<Twitter consumer secret>
<Twitter access token>
<Twitter access token secret>
<Wit.ai token>`)
type Credentials struct {
TwitterConsumerKey string
TwitterConsumerSecret string
TwitterAccessToken string
TwitterAccessTokenSecret string
WitgoServerToken string
}
func loadCredentials(path string) (out Credentials, err error) {
var (
credentials []byte
lines []string
)
if path == "" {
err = noCredentialsErr
return
}
if credentials, err = ioutil.ReadFile(path); err != nil {
return
}
lines = strings.Split(string(credentials), "\n")
if len(lines) < 5 {
err = fmt.Errorf("Credentials file did not have enough lines!")
return
}
out = Credentials{
TwitterConsumerKey: lines[0],
TwitterConsumerSecret: lines[1],
TwitterAccessToken: lines[2],
TwitterAccessTokenSecret: lines[3],
WitgoServerToken: lines[4],
}
return
}
func processError(err error) {
switch e := err.(type) {
case twittergo.RateLimitError:
fmt.Printf("Rate limited, reset at %v\n", e.Reset)
case twittergo.Errors:
for i, val := range e.Errors() {
fmt.Printf("Error #%v - ", i+1)
fmt.Printf("Code: %v ", val.Code())
fmt.Printf("Msg: %v\n", val.Message())
}
default:
fmt.Printf("There was an error running the script: %v\n", err)
}
os.Exit(1)
}
func main() {
var (
credentialsPath string
credentials Credentials
processedTo int64
err error
twitter *TwitterClient
witai *witgo.Witgo
)
flag.StringVar(&credentialsPath, "credentials", "", "Path to credentials file")
flag.Int64Var(&processedTo, "processedTo", -1, "Override ID to start processing from")
flag.Parse()
if credentials, err = loadCredentials(credentialsPath); err != nil {
processError(err)
}
twitter = NewTwitterClient(credentials)
if processedTo == -1 {
if err = twitter.SetProcessedMarkerToCurrent(); err != nil {
processError(err)
}
} else {
twitter.processedToID = processedTo
}
fmt.Printf("Processed to: %v\n", twitter.processedToID)
witai = witgo.NewWitgo(
witgo.NewClient(credentials.WitgoServerToken),
twitter,
)
if err = witai.Process(twitter); err != nil {
processError(err)
}
}