~gjabell/mfn

ref: 60b7af8cf04d70202fe33f731633968f971180ee mfn/webhook.go -rw-r--r-- 1.4 KiB
60b7af8cGalen Abell Refactor to allow multiple notification targets 1 year, 7 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
63
64
65
66
67
68
69
70
71
package main

import (
	"bytes"
	"encoding/json"
	"html/template"
	"io/ioutil"
	"log"
	"net/http"
	"time"

	mf "miniflux.app/client"
)

type Webhook struct {
	// Notifier
	template *template.Template
	url      string
}

func NewWebhook(conf *Config) *Webhook {
	return &Webhook{
		template: conf.WebhookTemplate,
		url:      conf.WebhookURL,
	}
}

func (w *Webhook) Notify(entry *mf.Entry) error {
	var text bytes.Buffer
	if err := w.template.Execute(&text, entry); err != nil {
		return err
	}

	body := map[string]string{"text": text.String(), "format": "html", "displayName": "Miniflux", "avatarUrl": "https://miniflux.app/favicon.ico"}
	jsonVal, err := json.Marshal(body)
	if err != nil {
		return err
	}

	res, err := http.Post(w.url, "application/json", bytes.NewBuffer(jsonVal))
	if err != nil {
		return err
	}
	resBytes, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil
	}
	log.Printf("Webhook response: %s", string(resBytes))

	return nil
}

func (w *Webhook) Test() error {
	e := &mf.Entry{
		ID:         0,
		UserID:     0,
		FeedID:     0,
		Status:     "unread",
		Hash:       "deadbeef",
		Title:      "Test Entry",
		URL:        "https://miniflux.app",
		Date:       time.Now(),
		Content:    "This is a test miniflux entry",
		Author:     "Author",
		Starred:    false,
		Enclosures: make([]*mf.Enclosure, 0),
		Feed:       nil,
	}

	return w.Notify(e)
}