created twitter example

This commit is contained in:
Henrique Chehad
2016-09-14 10:33:31 -03:00
committed by Travis Reeder
parent 042bccb2e6
commit 978224ecf8
3 changed files with 146 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
FROM iron/go:dev
ADD . $GOPATH/src/github.com/iron-io/functions/examples/twitter
WORKDIR $GOPATH/src/github.com/iron-io/functions/examples/twitter
RUN go get .
ENTRYPOINT ["go", "run", "main.go"]

View File

@@ -0,0 +1,80 @@
# Twitter Example
This function exemplifies an authentication in Twitter API and get latest tweets of an account.
## Requirements
You need create or configure a [Twitter App](https://apps.twitter.com/) and configure Customer Access and Access Token.
Reference: https://dev.twitter.com/oauth/overview/application-owner-access-tokens
## Development
### Building image locally
```
# SET BELOW TO YOUR DOCKER HUB USERNAME
export USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it
docker build -t $USERNAME/functions-twitter .
```
### Publishing it
```
docker push $USERNAME/functions-twitter
```
## Running it on IronFunctions
You need a running IronFunctions API
### First, let's define this environment variables
Set your Twitter Credentials in environment variables.
```sh
export CUSTOMER_KEY="XXXXXX"
export CUSTOMER_SECRET="XXXXXX"
export ACCESS_TOKEN="XXXXXX"
export ACCESS_SECRET="XXXXXX"
```
### Create the application
```sh
curl -H "Content-Type: application/json" -X POST -d '{
"app": {
"name": "twitter",
"config": {
"CUSTOMER_KEY": "'$CUSTOMER_KEY'",
"CUSTOMER_SECRET": "'$CUSTOMER_SECRET'",
"ACCESS_TOKEN": "'$ACCESS_TOKEN'",
"ACCESS_SECRET": "'$ACCESS_SECRET'"
}
}
}' http://localhost:8080/v1/apps
```
### Add the route
```sh
curl -H "Content-Type: application/json" -X POST -d '{
"route": {
"path":"/tweets",
"image":"'$USERNAME/functions-twitter'"
}
}' http://localhost:8080/v1/apps/twitter/routes
```
### Calling the function
```sh
# Latests tweets of default account (getiron)
curl http://localhost:8080/r/twitter/tweets
# Latests tweets of specific account
curl -X POST --data '{"username": "getiron"}' http://localhost:8080/r/twitter/tweets
```

58
examples/twitter/main.go Normal file
View 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)
}
}