forked from iron-io/functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
58 lines (44 loc) · 1.15 KB
/
function.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
type payload struct {
Username string `json:"username"`
}
func main() {
username := "getiron"
// Getting username in payload
envPayload := os.Getenv("PAYLOAD")
if envPayload != "" {
var pl payload
err := json.Unmarshal([]byte(envPayload), &pl)
if err != nil {
log.Println("Invalid payload")
return
}
if pl.Username != "" {
username = pl.Username
}
}
fmt.Println("Looking for tweets of the account:", username)
// Twitter auth config
config := oauth1.NewConfig(os.Getenv("CUSTOMER_KEY"), os.Getenv("CUSTOMER_SECRET"))
token := oauth1.NewToken(os.Getenv("ACCESS_TOKEN"), os.Getenv("ACCESS_SECRET"))
httpClient := config.Client(oauth1.NoContext, token)
// Twitter client
client := twitter.NewClient(httpClient)
// Load tweets
tweets, _, err := client.Timelines.UserTimeline(&twitter.UserTimelineParams{ScreenName: username})
if err != nil {
fmt.Println("Error loading tweets: ", err)
}
// Show tweets
for _, tweet := range tweets {
fmt.Println(tweet.User.Name + ": " + tweet.Text)
}
}