~gjabell/mfn

ref: 2e65bde5555018cd5cdfc4a704c9da335fe38668 mfn/webhook.go -rw-r--r-- 980 bytes View raw
2e65bde5Galen Abell Add email notifier 2 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
package main

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

	mf "miniflux.app/client"
)

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

func NewWebhook(conf *Config) *Webhook {
	return &Webhook{
		template: conf.WebHookConfig.Template,
		endpoint: conf.WebHookConfig.Endpoint,
	}
}

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.endpoint, "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
}