twitterbots-go

twitter bots @soulstxt and @lacroix-bot
Log | Files | Refs | README | LICENSE

commit 808f9e3c8b6325179a52e6c7ce20b59d599156ae
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 26 Sep 2020 21:48:34 -0700

Simple ability to make tweets

Diffstat:
Amain.go | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+), 0 deletions(-)

diff --git a/main.go b/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "encoding/json" + "fmt" + "github.com/dghubble/go-twitter/twitter" + "github.com/dghubble/oauth1" + "os" + "path" +) + +func main() { + client, err := getCreds("soulstxt") + if err != nil { + // temp + panic(err) + } + _, _, err = client.Statuses.Update("just setting up my twttr", nil) + if err != nil { + panic(err) + } +} + +type TwitterCreds struct { + ConsumerKey string + ConsumerSecret string + AccessToken string + AccessSecret string +} + +func getCreds(serviceName string) (*twitter.Client, error) { + home, err := os.UserHomeDir() + if err != nil { + return nil, err + } + fileName := path.Join(home, ".config", "twitter-creds", serviceName+".conf") + file, err := os.Open(fileName) + if err != nil { + return nil, err + } + defer file.Close() + decoder := json.NewDecoder(file) + configuration := TwitterCreds{} + err = decoder.Decode(&configuration) + if err != nil { + fmt.Println("error:", err) + } + config := oauth1.NewConfig(configuration.ConsumerKey, configuration.ConsumerSecret) + token := oauth1.NewToken(configuration.AccessToken, configuration.AccessSecret) + httpClient := config.Client(oauth1.NoContext, token) + twitterClient := twitter.NewClient(httpClient) + return twitterClient, nil +}