-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio.go
57 lines (47 loc) · 1.3 KB
/
twilio.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
package process
import (
"bytes"
"errors"
"net/http"
"net/url"
"os"
"strings"
)
func Message(phone string, message string) error {
// Create the data for the POST request
values := url.Values{}
values.Set("From", os.Getenv("TWILIO_PHONE"))
values.Set("Body", message)
values.Set("To", phone)
// Encode the data to be sent
encodedValues := values.Encode()
// Prepare the request
req, err := http.NewRequest("POST", "https://api.twilio.com/2010-04-01/Accounts/"+os.Getenv("TWILIO_ACCOUNT_SID")+"/Messages.json", strings.NewReader(encodedValues))
if err != nil {
return err
}
// Set headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// Set basic authentication
req.SetBasicAuth(os.Getenv("TWILIO_ACCOUNT_SID"), os.Getenv("TWILIO_AUTH_TOKEN"))
// Create an HTTP client and send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return errors.New("non-successful response status " + resp.Status + " - response body: " + buf.String())
}
return nil
}
func TruncateString(input string) string {
maxLength := 100
if len(input) > maxLength {
return input[:maxLength]
}
return input
}