-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipBot.go
76 lines (56 loc) · 1.18 KB
/
ipBot.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"sync"
)
func checkIpAddressChange(c chan<- string) {
var ip string
for {
resp, err := http.Get("https://ifconfig.co")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
htmlData, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
result := string(htmlData)
// Only return the result if there has been a change in IP Address
if result != ip {
ip = result
c <- result
}
time.Sleep(1 * time.Minute)
}
}
func slackIpAddressChange(c chan string) {
// ws, _ := slackConnect(os.Args[1])
ws, _ := slackConnect("xoxb-109879420662-lyrIyOq8nsnnBcmaL8IWqOrd")
h, _ := os.Hostname()
for {
msg := <-c
fmt.Println(msg)
m := Message{Type: "message", Channel: "C3GVB1RB8", Text: h + " : " + msg}
postMessage(ws, m)
}
}
func main() {
// if len(os.Args) != 2 {
// fmt.Fprintf(os.Stderr, "usage: ipBot slack-bot-token\n")
// os.Exit(1)
// }
fmt.Println("Starting up...")
var c chan string = make(chan string)
var wg sync.WaitGroup
wg.Add(1)
go checkIpAddressChange(c)
go slackIpAddressChange(c)
wg.Wait()
}