github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/examples/twitter/function.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  
     9  	"github.com/dghubble/go-twitter/twitter"
    10  	"github.com/dghubble/oauth1"
    11  )
    12  
    13  type payload struct {
    14  	Username string `json:"username"`
    15  }
    16  
    17  func main() {
    18  	username := "getiron"
    19  
    20  	// Getting username in payload
    21  	envPayload := os.Getenv("PAYLOAD")
    22  	if envPayload != "" {
    23  		var pl payload
    24  
    25  		err := json.Unmarshal([]byte(envPayload), &pl)
    26  		if err != nil {
    27  			log.Println("Invalid payload")
    28  			return
    29  		}
    30  
    31  		if pl.Username != "" {
    32  			username = pl.Username
    33  		}
    34  	}
    35  
    36  	fmt.Println("Looking for tweets of the account:", username)
    37  
    38  	// Twitter auth config
    39  	config := oauth1.NewConfig(os.Getenv("CUSTOMER_KEY"), os.Getenv("CUSTOMER_SECRET"))
    40  	token := oauth1.NewToken(os.Getenv("ACCESS_TOKEN"), os.Getenv("ACCESS_SECRET"))
    41  
    42  	httpClient := config.Client(oauth1.NoContext, token)
    43  
    44  	// Twitter client
    45  	client := twitter.NewClient(httpClient)
    46  
    47  	// Load tweets
    48  	tweets, _, err := client.Timelines.UserTimeline(&twitter.UserTimelineParams{ScreenName: username})
    49  	if err != nil {
    50  		fmt.Println("Error loading tweets: ", err)
    51  	}
    52  
    53  	// Show tweets
    54  	for _, tweet := range tweets {
    55  		fmt.Println(tweet.User.Name + ": " + tweet.Text)
    56  	}
    57  
    58  }