-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsms.go
More file actions
47 lines (40 loc) · 1.04 KB
/
sms.go
File metadata and controls
47 lines (40 loc) · 1.04 KB
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const (
accountSID = "TwilioSID"
authToken = "TwilioToken"
urlString = "https://api.twilio.com/2010-04-01/Accounts/accountSID/Messages.json"
)
func main() {
// Basic data for our message
v := url.Values{}
v.Set("To", "+Receiver")
v.Set("From", "+VirtualTwilioSenderNumber")
v.Set("Body", "You are pretty Welcome to put your custom SMS here!")
reqBody := *strings.NewReader(v.Encode())
// New client
client := &http.Client{}
req, _ := http.NewRequest("POST", urlString, &reqBody)
req.SetBasicAuth(accountSID, authToken)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// Make request
resp, _ := client.Do(req)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var data map[string]interface{}
bodyBytes, _ := ioutil.ReadAll(resp.Body)
err := json.Unmarshal(bodyBytes, &data)
if err == nil {
fmt.Println(data["sid"])
}
} else {
fmt.Println(resp.Status)
}
}