-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
42 lines (37 loc) · 1.01 KB
/
service.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
//
// @Author: Geoffrey Bauduin <bauduin.geo@gmail.com>
//
package twiliosms
import (
"net/http"
"net/url"
"strings"
)
type Service struct {
accountID string
authToken string
phonenumber string
}
func NewService (accountID string, authToken string, phonenumber string) *Service {
return &Service{
accountID: accountID,
authToken: authToken,
phonenumber: phonenumber,
}
}
func (this *Service) Send (phonenumber string, content string) (*http.Response, error) {
values := url.Values{}
values.Set("To", phonenumber)
values.Set("From", this.phonenumber)
values.Set("Body", content)
urlValues := strings.NewReader(values.Encode())
client := &http.Client{}
req, err := http.NewRequest("POST", "https://api.twilio.com/2010-04-01/Accounts/" + this.accountID + "/Messages.json", urlValues)
if err != nil {
return nil, err
}
req.SetBasicAuth(this.accountID, this.authToken)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
return client.Do(req)
}