-
Notifications
You must be signed in to change notification settings - Fork 3
/
twitter.go
59 lines (45 loc) · 1.42 KB
/
twitter.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
package inputs
import (
"log"
"strings"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
"github.com/hexbotio/hex/models"
)
// Twitter Struct representing the
type Twitter struct {
}
// Input Input to the Twitter stream api
func (x Twitter) Read(inputMsgs chan<- models.Message, service models.Service) {
defer Recovery(service)
config := oauth1.NewConfig(service.Config["Key"], service.Config["Secret"])
token := oauth1.NewToken(service.Config["AccessToken"], service.Config["AccessTokenSecret"])
// OAuth1 http.Client will automatically authorize Requests
httpClient := config.Client(oauth1.NoContext, token)
// Twitter Client
client := twitter.NewClient(httpClient)
// Convenience Demux demultiplexed stream messages
demux := twitter.NewSwitchDemux()
demux.Tweet = func(tweet *twitter.Tweet) {
message := models.MakeMessage(service.Type, service.Name, "", tweet.User.ScreenName, tweet.Text)
message.Inputs["hex.twitter.lang"] = tweet.Lang
inputMsgs <- message
}
demux.DM = func(dm *twitter.DirectMessage) {
// do nothing
}
demux.Event = func(event *twitter.Event) {
// do nothing
}
// FILTER
filterParams := &twitter.StreamFilterParams{
Track: strings.Split(service.Config["Filter"], ","),
StallWarnings: twitter.Bool(true),
}
stream, err := client.Streams.Filter(filterParams)
if err != nil {
log.Println(err)
return
}
go demux.HandleChan(stream.Messages)
}