-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
148 lines (134 loc) · 3.7 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/hashicorp/go-hclog"
)
const (
EnvVarTwilioAccountSID = "TWILIO_ACCOUNT_SID"
EnvVarTwilioAuthToken = "TWILIO_AUTH_TOKEN"
twilioAPIBaseURL = "https://api.twilio.com/2010-04-01"
)
var (
// Command-line flags.
pageToCheck = flag.String("url", "", `ex: "https://google.com"`)
matchPhrase = flag.String("phrase", "", `ex: "Hello World"`)
to = flag.String("to", "", `recipient's phone number, ex: "503-123-4567"`)
from = flag.String("from", "", `sender's phone number, ex: "503-123-4567", should exist in Twilio`)
frequency = flag.Int("frequency", 60, `ex: "60", how frequently in seconds the web page should be checked, defaults to 60`)
dryRun = flag.Bool("dryrun", false, `if included, indicates that notifications should be logged instead of sent'`)
// Logger for tool use.
logger = hclog.Default()
)
func main() {
flag.Parse()
twilioAccountSID := os.Getenv(EnvVarTwilioAccountSID)
if twilioAccountSID == "" {
if !*dryRun {
logger.Error(EnvVarTwilioAccountSID + " not set in environment")
os.Exit(1)
}
}
twilioAuthToken := os.Getenv(EnvVarTwilioAuthToken)
if twilioAuthToken == "" {
if !*dryRun {
logger.Error(EnvVarTwilioAuthToken + " not set in environment")
os.Exit(1)
}
}
if pageToCheck == nil || *pageToCheck == "" {
logger.Error(`"url" flag must be included`)
os.Exit(1)
}
if matchPhrase == nil || *matchPhrase == "" {
logger.Error(`"phrase" flag must be included`)
os.Exit(1)
}
if to == nil || *to == "" {
logger.Error(`"to" flag must be included`)
os.Exit(1)
}
if from == nil || *from == "" {
logger.Error(`"from" flag must be included`)
os.Exit(1)
}
checkFrequency := time.Second * time.Duration(*frequency)
for i := 0; true; i++ {
if i != 0 {
<-time.NewTimer(checkFrequency).C
}
logger.Info("checking...")
hasPhrase, err := checkPage()
if err != nil {
logger.Warn(fmt.Sprintf("could not get page: %s", err))
continue
}
if hasPhrase {
continue
}
logger.Info(fmt.Sprintf("%q no longer has %q", *pageToCheck, *matchPhrase))
if !*dryRun {
if err := notify(twilioAccountSID, twilioAuthToken); err != nil {
logger.Warn(fmt.Sprintf("could not send notification: %s", err))
continue
}
}
return
}
}
func checkPage() (bool, error) {
resp, err := http.Get(*pageToCheck)
if err != nil {
return false, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
logger.Warn(fmt.Sprintf("unable to close page response body due to %s", err))
}
}()
if resp.StatusCode != 200 {
return false, errors.New(resp.Status)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
return strings.Contains(string(b), *matchPhrase), nil
}
func notify(twilioAccountSID, twilioAuthToken string) error {
endpoint := "/Accounts/" + twilioAccountSID + "/Messages.json"
method := http.MethodPost
v := url.Values{}
v.Set("To", *to)
v.Set("From", *from)
v.Set("Body", fmt.Sprintf("%q no longer has %q", *pageToCheck, *matchPhrase))
rb := *strings.NewReader(v.Encode())
req, err := http.NewRequest(method, twilioAPIBaseURL+endpoint, &rb)
if err != nil {
return err
}
req.SetBasicAuth(twilioAccountSID, twilioAuthToken)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil {
logger.Warn(fmt.Sprintf("unable to close SMS response body due to %s", err))
}
}()
if resp.StatusCode != 201 {
b, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("%d: %s", resp.StatusCode, b)
}
return nil
}