Create a gist now

Instantly share code, notes, and snippets.

Bandon FEWS Tweets
// Copyright Conor O'Neill 2016. conor@conoroneill.com
// cd /home/centos/gitwork/go/src/github.com/conoro/bandonfews-tweets/
// go build github.com/conoro/bandonfews-tweets
// go install github.com/conoro/bandonfews-tweets
// sudo systemctl restart bandonfews-tweets
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/ChimeraCoder/anaconda"
)
type Configuration struct {
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessTokenSecret string
}
func main() {
file, _ := os.Open("conf.json")
//file, _ := os.Open("conf-test.json")
decoder := json.NewDecoder(file)
configuration := Configuration{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("config error:", err)
}
anaconda.SetConsumerKey(configuration.ConsumerKey)
anaconda.SetConsumerSecret(configuration.ConsumerSecret)
api := anaconda.NewTwitterApi(configuration.AccessToken, configuration.AccessTokenSecret)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
body := r.PostFormValue("Body")
log.Println(string(body))
if strings.Contains(strings.ToLower(body), "bandon fews") || strings.Contains(strings.ToLower(body), "www.met.ie") {
fmt.Fprintf(w, "Thanks for the update")
if len(body) <= 140 {
// Just post normal Tweet
result, err := api.PostTweet(body, nil)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(result)
fmt.Println("Tweet ID: ", result.Id)
}
} else {
// convert string to slice of words
strSlice := strings.Fields(body)
var originalTweetID int64
tweetParams := url.Values{}
var tweetCount int64 = 0
var subTweet string = ""
var intermediate string = ""
for ind, word := range strSlice {
if ind == 0 {
intermediate = word
} else {
intermediate = intermediate + " " + word
}
// This was 137 (to account for 140 minus "1/ ")
// But the t.co "shortening" is making www.met.ie longer
// So to be safe, I'm making it 137 minus length of t.co link (22)
if len(intermediate) > 115 {
tweetCount++
if tweetCount == 1 {
// Just Tweet and extract the ID from the response
result, err := api.PostTweet("1/ "+subTweet, nil)
fmt.Println("1/ " + subTweet)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(result)
originalTweetID = result.Id
}
} else {
// adding one second delay in Tweetstorm to prevent any throttling by Twitter
time.Sleep(time.Second)
// Tweet in reply to the first tweet by setting in_reply_to_status_id
// originalTweetID
tweetParams.Set("in_reply_to_status_id", strconv.FormatInt(originalTweetID, 10))
result, err := api.PostTweet(strconv.FormatInt(tweetCount, 10)+"/ "+subTweet, tweetParams)
fmt.Println(strconv.FormatInt(tweetCount, 10) + "/ " + subTweet)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(result)
}
}
intermediate = word
subTweet = word
} else {
subTweet = intermediate
}
}
if subTweet != "" {
tweetCount++
// adding one second delay in Tweetstorm to prevent any throttling by Twitter
time.Sleep(time.Second)
// Tweet in reply to the first tweet by setting in_reply_to_status_id
// originalTweetID
tweetParams.Set("in_reply_to_status_id", strconv.FormatInt(originalTweetID, 10))
result, err := api.PostTweet(strconv.FormatInt(tweetCount, 10)+"/ "+subTweet, tweetParams)
fmt.Println(strconv.FormatInt(tweetCount, 10) + "/ " + subTweet)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(result)
}
}
}
}
})
log.Fatal(http.ListenAndServe(":8333", nil))
}
@conoro
Owner
conoro commented Dec 20, 2016

Now splits tweets on word boundaries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment