Skip to content

Commit e3da2e0

Browse files
committed
feat: pin latest message
1 parent fe1a0c9 commit e3da2e0

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

.github/workflows/docker.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ jobs:
1717
with:
1818
docker_registry: ghcr.io
1919
docker_username: ${{ github.actor }}
20-
docker_password: ${{ secrets.GITHUB_TOKEN }}
21-
docker_push_webhook: ${{ secrets.DOCKER_PUSH_WEBHOOK }}
20+
docker_password: ${{ secrets.GITHUB_TOKEN }}
2221
backup_registry: ${{ secrets.BACKUP_REGISTRY }}
2322
backup_username: ${{ secrets.BACKUP_USERNAME }}
2423
backup_password: ${{ secrets.BACKUP_PASSWORD }}
25-
backup_push_webhook: ${{ secrets.BACKUP_PUSH_WEBHOOK }}

main.go

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,89 @@ func sendPVEStatusToTelegram(text string, temp float64, conf *Config) error {
113113
}
114114
jsonData, err := json.Marshal(data)
115115
if err != nil {
116-
return fmt.Errorf("failed to serialize JSON data: %w", err)
116+
return err
117117
}
118118

119119
req, err := http.NewRequest("POST", fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", conf.Token), bytes.NewBuffer(jsonData))
120120
if err != nil {
121-
return fmt.Errorf("failed to create request: %w", err)
121+
return err
122122
}
123123
req.Header.Set("Content-Type", "application/json")
124124

125125
resp, err := http.DefaultClient.Do(req)
126126
if err != nil {
127-
return fmt.Errorf("failed to send request: %w", err)
127+
return err
128128
}
129129
defer resp.Body.Close()
130130

131131
if resp.StatusCode != http.StatusOK {
132132
return fmt.Errorf("request failed with status code: %s", resp.Status)
133133
}
134134

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+
135150
log.Println("sendPVEStatusToTelegram: true")
136151
return nil
137152
}
138153

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+
139199
func loadConfig(path string) (*Config, error) {
140200
if strings.HasPrefix(path, "http") {
141201
resp, err := http.Get(path)
@@ -150,12 +210,12 @@ func loadConfig(path string) (*Config, error) {
150210
}
151211
return config, nil
152212
} else {
153-
bytes, err := os.ReadFile(path)
213+
file, err := os.ReadFile(path)
154214
if err != nil {
155215
log.Fatal(err)
156216
}
157217
config := &Config{}
158-
err = json.Unmarshal(bytes, config)
218+
err = json.Unmarshal(file, config)
159219
if err != nil {
160220
return nil, err
161221
}

0 commit comments

Comments
 (0)