@@ -113,29 +113,89 @@ func sendPVEStatusToTelegram(text string, temp float64, conf *Config) error {
113
113
}
114
114
jsonData , err := json .Marshal (data )
115
115
if err != nil {
116
- return fmt . Errorf ( "failed to serialize JSON data: %w" , err )
116
+ return err
117
117
}
118
118
119
119
req , err := http .NewRequest ("POST" , fmt .Sprintf ("https://api.telegram.org/bot%s/sendMessage" , conf .Token ), bytes .NewBuffer (jsonData ))
120
120
if err != nil {
121
- return fmt . Errorf ( "failed to create request: %w" , err )
121
+ return err
122
122
}
123
123
req .Header .Set ("Content-Type" , "application/json" )
124
124
125
125
resp , err := http .DefaultClient .Do (req )
126
126
if err != nil {
127
- return fmt . Errorf ( "failed to send request: %w" , err )
127
+ return err
128
128
}
129
129
defer resp .Body .Close ()
130
130
131
131
if resp .StatusCode != http .StatusOK {
132
132
return fmt .Errorf ("request failed with status code: %s" , resp .Status )
133
133
}
134
134
135
+ var respData struct {
136
+ Result struct {
137
+ MessageId int64 `json:"message_id"`
138
+ }
139
+ }
140
+ err = json .NewDecoder (resp .Body ).Decode (& respData )
141
+ if err != nil {
142
+ return err
143
+ }
144
+
145
+ err = pinMessageToTelegram (conf , respData .Result .MessageId )
146
+ if err != nil {
147
+ return err
148
+ }
149
+
135
150
log .Println ("sendPVEStatusToTelegram: true" )
136
151
return nil
137
152
}
138
153
154
+ func pinMessageToTelegram (conf * Config , messageId int64 ) error {
155
+ // unpin all messages
156
+ data := map [string ]interface {}{
157
+ "chat_id" : conf .TargetId ,
158
+ }
159
+ jsonData , err := json .Marshal (data )
160
+ if err != nil {
161
+ return err
162
+ }
163
+ req , err := http .NewRequest ("POST" , fmt .Sprintf ("https://api.telegram.org/bot%s/unpinAllChatMessages" , conf .Token ), bytes .NewBuffer (jsonData ))
164
+ if err != nil {
165
+ return err
166
+ }
167
+ req .Header .Set ("Content-Type" , "application/json" )
168
+ resp , err := http .DefaultClient .Do (req )
169
+ if err != nil {
170
+ return err
171
+ }
172
+ err = resp .Body .Close ()
173
+ if err != nil {
174
+ return err
175
+ }
176
+
177
+ // pin the message
178
+ data = map [string ]interface {}{
179
+ "chat_id" : conf .TargetId ,
180
+ "message_id" : messageId ,
181
+ "disable_notification" : true ,
182
+ }
183
+ jsonData , err = json .Marshal (data )
184
+ if err != nil {
185
+ return err
186
+ }
187
+ req , err = http .NewRequest ("POST" , fmt .Sprintf ("https://api.telegram.org/bot%s/pinChatMessage" , conf .Token ), bytes .NewBuffer (jsonData ))
188
+ if err != nil {
189
+ return err
190
+ }
191
+ req .Header .Set ("Content-Type" , "application/json" )
192
+ resp , err = http .DefaultClient .Do (req )
193
+ if err != nil {
194
+ return err
195
+ }
196
+ return resp .Body .Close ()
197
+ }
198
+
139
199
func loadConfig (path string ) (* Config , error ) {
140
200
if strings .HasPrefix (path , "http" ) {
141
201
resp , err := http .Get (path )
@@ -150,12 +210,12 @@ func loadConfig(path string) (*Config, error) {
150
210
}
151
211
return config , nil
152
212
} else {
153
- bytes , err := os .ReadFile (path )
213
+ file , err := os .ReadFile (path )
154
214
if err != nil {
155
215
log .Fatal (err )
156
216
}
157
217
config := & Config {}
158
- err = json .Unmarshal (bytes , config )
218
+ err = json .Unmarshal (file , config )
159
219
if err != nil {
160
220
return nil , err
161
221
}
0 commit comments