-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
57 lines (50 loc) · 2.04 KB
/
main.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 main
import (
"fmt"
"strings"
"math/rand"
"time"
"net/http"
"net/url"
"encoding/json"
)
func main() {
// Set account keys & information
accountSid := "ACXXXX"
authToken := "XXXXXX"
urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Messages.json"
// Create possible message bodies
quotes := [7]string{"I urge you to please notice when you are happy, and exclaim or murmur or think at some point, 'If this isn't nice, I don't know what is.'",
"Peculiar travel suggestions are dancing lessons from God.",
"There's only one rule that I know of, babies—God damn it, you've got to be kind.",
"Many people need desperately to receive this message: 'I feel and think much as you do, care about many of the things you care about, although most people do not care about them. You are not alone.'",
"That is my principal objection to life, I think: It's too easy, when alive, to make perfectly horrible mistakes.",
"So it goes.",
"We must be careful about what we pretend to be."}
// Set up rand
rand.Seed(time.Now().Unix())
// Pack up the data for our message
msgData := url.Values{}
msgData.Set("To","NUMBER_TO")
msgData.Set("From","NUMBER_FROM")
msgData.Set("Body",quotes[rand.Intn(len(quotes))])
msgDataReader := *strings.NewReader(msgData.Encode())
// Create HTTP request client
client := &http.Client{}
req, _ := http.NewRequest("POST", urlStr, &msgDataReader)
req.SetBasicAuth(accountSid, authToken)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// Make HTTP POST request and return message SID
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);
}
}