├── .gitignore ├── .goxc.json ├── Dockerfile ├── LICENSE ├── README.md ├── config.example.json ├── glide.lock ├── glide.yaml └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | debian/ 3 | config.json 4 | -------------------------------------------------------------------------------- /.goxc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ArtifactsDest": "build", 3 | "Arch": "386,amd64", 4 | "Os": "darwin,linux,windows", 5 | "PackageVersion": "1.0.0", 6 | "ConfigVersion": "0.9", 7 | "TaskSettings": { 8 | "pkg-build": { 9 | "metadata": { 10 | "description": "Send interesting tweets from Twitter to Slack", 11 | "maintainer": "Mathias Biilmann (http://mathias-biilmann.net)" 12 | }, 13 | "metadata-deb": { 14 | "Depends": "", 15 | "Homepage": "https://github.com/netlify/twickr" 16 | } 17 | } 18 | }, 19 | "Resources": { 20 | "Include": "README,LICENSE,config.example.json" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.10.2 2 | WORKDIR /go/src/github.com/netlify/twickr 3 | COPY . /go/src/github.com/netlify/twickr/ 4 | ENV CGO_ENABLED 0 5 | RUN go get -u github.com/Masterminds/glide && glide install && go build 6 | 7 | FROM alpine:3.7 8 | RUN apk add --no-cache ca-certificates 9 | COPY --from=0 /go/src/github.com/netlify/twickr/twickr /twickr/twickr 10 | WORKDIR /twickr 11 | CMD ["./twickr"] 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) <2015> MakerLoop Inc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Twickr 2 | 3 | > Send interesting tweets from Twitter to Slack. 4 | 5 | Twickr connects to Twitter and listens for keywords you're interested in. Whenever a keyword is mentioned in a tweet, Twickr sends you a message over Slack. 6 | 7 | ## Installation 8 | 9 | Download the relevant release for your server's OS and architecture and extract the files. 10 | 11 | [Twickr Downloads](https://github.com/netlify/twickr/releases) 12 | 13 | ## Configuration 14 | 15 | Before you can run Twickr, you need to setup a [Twitter application](https://apps.twitter.com/) and a [Slack Inbound Webhook](https://netlify.slack.com/services/new/incoming-webhook). 16 | 17 | ### Twitter Setup 18 | 19 | 1. Go to [Twitter's Application Management](https://apps.twitter.com/) and create a new app. 20 | 2. Fill out the name description and website and create the app. 21 | 3. Go to "Keys and Access Tokens" and create a new access token 22 | 23 | ### Slack Setup 24 | 25 | 1. Create a new Inbound Webhook for your Slack account [here](https://netlify.slack.com/services/new/incoming-webhook) 26 | 27 | ### The Configuration File 28 | 29 | Create a `config.json` file in the same folder as the twickr executable. An example configuration file is included in the download. 30 | 31 | 1. Fill in the credentials for your new Twitter app and the URL from your Slack webhook. 32 | 2. Optionally specify which channel to send Twickr messages to 33 | 3. Enter a coma-separated list of keywords you want Twickr to keep an eye on 34 | 35 | ## Running 36 | 37 | ```bash 38 | cd 39 | ./twickr 40 | ``` 41 | 42 | Normally you'll run Twickr on a server (rather than on your laptop or desktop machine) where it can keep running continuously. An easy way to make sure it stays up after you close your SSH connection is to run it in a TMUX or Screen session. 43 | 44 | ## Netlify 45 | 46 | Twickr is a small tool developed by [Netlify](https://www.netlify.com). Netlify builds, deploys & hosts static sites and apps. 47 | -------------------------------------------------------------------------------- /config.example.json: -------------------------------------------------------------------------------- 1 | { 2 | "consumerKey": "TWITTER CONSUMER KEY", 3 | "consumerSecret": "TWITTER CONSUMER SECRET", 4 | "accessToken": "TWITTER ACCESS TOKEN", 5 | "accessSecret": "TWITTER ACCESS SECRET", 6 | "slackWebhook": "SLACK INCOMING WEBHOOK", 7 | "slackChannel": "OPTIONAL SLACK CHANNEL TO SEND TO", 8 | "keywords": "keywords,you want,to track", 9 | "keywordsIgnored": "OPTIONAL,keywords,you don't want,to track" 10 | } 11 | -------------------------------------------------------------------------------- /glide.lock: -------------------------------------------------------------------------------- 1 | hash: addc551c0abd74ac6ad6160a20987b2a0b1dc18f28e58e5ed3ec0daa3f0a51c2 2 | updated: 2018-05-07T17:58:49.92964+02:00 3 | imports: 4 | - name: github.com/darkhelmet/twitterstream 5 | version: 0a79e05b284c9a009c99f6f7d79a188c78f5623b 6 | - name: github.com/garyburd/go-oauth 7 | version: bca2e7f09a178fd36b034107a00e2323bca6a82e 8 | subpackages: 9 | - oauth 10 | - name: golang.org/x/net 11 | version: 4b14673ba32bee7f5ac0f990a48f033919fd418b 12 | subpackages: 13 | - context 14 | testImports: [] 15 | -------------------------------------------------------------------------------- /glide.yaml: -------------------------------------------------------------------------------- 1 | package: github.com/netlify/twickr 2 | import: 3 | - package: github.com/darkhelmet/twitterstream 4 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "io/ioutil" 8 | "log" 9 | "net/http" 10 | "time" 11 | "strings" 12 | 13 | "github.com/darkhelmet/twitterstream" 14 | ) 15 | 16 | var config *Config 17 | 18 | // Config holds all the configuration data 19 | type Config struct { 20 | ConsumerKey string `json:"consumerKey"` 21 | ConsumerSecret string `json:"consumerSecret"` 22 | AccessToken string `json:"accessToken"` 23 | AccessSecret string `json:"accessSecret"` 24 | SlackWebhook string `json:"slackWebhook"` 25 | SlackChannel string `json:"slackChannel"` 26 | Keywords string `json:"keywords"` 27 | KeywordsIgnored string `json:"keywordsIgnored"` 28 | } 29 | 30 | type attachment map[string]string 31 | 32 | // SlackMsg represents a message sent to Slack over a webhook 33 | type SlackMsg struct { 34 | User string `json:"username"` 35 | Text string `json:"text"` 36 | Channel *string `json:"channel"` 37 | Attachments []attachment `json:"attachments"` 38 | } 39 | 40 | func init() { 41 | config = &Config{} 42 | data, err := ioutil.ReadFile("config.json") 43 | if err != nil { 44 | log.Fatalf("Error reading configuration: %v", err) 45 | } 46 | err = json.Unmarshal(data, config) 47 | if err != nil { 48 | log.Fatalf("Error parsing configuration: %v", err) 49 | } 50 | } 51 | 52 | func decode(conn *twitterstream.Connection) { 53 | for { 54 | if tweet, err := conn.Next(); err == nil { 55 | if !testIgnoredKeywords(tweet.Text) { 56 | continue 57 | } 58 | msg := createMessage(tweet) 59 | b, err := json.Marshal(msg) 60 | if err != nil { 61 | log.Printf("Error encoding json for tweet: %v %v\n", tweet, err) 62 | } else { 63 | buf := bytes.NewReader(b) 64 | http.Post(config.SlackWebhook, "application/json", buf) 65 | } 66 | 67 | log.Printf("%s said: %s\n", tweet.User.ScreenName, tweet.Text) 68 | } else { 69 | log.Printf("Failed decoding tweet: %s", err) 70 | return 71 | } 72 | } 73 | } 74 | 75 | func testIgnoredKeywords(tweetText string) bool { 76 | if config.KeywordsIgnored != "" { 77 | for _, keyword := range strings.Split(config.KeywordsIgnored, ",") { 78 | if strings.Contains(tweetText, keyword) { 79 | log.Printf("Tweet contained ignored keyword: %s", keyword) 80 | return false 81 | } 82 | } 83 | } 84 | return true 85 | } 86 | 87 | func createMessage(tweet *twitterstream.Tweet) *SlackMsg { 88 | text := "https://twitter.com/" + tweet.User.IdStr + "/status/" + tweet.IdString 89 | 90 | att := attachment{ 91 | "color": "grey", 92 | "author_icon": tweet.User.ProfileImageUrlHttps, 93 | "author_name": fmt.Sprintf("%s @%s", tweet.User.Name, tweet.User.ScreenName), 94 | "author_link": fmt.Sprintf("https://twitter.com/%s", tweet.User.ScreenName), 95 | "text": tweet.Text, 96 | } 97 | 98 | if len(tweet.Entities.Media) > 0 { 99 | att["image_url"] = tweet.Entities.Media[0].SecureMediaUrl 100 | } 101 | 102 | return &SlackMsg{ 103 | User: tweet.User.ScreenName, 104 | Text: text, 105 | Attachments: []attachment{att}, 106 | Channel: &config.SlackChannel, 107 | } 108 | } 109 | 110 | func main() { 111 | client := twitterstream.NewClient( 112 | config.ConsumerKey, 113 | config.ConsumerSecret, 114 | config.AccessToken, 115 | config.AccessSecret, 116 | ) 117 | for { 118 | conn, err := client.Track(config.Keywords) 119 | if err != nil { 120 | log.Printf("Tracking failed, sleeping for 1 minute: %v", err) 121 | time.Sleep(1 * time.Minute) 122 | continue 123 | } 124 | decode(conn) 125 | } 126 | } 127 | --------------------------------------------------------------------------------