-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
145 lines (125 loc) · 3.63 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bytes"
"encoding/json"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/s3"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
var config = Configuration{
SlackName: os.Getenv("SLACK_NAME"),
SlackIconUrl: os.Getenv("SLACK_ICON_URL"),
SlackWebHookUrl: os.Getenv("SLACK_WEBHOOK_URL"),
SlackChannel: os.Getenv("SLACK_CHANNEL"),
Host: os.Getenv("HOST"),
VoicemailAudio: os.Getenv("VOICEMAIL_AUDIO"),
ElksUserName: os.Getenv("ELKS_USERNAME"),
ElksPassword: os.Getenv("ELKS_PASSWORD"),
AWSAccessKey: os.Getenv("AWS_ACCESS_KEY"),
AWSSecretKey: os.Getenv("AWS_SECRET_KEY"),
S3BucketName: os.Getenv("S3_BUCKET_NAME"),
}
func main() {
AWSAuth := aws.Auth{
AccessKey: config.AWSAccessKey,
SecretKey: config.AWSSecretKey,
}
region := aws.EUWest
http.HandleFunc("/incoming_call", func(w http.ResponseWriter, r *http.Request) {
i := IncomingResponse{
Play: config.VoicemailAudio,
Next: struct {
Record string `json:"record"`
}{
Record: config.Host + "/voicemail",
},
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(i)
})
http.HandleFunc("/voicemail", func(w http.ResponseWriter, r *http.Request) {
log.Print("Incoming message...")
err := r.ParseForm()
check(err)
from := r.FormValue("from")
wav := r.FormValue("wav")
if len(wav) == 0 || len(from) == 0 {
log.Print("Bad request")
http.Error(w, "", http.StatusBadRequest)
return
}
log.Print("Incoming message from " + from)
log.Print("Retrieving audio file " + wav + "...")
req, err := http.NewRequest("GET", wav, nil)
req.SetBasicAuth(config.ElksUserName, config.ElksPassword)
client := &http.Client{}
resp, err := client.Do(req)
check(err)
defer resp.Body.Close()
log.Print("Downloading...")
audio, err := ioutil.ReadAll(resp.Body)
check(err)
log.Print("Connecting to AWS S3...")
connection := s3.New(AWSAuth, region)
bucket := connection.Bucket(config.S3BucketName)
path := "voicemail/" + time.Now().Format("20060102150405") + ".wav"
log.Print("Writing " + path + " to " + config.S3BucketName + "...")
err = bucket.Put(path, audio, "audio/wav", s3.ACL("public-read"))
check(err)
log.Print("Posting message to Slack, channel " + config.SlackChannel + "...")
payload := SlackPayload{
UserName: config.SlackName,
IconUrl: config.SlackIconUrl,
Text: "New voice message from " + from + " <" + bucket.URL(path) + ">!",
Channel: config.SlackChannel,
}
json_payload, _ := json.Marshal(payload)
slack_req, err := http.NewRequest("POST", config.SlackWebHookUrl, bytes.NewBuffer([]byte(json_payload)))
req.Header.Set("Content-Type", "application/json")
slack_client := &http.Client{}
slack_resp, err := slack_client.Do(slack_req)
check(err)
defer slack_resp.Body.Close()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(nil)
})
port := os.Getenv("PORT")
if len(port) == 0 {
port = "3000"
}
http.ListenAndServe(":"+port, nil)
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
type Configuration struct {
SlackName string
SlackIconUrl string
SlackWebHookUrl string
SlackChannel string
Host string
VoicemailAudio string
ElksUserName string
ElksPassword string
AWSAccessKey string
AWSSecretKey string
S3BucketName string
}
type SlackPayload struct {
UserName string `json:"username"`
IconUrl string `json:"icon_url"`
Text string `json:"text"`
Channel string `json:"channel"`
}
type IncomingResponse struct {
Play string `json:"play"`
Next struct {
Record string `json:"record"`
} `json:"next"`
}