Add Slack mechanism
parent
b69b96c7e9
commit
233a0b0631
|
@ -1,8 +1,11 @@
|
|||
package main // import "github.com/productionwentdown/email-collector"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -16,11 +19,13 @@ import (
|
|||
var filename string
|
||||
var listen string
|
||||
var redirect string
|
||||
var slack string
|
||||
|
||||
func main() {
|
||||
flag.StringVar(&filename, "file", "list.csv", "file to append records to")
|
||||
flag.StringVar(&listen, "listen", ":8080", "address to listen to")
|
||||
flag.StringVar(&redirect, "redirect", "/subscribed", "path to redirect to upon success")
|
||||
flag.StringVar(&slack, "slack", "", "optional slack webhook url")
|
||||
flag.Parse()
|
||||
|
||||
csvMutex := &sync.Mutex{}
|
||||
|
@ -58,6 +63,30 @@ func main() {
|
|||
csvWriter.Write([]string{email, time.Now().String()})
|
||||
csvWriter.Flush()
|
||||
csvMutex.Unlock()
|
||||
if len(slack) > 4 {
|
||||
object := map[string]string{
|
||||
"text": "A human (" + email + ") subscribed to the list! Wohoo!",
|
||||
}
|
||||
payload, err := json.Marshal(object)
|
||||
if err != nil {
|
||||
log.Println("json.Marshal failed, not supposed to happen!")
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
resp, err := http.Post(slack, "application/json", bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
log.Println(body)
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
}
|
||||
w.Header().Add("Location", redirect)
|
||||
w.WriteHeader(303)
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue