forked from anhbh310/gotify2telegram
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.go
More file actions
152 lines (132 loc) · 3.63 KB
/
Copy pathplugin.go
File metadata and controls
152 lines (132 loc) · 3.63 KB
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
package main
import (
"fmt"
"net"
"time"
"github.com/gorilla/websocket"
"github.com/gotify/plugin-api"
)
// GetGotifyPluginInfo returns gotify plugin info
func GetGotifyPluginInfo() plugin.Info {
return plugin.Info{
Version: "2.0",
Author: "Leko",
Name: "SNS Forwarder",
Description: "Forward Gotify messages to Telegram and Discord",
ModulePath: "https://github.com/lekoOwO/gotify2telegram",
}
}
// Plugin is the plugin instance
type Plugin struct {
config *Config
ws *websocket.Conn
msgHandler plugin.MessageHandler
isEnabled bool
}
type GotifyMessage struct {
Id uint32
Appid uint32
Message string
Title string
Priority uint32
Date string
}
func (p *Plugin) connect_websocket(url string) {
for {
debug("connect_websocket:: Connecting ws.")
ws, _, err := websocket.DefaultDialer.Dial(url, nil)
if err == nil {
ws.SetCloseHandler(func(code int, text string) error {
p.ws = nil
return fmt.Errorf("WebSocket Connection Closed. Resetting plugin.ws.")
})
p.ws = ws
// Get Local Port
if tcpConn, ok := ws.UnderlyingConn().(*net.TCPConn); ok {
localAddr := tcpConn.LocalAddr().(*net.TCPAddr)
debug("connect_websocket:: Connection Success. Port: %d", localAddr.Port)
} else {
debug("connect_websocket:: Connection Success. Underlying connection is not a TCP connection")
}
break
}
fmt.Printf("Cannot connect to websocket: %v\n", err)
time.Sleep(5 * time.Second)
}
}
func (p *Plugin) get_websocket_msg(url string) {
ws_url := url + "/stream?token=" + p.config.GotifyClientToken
p.connect_websocket(ws_url) // Initial connection setup
for {
if !p.isEnabled {
break
}
if p.ws == nil {
// If WebSocket is not initialized, attempt to reconnect
time.Sleep(3 * time.Second)
p.connect_websocket(ws_url)
}
msg := &GotifyMessage{}
err := p.ws.ReadJSON(msg)
if err != nil {
fmt.Printf("Error while reading WebSocket: %v\n", err)
p.ws.Close()
continue
}
// Process the received message
for _, subClient := range p.config.Clients {
if subClient.AppId == int(msg.Appid) || subClient.AppId == -1 {
debug("get_websocket_msg: AppId Matched! Sending to telegram...")
// Send to Telegram if configured
if subClient.Telegram.BotToken != "" {
send_msg_to_telegram(
format_telegram_message(msg),
subClient.Telegram.BotToken,
subClient.Telegram.ChatId,
subClient.Telegram.ThreadId,
)
}
// Send to Discord if configured
if subClient.Discord.WebhookURL != "" {
username := subClient.Discord.Username
avatar := subClient.Discord.AvatarURL
// fallback to global defaults if empty
if username == "" && p.config != nil {
username = p.config.DiscordDefaults.Username
}
if avatar == "" && p.config != nil {
avatar = p.config.DiscordDefaults.AvatarURL
}
embeds := format_discord_embeds(msg)
send_msg_to_discord(embeds, subClient.Discord.WebhookURL, username, avatar)
}
break
}
}
}
}
// SetMessageHandler implements plugin.Messenger
// Invoked during initialization
func (p *Plugin) SetMessageHandler(h plugin.MessageHandler) {
p.msgHandler = h
}
func (p *Plugin) Enable() error {
p.isEnabled = true
go p.get_websocket_msg(p.config.GotifyHost)
return nil
}
// Disable implements plugin.Plugin
func (p *Plugin) Disable() error {
p.isEnabled = false
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")
}