-
Notifications
You must be signed in to change notification settings - Fork 1
/
notifications.go
50 lines (40 loc) · 942 Bytes
/
notifications.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/jangxx/go-poclient"
)
func listenForMessages(po *poclient.Client) {
for {
select {
case message := <-po.Messages:
messages[message.UniqueID] = message
if !config.cache.Exists(message.IconID + ".png") {
//download icon first
err := downloadMessageIcon(message)
if err != nil {
log.Println("Error while downloading icon: " + err.Error())
}
}
sendNotification(message)
}
}
}
func downloadMessageIcon(message poclient.Message) error {
resp, err := http.Get(getMessageIconURL(message))
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
config.cache.WriteFile(message.IconID+".png", body)
return nil
}
func getMessageIconURL(message poclient.Message) string {
return fmt.Sprintf("https://api.pushover.net/icons/%s.png", message.IconID)
}