mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Examples changes (#201)
This commit is contained in:
58
examples/twitter/function.go
Normal file
58
examples/twitter/function.go
Normal file
@@ -0,0 +1,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("CONFIG_CUSTOMER_KEY"), os.Getenv("CONFIG_CUSTOMER_SECRET"))
|
||||
token := oauth1.NewToken(os.Getenv("CONFIG_ACCESS_TOKEN"), os.Getenv("CONFIG_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)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user