-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (87 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bufio"
"fmt"
flag "github.com/ogier/pflag"
"os"
)
var (
message string
number string
u string
debug bool
apikey string
secret string
from string
)
const (
server string = "api.smsglobal.com"
uri string = "/v1/sms/"
glblkey string = "GLBLKEY"
glblscrt string = "GLBLSCRT"
)
func init() {
flag.BoolVarP(&debug, "debug", "d", false, "enable debug")
flag.StringVarP(&message, "message", "m", "", "the message to be sent")
flag.StringVarP(&number, "number", "n", "", "the mobile phone number to send to")
flag.StringVarP(&from, "from", "f", "smsglobal", "string identifier of who is sending the msg")
}
func main() {
flag.Parse()
if number == "" {
fmt.Printf("required option --number missing.\n")
flag.PrintDefaults()
os.Exit(1)
}
if message == "" {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
message += scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "error reading stdin:", err)
}
}
if message == "" {
fmt.Printf("no message provided.\n")
flag.PrintDefaults()
os.Exit(1)
}
apikey = os.Getenv(glblkey)
if apikey == "" {
fmt.Printf("missing environment variable %s\n", glblkey)
os.Exit(1)
}
secret = os.Getenv(glblscrt)
if secret == "" {
fmt.Printf("missing environment variable %s\n", glblscrt)
os.Exit(1)
}
u = "https://" + server + uri
if debug {
fmt.Printf("debug enabled.\n")
fmt.Printf("url: %s\n", u)
fmt.Printf("number: %s\n", number)
fmt.Printf("message: %s\n", message)
}
requ := SMSMessage{}
resu := SMSResponse{}
requ.Origin = from
requ.Destination = number
requ.Message = message
err, resp := PostMsg(u, &requ, &resu)
if err != nil {
fmt.Printf("%s : %s\n", resp.HttpResponse().Status, err)
os.Exit(1)
}
if resp.Status() != 201 {
fmt.Printf("ERR %s\n", resp.HttpResponse().Status)
printResponse(&resu)
os.Exit(resp.Status())
} else {
head := resp.HttpResponse().Header
msgid := head.Get("Location")
fmt.Printf("%s %s\n", resp.HttpResponse().Status, msgid)
}
os.Exit(0)
}