-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (102 loc) · 3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"github.com/honeybadger-io/honeybadger-go"
"io"
"log"
"net/http"
"os"
)
type Message struct {
From string
Text string
NumImages int
TwilioImageURLs []string
ImageFilenames []string
MBImageURLs []string
MBPostURL string
TwitterMediaIds []string
TwitterPostURL string
}
var config Config
var Version = "development"
func post(message *Message) error {
// download images, if there are any
if message.NumImages > 0 {
err := DownloadTwilioImages(message)
if err != nil {
log.Printf("error downloading from Twilio")
return err
}
}
// post the message to Micro.blog, if it's configured
if config.MicroBlog != (MicroBlogConfig{}) {
err := UploadMessageToMicroBlog(message)
if err != nil {
log.Printf("error posting message to Micro.blog")
return err
}
} else {
log.Printf("no configuration for Micro.blog - skipping")
}
// post the message to Twitter, if it's configured
if config.Twitter != (TwitterConfig{}) {
err := UploadMessageToTwitter(message)
if err != nil {
log.Printf("error posting message to Twitter")
return err
}
} else {
log.Printf("no configuration for Twitter - skipping")
}
return nil
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, "ok")
}
func handler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Printf("error reading parsing form data: %s\n", err)
}
message := ParseTwilioWebhook(r.PostForm)
// check for an unrecognized sender
if message.From == "" {
log.Printf("message from unrecognized number; returning")
_, err = io.WriteString(w, Twiml("your number is not allowed to text here"))
if err != nil {
log.Printf("error writing twiml response")
}
return
}
err = post(&message)
if err != nil && config.HoneybadgerAPIKey != "" {
log.Printf("notifying Honeybadger of err: %s\n", err)
_, _ = honeybadger.Notify(err)
}
// always respond to Twilio (with rose-tinted message)
_, err = io.WriteString(w, Twiml(fmt.Sprintf("message posted %s", message.MBPostURL)))
if err != nil {
log.Printf("error writing twiml response")
}
RemoveTwilioImages(message)
log.Printf("done processing message from %s, with %d images: %q\n", message.From, message.NumImages, message.Text)
}
func main() {
config = LoadConfig()
if config.HoneybadgerAPIKey != "" {
honeybadger.Configure(honeybadger.Configuration{APIKey: config.HoneybadgerAPIKey})
defer honeybadger.Monitor() // reports unhandled panics
}
if config.Logfile != "stderr" && config.Logfile != "" {
file, err := os.OpenFile(config.Logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf("error creating logfile %q: %s", config.Logfile, err)
}
log.SetOutput(file)
}
log.Printf("config loaded; version %q listening on %s%s", Version, config.Server, config.ServerRoute)
http.HandleFunc("/status", statusHandler)
http.HandleFunc(config.ServerRoute, handler)
log.Fatal(http.ListenAndServe(config.Server, nil))
}