forked from anhbh310/gotify2telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
142 lines (123 loc) · 3.34 KB
/
plugin.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/gotify/plugin-api"
"github.com/gorilla/websocket"
)
// GetGotifyPluginInfo returns gotify plugin info
func GetGotifyPluginInfo() plugin.Info {
return plugin.Info{
Version: "1.0",
Author: "Anh Bui",
Name: "Gotify 2 Telegram",
Description: "Telegram message fowarder for gotify",
ModulePath: "https://github.com/anhbh310/gotify2telegram",
}
}
// Plugin is the plugin instance
type Plugin struct {
ws *websocket.Conn;
msgHandler plugin.MessageHandler;
chatid string;
telegram_bot_token string;
gotify_host string;
}
type GotifyMessage struct {
Id uint32;
Appid uint32;
Message string;
Title string;
Priority uint32;
Date string;
}
type Payload struct {
ChatID string `json:"chat_id"`
Text string `json:"text"`
}
func (p *Plugin) send_msg_to_telegram(msg string) {
data := Payload{
// Fill struct
ChatID: p.chatid,
Text: msg,
}
payloadBytes, err := json.Marshal(data)
if err != nil {
fmt.Println("Create json false")
return
}
body := bytes.NewReader(payloadBytes)
req, err := http.NewRequest("POST", "https://api.telegram.org/bot"+ p.telegram_bot_token +"/sendMessage", body)
if err != nil {
fmt.Println("Create request false")
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("Send request false: %v\n", err)
return
}
defer resp.Body.Close()
}
func (p *Plugin) connect_websocket() {
for {
ws, _, err := websocket.DefaultDialer.Dial(p.gotify_host, nil)
if err == nil {
p.ws = ws
break
}
fmt.Printf("Cannot connect to websocket: %v\n", err)
time.Sleep(5)
}
}
func (p *Plugin) get_websocket_msg(url string, token string) {
p.gotify_host = url + "/stream?token=" + token
p.chatid = os.Getenv("TELEGRAM_CHAT_ID")
p.telegram_bot_token = os.Getenv("TELEGRAM_BOT_TOKEN")
go p.connect_websocket()
for {
msg := &GotifyMessage{}
if p.ws == nil {
time.Sleep(3)
continue
}
err := p.ws.ReadJSON(msg)
if err != nil {
fmt.Printf("Error while reading websocket: %v\n", err)
p.connect_websocket()
continue
}
p.send_msg_to_telegram(msg.Date + "\n" + msg.Title + "\n\n" + msg.Message)
}
}
// SetMessageHandler implements plugin.Messenger
// Invoked during initialization
func (p *Plugin) SetMessageHandler(h plugin.MessageHandler) {
p.msgHandler = h
}
func (p *Plugin) Enable() error {
go p.get_websocket_msg(os.Getenv("GOTIFY_HOST"), os.Getenv("GOTIFY_CLIENT_TOKEN"))
return nil
}
// Disable implements plugin.Plugin
func (p *Plugin) Disable() error {
if p.ws != nil {
p.ws.Close()
}
return nil
}
// NewGotifyPluginInstance creates a plugin instance for a user context.
func NewGotifyPluginInstance(ctx plugin.UserContext) plugin.Plugin {
return &Plugin{}
}
func main() {
panic("this should be built as go plugin")
// For testing
// p := &Plugin{nil, nil, "", "", ""}
// p.get_websocket_msg(os.Getenv("GOTIFY_HOST"), os.Getenv("GOTIFY_CLIENT_TOKEN"))
}