-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio.go
49 lines (40 loc) · 1.18 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
package sms
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"github.com/ahmad-khatib0/go/websockets/vigilate/internal/config"
)
func SendTextTwilio(to, msg string, app *config.AppConfig) error {
secret := app.PreferenceMap["twilio_auth_token"]
key := app.PreferenceMap["twilio_sid"]
urlStr := fmt.Sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json", key)
msgData := url.Values{}
msgData.Set("To", to)
msgData.Set("From", app.PreferenceMap["twilio_phone_number"])
msgData.Set("Body", msg)
msgDataReader := *strings.NewReader(msgData.Encode())
client := &http.Client{}
req, _ := http.NewRequest("POST", urlStr, &msgDataReader)
req.SetBasicAuth(key, secret)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
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 {
log.Println(err)
return err
}
} else {
log.Println("Error sending SMS!")
return errors.New("error sending SMS! Wrong response code")
}
return nil
}