-
Notifications
You must be signed in to change notification settings - Fork 0
/
dogecall.go
159 lines (130 loc) · 3.82 KB
/
dogecall.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
149
150
151
152
153
154
155
156
157
158
159
/*
dogecall.go
porting my small dogecall.py program to Go.
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/user"
"regexp"
"strings"
"github.com/hako/dogecall/Godeps/_workspace/src/github.com/docopt/docopt-go"
)
var usage = `dogecall - ENCOUNTER A DOGE THROUGH A PHONE CALL.
Usage:
dogecall [-n <number>]
dogecall [-h | --help] [-v | --version]
Options:
-n [PHONE NUMBER] such fone numer (with area codez) eg. +44 = 44
-h, --help show this help message and exit.
-v, --version the versions lol.
Example: dogecall -n [PHONE NUMBER]`
type dogecallrc struct {
Accountsid string `json:"account_sid"`
TwAuthtoken string `json:"tw_authtoken"`
TwNumber string `json:"tw_number"`
URL string `json:"url"`
}
var version = "0.5"
var twilioAPIBaseURL = "https://api.twilio.com/2010-04-01"
func main() {
if len(os.Args) < 2 {
fmt.Println(usage)
os.Exit(1)
}
args, _ := docopt.Parse(usage, nil, true, version, false)
// Load .dogecallrc
dcConfig, err := loadDogeCallRC()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if args["-n"] != nil {
phoneNumber := args["-n"].(string)
twilioNumber := dcConfig.TwNumber
// Check both phone numbers before calling.
check1 := checkNumber(phoneNumber)
if check1 != true {
fmt.Printf("wow, such number \"%s\" not valid, lol.", phoneNumber)
os.Exit(1)
}
check2 := checkNumber(twilioNumber)
if check2 != true {
fmt.Printf("wow, such number in config \"%s\" is not valid, lol.", twilioNumber)
os.Exit(1)
}
call(twilioNumber, phoneNumber)
}
}
// Checks the phone number.
func checkNumber(phoneNumber string) bool {
phoneRegex := `[(]?\d{3}[)]?\s?-?\s?\d{3}\s?-?\s?\d{4}`
result, _ := regexp.MatchString(phoneRegex, phoneNumber)
return result
}
// Load .dogecallrc.
func loadDogeCallRC() (dogecallrc, error) {
var data dogecallrc
usr, _ := user.Current()
rc, err := ioutil.ReadFile(usr.HomeDir + "/" + ".dogecallrc")
if err != nil {
fmt.Println("creating dogecallrc...")
err = createDogeCallRC(usr.HomeDir)
if err != nil {
return data, err
}
return data, errors.New("please configure .dogecallrc 2 use dogecall pls")
}
if err := json.Unmarshal(rc, &data); err != nil {
return data, errors.New("Error decoding from JSON")
}
return data, nil
}
// Create .dogecallrc if it does not exist already.
func createDogeCallRC(dir string) error {
var data dogecallrc
data.Accountsid = ""
data.TwNumber = ""
data.URL = "http://dc.hakobaito.co.uk/doge" // Default URL, feel free to change.
data.TwAuthtoken = ""
rcdata, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
ioutil.WriteFile(dir+"/"+".dogecallrc", rcdata, 0644)
return nil
}
// Make a call to the person.
func call(twilioNumber string, recievingNumber string) {
dcConfig, _ := loadDogeCallRC()
accountSid := dcConfig.Accountsid
authToken := dcConfig.TwAuthtoken
callURL := dcConfig.URL
twilioCallURL := twilioAPIBaseURL + "/Accounts/" + accountSid + "/Calls.json"
// Prepare POST values.
// Remember, the 'To' number must be a 'verified number' when using a twilio trial account.
postVals := url.Values{}
postVals.Set("To", recievingNumber)
postVals.Set("From", twilioNumber)
postVals.Set("Record", "false")
postVals.Set("Url", callURL)
data := *strings.NewReader(postVals.Encode())
fmt.Println("pls wait...")
// Create a HTTP client, prepare and send the request.
client := &http.Client{}
req, _ := http.NewRequest("POST", twilioCallURL, &data)
req.SetBasicAuth(accountSid, authToken)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, _ := client.Do(req)
if res.StatusCode != 201 {
fmt.Printf("such number \"%s\" so wrong, check again pls. sad doge.", recievingNumber)
} else {
fmt.Printf("wow. a wild shibe in 3 secs %s, pick up pls.", recievingNumber)
}
}