This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
134 lines (109 loc) · 4.12 KB
/
sender.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
package main
import (
// "bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
log "github.com/sirupsen/logrus"
)
// TwillioSender abstracts interactions with the twilio API
type TwillioSender struct {
endpoint, user, token, from string
}
// SendMessage sends a SMS with the provided body to the number. It will return
// an error if sending fails
func (s TwillioSender) SendMessage(msgTo, msgBody string) error {
var jsonData []byte
var err error
// Set required data
data := url.Values{}
data.Set("To", msgTo)
data.Set("From", s.from)
values := map[string]string{
"type": "nachricht",
"message": msgBody,
}
if jsonData, err = json.Marshal(values); err != nil {
return err
}
data.Set("Parameters", string(jsonData))
// Create a new client and request
client := &http.Client{}
r, err := http.NewRequest("POST", s.endpoint, strings.NewReader(data.Encode())) // URL-encoded payload
if err != nil {
return err
}
// Set necessary headers
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.SetBasicAuth(s.user, s.token)
if disableSMS != "" {
log.Info("SMS sending disabled. Unset IMPF_DISABLE_SMS to enable")
return nil
}
// Execute the request
res, err := client.Do(r)
if err != nil {
return err
}
// Print the result status code
log.Debug(res.Status)
// Read the body of the response for logging
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
log.Debug(string(body))
return nil
}
// SendMessageOnboarding sends the onboarding message, notifying a person that
// his/her number has been added to the application
func (s TwillioSender) SendMessageOnboarding(toPhone string) error {
msg := "Willkommen bei der kurzfristigen Impfterminvergabe der Feuerwehr Duisburg. Möchten Sie diesen Service nicht benutzen, antworten Sie jederzeit mit \"LÖSCHEN\"."
return s.SendMessage(toPhone, msg)
}
// SendMessageNotify send the invitation message when the person is invited to a call
func (s TwillioSender) SendMessageNotify(toPhone, start, end, locName, locStreet, locHouseNr, locPLZ, locCity, locOpt string) error {
msg := fmt.Sprintf(
// TODO make this a template instead of interpolating the string with vars manually
`Sie haben die Möglichkeit zur Corona-Impfung, heute %s-%sh in %s %s %s %s %s %s. Antworten Sie für Zusage mit "JA"`,
start, end, locName, locStreet, locHouseNr, locPLZ, locCity, locOpt,
)
return s.SendMessage(toPhone, msg)
}
// SendMessageReject sends the rejection message when the person tries to
// accept a call but it is already wull
func (s TwillioSender) SendMessageReject(toPhone string) error {
msg := "Leider wurden zwischenzeitlich schon alle Termine vergeben. Sie bleiben im System und werden ggf. wieder benachrichtigt."
return s.SendMessage(toPhone, msg)
}
// SendMessageAccept sends the acceptance message when a person replies to a
// call in time and has been given a spot
func (s TwillioSender) SendMessageAccept(toPhone, start, end, locName, locStreet, locHouseNr, locPLZ, locCity, locOpt, otp string) error {
// msg := "Termin bestätigt. " + locaName + ", heute " + start + "-" + end + "h . ID: " + otp + ". Falls Sie den Termin nicht wahrnehmen können, bitte \"STORNO\" antworten."
// TODO make this a template instead of interpolating the string with vars manually
msg := fmt.Sprintf(
`Termin bestätigt %s-%sh in %s %s %s %s %s %s. Falls Sie den Termin nicht wahrnehmen können, bitte \"STORNO\" antworten. Ihre ID ist: %v`,
start, end, locName, locStreet, locHouseNr, locPLZ, locCity, locOpt, otp,
)
return s.SendMessage(toPhone, msg)
}
// SendMessageDelete notifies a person that their number has been deleted from
// the database
func (s TwillioSender) SendMessageDelete(toPhone string) error {
msg := "Sie wurden erfolgreich entfernt und erhalten keine weiteren Nachrichten von uns."
return s.SendMessage(toPhone, msg)
}
// NewTwillioSender creates a new instance of the sender from the parameters
// passed
func NewTwillioSender(endpoint, user, token, from string) *TwillioSender {
return &TwillioSender{
endpoint: endpoint,
token: token,
user: user,
from: from,
}
}