-
Notifications
You must be signed in to change notification settings - Fork 6
/
plugin.go
182 lines (157 loc) · 4.91 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"net/http/httputil"
"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;
debugLogger *log.Logger;
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) {
step_size := 4090
sending_message := ""
for i:=0; i<len(msg); i+=step_size {
if i+step_size < len(msg) {
sending_message = msg[i : i+step_size]
} else {
sending_message = msg[i:len(msg)]
}
data := Payload{
// Fill struct
ChatID: p.chatid,
Text: sending_message,
}
payloadBytes, err := json.Marshal(data)
if err != nil {
p.debugLogger.Println("Create json false")
return
}
body := bytes.NewBuffer(payloadBytes)
// For future debugging
backup_body := bytes.NewBuffer(body.Bytes())
req, err := http.NewRequest("POST", "https://api.telegram.org/bot"+ p.telegram_bot_token +"/sendMessage", body)
if err != nil {
p.debugLogger.Println("Create request false")
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
p.debugLogger.Printf("Send request false: %v\n", err)
return
}
p.debugLogger.Println("HTTP request was sent successfully")
if resp.StatusCode == http.StatusOK {
p.debugLogger.Println("The message was forwarded successfully to Telegram")
} else {
// Log infor for debugging
p.debugLogger.Println("============== Request ==============")
pretty_print, err := httputil.DumpRequest(req, true)
if err != nil {
p.debugLogger.Printf("%v\n", err)
}
p.debugLogger.Printf(string(pretty_print))
p.debugLogger.Printf("%v\n", backup_body)
p.debugLogger.Println("============== Response ==============")
bodyBytes, err := io.ReadAll(resp.Body)
p.debugLogger.Printf("%v\n", string(bodyBytes))
}
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
}
p.debugLogger.Printf("Cannot connect to websocket: %v\n", err)
time.Sleep(5)
}
p.debugLogger.Println("WebSocket connected successfully, ready for forwarding")
}
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.debugLogger.Printf("chatid: %v\n", p.chatid)
p.telegram_bot_token = os.Getenv("TELEGRAM_BOT_TOKEN")
p.debugLogger.Printf("Bot token: %v\n", p.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 {
p.debugLogger.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.debugLogger = log.New(os.Stdout, "Gotify 2 Telegram: ", log.Lshortfile)
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"))
}