~alexwennerberg/twitterbots-go

808f9e3c8b6325179a52e6c7ce20b59d599156ae — alex wennerberg 2 months ago
Simple ability to make tweets
1 files changed, 53 insertions(+), 0 deletions(-)

A main.go
A  => main.go +53 -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
}