-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
126 lines (103 loc) · 2.65 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/araddon/dateparse"
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/carddav"
)
type Payload struct {
Text string `json:"text"`
Username string `json:"username"`
IconUrl string `json:"icon_url"`
}
func main() {
btvDebug := os.Getenv("BTV_DEBUG")
server := os.Getenv("WEBDAV_SERVER")
addressBook := os.Getenv("WEBDAV_ADRESSBOOK")
username := os.Getenv("WEBDAV_USERNAME")
password := os.Getenv("WEBDAV_PASSWORD")
webhookUrl := os.Getenv("WEBHOOK_URL")
botName := os.Getenv("BOT_NAME")
iconUrl := os.Getenv("ICON_URL")
if btvDebug != "" {
fmt.Println("WEBDAV_SERVER", server)
fmt.Println("WEBDAV_ADDRESSBOOK", addressBook)
fmt.Println("WEBDAV_USERNAME", username)
fmt.Println("WEBDAV_PASSWORD", password)
fmt.Println("WEEB_HOOK", webhookUrl)
fmt.Println("BOT_NAME", botName)
fmt.Println("ICON_URL", iconUrl)
}
var webDavClient webdav.HTTPClient = http.DefaultClient
if username != "" {
webDavClient = webdav.HTTPClientWithBasicAuth(webDavClient, username, password)
}
cardDavClient, err := carddav.NewClient(webDavClient, server)
if err != nil {
log.Fatal(err.Error())
}
res, err := cardDavClient.QueryAddressBook(addressBook, &carddav.AddressBookQuery{
DataRequest: carddav.AddressDataRequest{
AllProp: true,
},
})
if err != nil {
log.Fatal(err.Error())
}
for _, v := range res {
name := v.Card.Value("FN")
birthDay := v.Card.Value("BDAY")
if birthDay == "" {
continue
}
t, err := dateparse.ParseLocal(birthDay)
if err != nil {
fmt.Println("Error while parsing date :", err)
}
now := time.Now()
if now.Day() == t.Day() && now.Month() == t.Month() {
message := ""
var defaultYear int = 1604
if now.Year() == defaultYear {
message = name
} else {
diffYears := fmt.Sprintf("%d", now.Year()-t.Year())
message = name + " (" + diffYears + ")"
}
payload := &Payload{
Text: message,
Username: botName,
IconUrl: iconUrl,
}
if btvDebug != "" {
fmt.Printf("%#v\n", payload)
fmt.Printf("%s\n", message)
}
dataBytes := new(bytes.Buffer)
err := json.NewEncoder(dataBytes).Encode(payload)
if err != nil {
log.Fatalln(err)
}
payloadMarshal, err := json.Marshal(payload)
if err != nil {
log.Fatalln(err)
}
resp, err := http.Post(webhookUrl, "application/json", bytes.NewBuffer(payloadMarshal))
if err != nil {
log.Fatalln(err)
}
s, _ := ioutil.ReadAll(resp.Body)
if btvDebug != "" {
fmt.Println("STATUS CODE", resp.StatusCode)
fmt.Println("STATUS CODE", string(s))
}
}
}
}