@@ -4,32 +4,32 @@ import (
44 "bytes"
55 "encoding/json"
66 "io"
7- "fmt "
7+ "log "
88 "net/http"
99 "net/http/httputil"
1010 "os"
1111 "time"
1212
13- "github.com/gotify/plugin-api"
13+ "github.com/gotify/plugin-api"
1414 "github.com/gorilla/websocket"
1515)
1616
1717// GetGotifyPluginInfo returns gotify plugin info
1818func GetGotifyPluginInfo () plugin.Info {
19- return plugin.Info {
20- Version : "1.0" ,
21- Author : "Anh Bui" ,
22- Name : "Gotify 2 Telegram" ,
23- Description : "Telegram message fowarder for gotify" ,
24- ModulePath : "https://github.com/anhbh310/gotify2telegram" ,
25-
26- }
19+ return plugin.Info {
20+ Version : "1.0" ,
21+ Author : "Anh Bui" ,
22+ Name : "Gotify 2 Telegram" ,
23+ Description : "Telegram message fowarder for gotify" ,
24+ ModulePath : "https://github.com/anhbh310/gotify2telegram" ,
25+ }
2726}
2827
2928// Plugin is the plugin instance
3029type Plugin struct {
3130 ws * websocket.Conn ;
3231 msgHandler plugin.MessageHandler ;
32+ debugLogger * log.Logger ;
3333 chatid string ;
3434 telegram_bot_token string ;
3535 gotify_host string ;
@@ -45,8 +45,8 @@ type GotifyMessage struct {
4545}
4646
4747type Payload struct {
48- ChatID string `json:"chat_id"`
49- Text string `json:"text"`
48+ ChatID string `json:"chat_id"`
49+ Text string `json:"text"`
5050}
5151
5252func (p * Plugin ) send_msg_to_telegram (msg string ) {
@@ -67,7 +67,7 @@ func (p *Plugin) send_msg_to_telegram(msg string) {
6767 }
6868 payloadBytes , err := json .Marshal (data )
6969 if err != nil {
70- fmt .Println ("Create json false" )
70+ p . debugLogger .Println ("Create json false" )
7171 return
7272 }
7373 body := bytes .NewBuffer (payloadBytes )
@@ -76,34 +76,34 @@ func (p *Plugin) send_msg_to_telegram(msg string) {
7676
7777 req , err := http .NewRequest ("POST" , "https://api.telegram.org/bot" + p .telegram_bot_token + "/sendMessage" , body )
7878 if err != nil {
79- fmt .Println ("Create request false" )
79+ p . debugLogger .Println ("Create request false" )
8080 return
8181 }
8282 req .Header .Set ("Content-Type" , "application/json" )
8383
8484 resp , err := http .DefaultClient .Do (req )
8585
8686 if err != nil {
87- fmt .Printf ("Send request false: %v\n " , err )
87+ p . debugLogger .Printf ("Send request false: %v\n " , err )
8888 return
8989 }
90- fmt .Println ("HTTP request was sent successfully" )
90+ p . debugLogger .Println ("HTTP request was sent successfully" )
9191
9292 if resp .StatusCode == http .StatusOK {
93- fmt .Println ("The message was forwarded successfully to Telegram" )
93+ p . debugLogger .Println ("The message was forwarded successfully to Telegram" )
9494 } else {
9595 // Log infor for debugging
96- fmt .Println ("============== Request ==============" )
96+ p . debugLogger .Println ("============== Request ==============" )
9797 pretty_print , err := httputil .DumpRequest (req , true )
9898 if err != nil {
99- fmt .Printf ("%v\n " , err )
99+ p . debugLogger .Printf ("%v\n " , err )
100100 }
101- fmt .Printf (string (pretty_print ))
102- fmt .Printf ("%v\n " , backup_body )
101+ p . debugLogger .Printf (string (pretty_print ))
102+ p . debugLogger .Printf ("%v\n " , backup_body )
103103
104- fmt .Println ("============== Response ==============" )
104+ p . debugLogger .Println ("============== Response ==============" )
105105 bodyBytes , err := io .ReadAll (resp .Body )
106- fmt .Printf ("%v\n " , string (bodyBytes ))
106+ p . debugLogger .Printf ("%v\n " , string (bodyBytes ))
107107
108108 }
109109
@@ -118,18 +118,18 @@ func (p *Plugin) connect_websocket() {
118118 p .ws = ws
119119 break
120120 }
121- fmt .Printf ("Cannot connect to websocket: %v\n " , err )
121+ p . debugLogger .Printf ("Cannot connect to websocket: %v\n " , err )
122122 time .Sleep (5 )
123123 }
124- fmt .Println ("WebSocket connected successfully, ready for forwarding" )
124+ p . debugLogger .Println ("WebSocket connected successfully, ready for forwarding" )
125125}
126126
127127func (p * Plugin ) get_websocket_msg (url string , token string ) {
128128 p .gotify_host = url + "/stream?token=" + token
129129 p .chatid = os .Getenv ("TELEGRAM_CHAT_ID" )
130- fmt .Printf ("chatid: %v\n " , p .chatid )
130+ p . debugLogger .Printf ("chatid: %v\n " , p .chatid )
131131 p .telegram_bot_token = os .Getenv ("TELEGRAM_BOT_TOKEN" )
132- fmt .Printf ("Bot token: %v\n " , p .telegram_bot_token )
132+ p . debugLogger .Printf ("Bot token: %v\n " , p .telegram_bot_token )
133133
134134 go p .connect_websocket ()
135135
@@ -141,7 +141,7 @@ func (p *Plugin) get_websocket_msg(url string, token string) {
141141 }
142142 err := p .ws .ReadJSON (msg )
143143 if err != nil {
144- fmt .Printf ("Error while reading websocket: %v\n " , err )
144+ p . debugLogger .Printf ("Error while reading websocket: %v\n " , err )
145145 p .connect_websocket ()
146146 continue
147147 }
@@ -152,6 +152,7 @@ func (p *Plugin) get_websocket_msg(url string, token string) {
152152// SetMessageHandler implements plugin.Messenger
153153// Invoked during initialization
154154func (p * Plugin ) SetMessageHandler (h plugin.MessageHandler ) {
155+ p .debugLogger = log .New (os .Stdout , "Gotify 2 Telegram: " , log .Lshortfile )
155156 p .msgHandler = h
156157}
157158
0 commit comments