~gjabell/mfn

ref: 298287072d83612d77b1acee812a011f045ac5b3 mfn/gotify.go -rw-r--r-- 1.1 KiB
29828707Galen Abell Add HTML option for email and webhook 1 year, 6 months ago
                                                                                
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
package main

import (
	"bytes"
	"net/http"
	"net/url"
	"strconv"

	mf "miniflux.app/client"
)

type Gotify struct {
	titleTemplate   *Template
	messageTemplate *Template
	server          string
	token           string
	priority        int
}

func NewGotify(conf *Config) *Gotify {
	gc := conf.GotifyConfig

	return &Gotify{
		titleTemplate:   gc.TitleTemplate,
		messageTemplate: gc.MessageTemplate,
		server:          gc.Server,
		token:           gc.Token,
		priority:        gc.Priority,
	}
}

func (g *Gotify) Notify(entry *mf.Entry) error {
	var title, message bytes.Buffer
	if err := g.titleTemplate.Execute(&title, entry); err != nil {
		return err
	}

	if err := g.messageTemplate.Execute(&message, entry); err != nil {
		return err
	}

	u, err := url.Parse(g.server)
	if err != nil {
		return err
	}

	q := u.Query()
	q.Set("token", g.token)

	u.Path = u.Path + "message"
	u.RawQuery = q.Encode()

	if _, err := http.PostForm(u.String(), url.Values{
		"title":    {title.String()},
		"message":  {message.String()},
		"priority": {strconv.Itoa(g.priority)},
	}); err != nil {
		return err
	}

	return nil
}