-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio.go
58 lines (48 loc) · 1.34 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
58
package main
import (
"fmt"
"strings"
"encoding/json"
"net/http"
"net/url"
)
func MsgSMS(ctx *Context, msg string) error {
cfg := ctx.Config
sid := cfg.Twilios[*ctx.ArgProfileName].Sid
token := cfg.Twilios[*ctx.ArgProfileName].Token
from := cfg.Twilios[*ctx.ArgProfileName].From
if from == "" {
from = *ctx.ArgFrom
}
to := cfg.Twilios[*ctx.ArgProfileName].To
if to == "" {
to = *ctx.ArgTo
}
fmt.Printf("To: %s From: %s\n", to, from)
fmt.Printf("SID: %s TOKEN: %s MSG: %s\n", sid, token, msg)
// Pack up the data for our message
msgData := url.Values{}
msgData.Set("To", to)
msgData.Set("From", from)
msgData.Set("Body", msg)
msgDataReader := *strings.NewReader(msgData.Encode())
urlStr := fmt.Sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json", sid)
req, _ := http.NewRequest("POST", urlStr, &msgDataReader)
req.SetBasicAuth(sid, token)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// Make HTTP POST request and return message SID
client := &http.Client{}
resp, _ := client.Do(req)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var data map[string]interface{}
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&data)
if err == nil {
fmt.Println(data["sid"])
}
} else {
fmt.Println(resp.Status)
}
return nil
}