-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitter.go
109 lines (85 loc) · 2.1 KB
/
emitter.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
package fsmtwilio
import (
"bytes"
"encoding/xml"
"errors"
"io/ioutil"
"net/http"
"net/url"
"os"
"reflect"
"strconv"
"strings"
emitable "github.com/go-carrot/fsm-emitable"
)
var (
ACCOUNT_SID = os.Getenv("TWILIO_ACCOUNT_SID")
AUTH_TOKEN = os.Getenv("TWILIO_AUTH_TOKEN")
TWILIO_NUMBER = os.Getenv("TWILIO_NUMBER")
ErrDeliveryFailed = errors.New("Message failed to send")
ErrUndelivered = errors.New("Message was not delivered")
)
type TwilioEmitter struct {
UUID string
}
func (t *TwilioEmitter) Emit(i interface{}) error {
client := http.Client{}
URL := "https://api.twilio.com/2010-04-01/Accounts/" + ACCOUNT_SID + "/Messages"
payload := url.Values{
"To": {t.UUID},
"From": {TWILIO_NUMBER},
}
switch v := i.(type) {
case string:
payload.Add("Body", v)
case emitable.Audio:
payload.Add("MediaUrl", v.URL)
case emitable.Image:
payload.Add("MediaUrl", v.URL)
case emitable.Video:
payload.Add("MediaUrl", v.URL)
case emitable.File:
payload.Add("MediaUrl", v.URL)
case emitable.QuickReply:
body := []string{v.Message + "\n"}
for _, reply := range v.Replies {
body = append(body, "- '"+reply+"'\n")
}
payload.Add("Body", strings.Join(body, ""))
case emitable.Typing:
return nil
default:
return errors.New("TwilioEmitter cannot handle " + reflect.TypeOf(i).String())
}
body := payload.Encode()
req, err := http.NewRequest(http.MethodPost, URL, bytes.NewBufferString(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", strconv.Itoa(len(body)))
req.SetBasicAuth(ACCOUNT_SID, AUTH_TOKEN)
res, err := client.Do(req)
if err != nil {
return err
}
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
response := new(MessageSentCallback)
err = xml.Unmarshal([]byte(resBody), &response)
if err != nil {
return err
}
if response.Message.ErrorCode != "" {
return errors.New(response.Message.ErrorMessage)
}
switch response.Message.Status {
case "failed":
return ErrDeliveryFailed
case "undelivered":
return ErrUndelivered
}
return nil
}