Advertisement
romerocm

day02-07-example2

Apr 19th, 2022
1,535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.09 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     // other imports
  5.     "fmt"
  6.     "log"
  7.     "os"
  8.  
  9.     "github.com/dghubble/go-twitter/twitter"
  10.     "github.com/dghubble/oauth1"
  11. )
  12.  
  13. // Credentials stores all of our access/consumer tokens
  14. // and secret keys needed for authentication against
  15. // the twitter REST API.
  16. type Credentials struct {
  17.     ConsumerKey       string
  18.     ConsumerSecret    string
  19.     AccessToken       string
  20.     AccessTokenSecret string
  21. }
  22.  
  23. // getClient is a helper function that will return a twitter client
  24. // that we can subsequently use to send tweets, or to stream new tweets
  25. // this will take in a pointer to a Credential struct which will contain
  26. // everything needed to authenticate and return a pointer to a twitter Client
  27. // or an error
  28. func getClient(creds *Credentials) (*twitter.Client, error) {
  29.     // Pass in your consumer key (API Key) and your Consumer Secret (API Secret)
  30.     config := oauth1.NewConfig(creds.ConsumerKey, creds.ConsumerSecret)
  31.     // Pass in your Access Token and your Access Token Secret
  32.     token := oauth1.NewToken(creds.AccessToken, creds.AccessTokenSecret)
  33.  
  34.     httpClient := config.Client(oauth1.NoContext, token)
  35.     client := twitter.NewClient(httpClient)
  36.  
  37.     // Verify Credentials
  38.     verifyParams := &twitter.AccountVerifyParams{
  39.         SkipStatus:   twitter.Bool(true),
  40.         IncludeEmail: twitter.Bool(true),
  41.     }
  42.  
  43.     // we can retrieve the user and verify if the credentials
  44.     // we have used successfully allow us to log in!
  45.     user, _, err := client.Accounts.VerifyCredentials(verifyParams)
  46.     if err != nil {
  47.         return nil, err
  48.     }
  49.  
  50.     log.Printf("User's ACCOUNT:\n%+v\n", user)
  51.     return client, nil
  52. }
  53. func main() {
  54.     creds := Credentials{
  55.         AccessToken:       os.Getenv("ACCESS_TOKEN"),
  56.         AccessTokenSecret: os.Getenv("ACCESS_TOKEN_SECRET"),
  57.         ConsumerKey:       os.Getenv("CONSUMER_KEY"),
  58.         ConsumerSecret:    os.Getenv("CONSUMER_SECRET"),
  59.     }
  60.     {
  61.         const DaysTotal int = 60
  62.         var remainingDays uint = 60
  63.         challenge := "#DevOpsTraineeProgram"
  64.  
  65.         fmt.Printf("Welcome to the %v challenge.\nThis challenge consists of %v days\n", challenge, DaysTotal)
  66.  
  67.         var TwitterName string
  68.         var DaysCompleted uint
  69.  
  70.         // asking for user input
  71.         fmt.Println("Enter Your Twitter Handle: ")
  72.         fmt.Scanln(&TwitterName)
  73.  
  74.         fmt.Println("How many days have you completed?: ")
  75.         fmt.Scanln(&DaysCompleted)
  76.  
  77.         // calculate remaining days
  78.         remainingDays = remainingDays - DaysCompleted
  79.  
  80.         //fmt.Printf("Thank you %v for taking part and completing %v days.\n", TwitterName, DaysCompleted)
  81.         //fmt.Printf("You have %v days remaining for the %v challenge\n", remainingDays, challenge)
  82.         // fmt.Println("Good luck")
  83.  
  84.         client, err := getClient(&creds)
  85.         if err != nil {
  86.             log.Println("Error getting Twitter Client, this is expected if you did not supply your Twitter API tokens")
  87.             log.Println(err)
  88.         }
  89.  
  90.         message := fmt.Sprintf("Hey I am %v I have been doing the %v for %v days and I have %v Days left", TwitterName, challenge, DaysCompleted, remainingDays)
  91.         tweet, resp, err := client.Statuses.Update(message, nil)
  92.         if err != nil {
  93.             log.Println(err)
  94.         }
  95.         log.Printf("%+v\n", resp)
  96.         log.Printf("%+v\n", tweet)
  97.     }
  98.  
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement