-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification.go
More file actions
42 lines (36 loc) · 1.02 KB
/
notification.go
File metadata and controls
42 lines (36 loc) · 1.02 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
type NotificationClient struct {
HttpClient *http.Client
WebhookUrl string
}
func NewNotificationClient(webhookUrl string) *NotificationClient {
return &NotificationClient{HttpClient: http.DefaultClient, WebhookUrl: webhookUrl}
}
type NotificationRequestBody struct {
Text string `json:"text"`
}
func (c *NotificationClient) NotifyInsertTaskError(task OismTask, err error) error {
return c.notify(fmt.Sprintf("タスク: %vをリスト: %v に追加できませんでした。Error: %v extra: %v", task.Title, task.ListName, err.Error(), strings.Join([]string{task.Notes, task.Duo}, ",")))
}
func (c *NotificationClient) notify(content string) error {
j, err := json.Marshal(NotificationRequestBody{Text: content})
if err != nil {
fmt.Println(err)
}
req, err := http.NewRequest("POST", c.WebhookUrl, bytes.NewBuffer(j))
if err != nil {
fmt.Println(err)
}
_, err = c.HttpClient.Do(req)
if err != nil {
fmt.Println(err)
}
return nil
}