forked from Rhymen/go-whatsapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (77 loc) · 2.2 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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/denislee/go-whatsapp"
)
type requestBody struct {
Text string `json:"text"`
}
type version struct {
major int
minor int
patch int
}
func main() {
log.SetFlags(0)
// Place here your slack webhook url
var webhookUrl = ""
wac, err := whatsapp.NewConn(5 * time.Second)
if err != nil {
panic(err)
}
v := wac.GetClientVersion()
clientVersion := &version{major: v[0], minor: v[1], patch: v[2]}
fmt.Printf("Client has version %d.%d.%d\n", clientVersion.major, clientVersion.minor, clientVersion.patch)
v, err = whatsapp.CheckCurrentServerVersion()
serverVersion := &version{major: v[0], minor: v[1], patch: v[2]}
if err != nil {
panic(err)
}
fmt.Printf("Server has version %d.%d.%d\n", serverVersion.major, serverVersion.minor, serverVersion.patch)
var report = ""
if serverVersion.major == clientVersion.major {
if serverVersion.minor == clientVersion.minor {
if serverVersion.patch == clientVersion.patch {
report = "Versions are equal"
} else if serverVersion.patch > clientVersion.patch {
report = fmt.Sprintf("New patch detected %d", serverVersion.patch)
}
} else if serverVersion.minor > clientVersion.minor {
report = fmt.Sprintf("New minor detected %d", serverVersion.minor)
}
} else if serverVersion.major > clientVersion.major {
report = fmt.Sprintf("New major detected %d", serverVersion.major)
}
fmt.Println(report)
if err := sendWebHookNotification(webhookUrl, report); err != nil {
log.Fatalln(err)
}
}
func sendWebHookNotification(webhookUrl string, msg string) error {
if webhookUrl == "" {
return errors.New("You must provide webhookurl to send the notification")
}
slackBody, _ := json.Marshal(requestBody{Text: msg})
req, err := http.NewRequest(http.MethodPost, webhookUrl, bytes.NewBuffer(slackBody))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return err
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
if buf.String() != "ok" {
return errors.New("Non-ok response returned from Slack")
}
return nil
}