forked from messagebird/sachet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexotel.go
64 lines (54 loc) · 1.61 KB
/
exotel.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
59
60
61
62
63
64
package sachet
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
//ExotelConfig configuration struct for exotel Client
type ExotelConfig struct {
AccountSID string `yaml:"account_sid"`
AuthToken string `yaml:"auth_token"`
}
//ExotelRequestTimeout is the timeout for http request to exotel
const ExotelRequestTimeout = time.Second * 20
//Exotel is the exte Exotel
type Exotel struct {
AccountSid string
Token string
}
//NewExotel creates a new
func NewExotel(config ExotelConfig) *Exotel {
Exotel := &Exotel{AccountSid: config.AccountSID, Token: config.AuthToken}
return Exotel
}
//Send send sms to n number of people using bulk sms api
func (c *Exotel) Send(message Message) (err error) {
smsURL := fmt.Sprintf("https://twilix.exotel.in/v1/Accounts/%s/Sms/send.json", c.AccountSid)
var request *http.Request
var resp *http.Response
form := url.Values{"From": {message.From}, "Body": {message.Text}, "To": message.To}
//preparing the request
request, err = http.NewRequest("POST", smsURL, strings.NewReader(form.Encode()))
if err != nil {
return
}
request.SetBasicAuth(c.AccountSid, c.Token)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("User-Agent", "SachetV1.0")
//calling the endpoint
httpClient := &http.Client{}
httpClient.Timeout = ExotelRequestTimeout
resp, err = httpClient.Do(request)
if err != nil {
return
}
defer resp.Body.Close()
var body []byte
resp.Body.Read(body)
if resp.StatusCode == http.StatusOK && err == nil {
return
}
return fmt.Errorf("Failed sending sms:Reason: %s , StatusCode : %d", string(body), resp.StatusCode)
}