-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Please add the functionality to send alerts to Telegram threads. #3115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -36,6 +36,7 @@ type alertTask struct { | |||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
type alertChat struct { | ||||||||||||||||||||||||||||||||||||||
ID string | ||||||||||||||||||||||||||||||||||||||
ThreadID string // ID темы (опционально) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
func (t *TaskRunner) sendMailAlert() { | ||||||||||||||||||||||||||||||||||||||
|
@@ -110,78 +111,163 @@ func (t *TaskRunner) sendMailAlert() { | |||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
func (t *TaskRunner) sendTelegramAlert() { | ||||||||||||||||||||||||||||||||||||||
if !util.Config.TelegramAlert || !t.alert { | ||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if t.Template.SuppressSuccessAlerts && t.Task.Status == task_logger.TaskSuccessStatus { | ||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
//func (t *TaskRunner) sendTelegramAlert() { | ||||||||||||||||||||||||||||||||||||||
// if !util.Config.TelegramAlert || !t.alert { | ||||||||||||||||||||||||||||||||||||||
// return | ||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// if t.Template.SuppressSuccessAlerts && t.Task.Status == task_logger.TaskSuccessStatus { | ||||||||||||||||||||||||||||||||||||||
// return | ||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// chatID := util.Config.TelegramChat | ||||||||||||||||||||||||||||||||||||||
// if t.alertChat != nil && *t.alertChat != "" { | ||||||||||||||||||||||||||||||||||||||
// chatID = *t.alertChat | ||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// if chatID == "" { | ||||||||||||||||||||||||||||||||||||||
// return | ||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// body := bytes.NewBufferString("") | ||||||||||||||||||||||||||||||||||||||
// author, version := t.alertInfos() | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// alert := Alert{ | ||||||||||||||||||||||||||||||||||||||
// Name: t.Template.Name, | ||||||||||||||||||||||||||||||||||||||
// Author: author, | ||||||||||||||||||||||||||||||||||||||
// Color: t.alertColor("telegram"), | ||||||||||||||||||||||||||||||||||||||
// Task: alertTask{ | ||||||||||||||||||||||||||||||||||||||
// ID: strconv.Itoa(t.Task.ID), | ||||||||||||||||||||||||||||||||||||||
// URL: t.taskLink(), | ||||||||||||||||||||||||||||||||||||||
// Result: t.Task.Status.Format(), | ||||||||||||||||||||||||||||||||||||||
// Version: version, | ||||||||||||||||||||||||||||||||||||||
// Desc: t.Task.Message, | ||||||||||||||||||||||||||||||||||||||
// }, | ||||||||||||||||||||||||||||||||||||||
// Chat: alertChat{ | ||||||||||||||||||||||||||||||||||||||
// ID: chatID, | ||||||||||||||||||||||||||||||||||||||
// }, | ||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// tpl, err := template.ParseFS(templates, "templates/telegram.tmpl") | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// if err != nil { | ||||||||||||||||||||||||||||||||||||||
// t.Log("Can't parse telegram alert template!") | ||||||||||||||||||||||||||||||||||||||
// panic(err) | ||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// if err := tpl.Execute(body, alert); err != nil { | ||||||||||||||||||||||||||||||||||||||
// t.Log("Can't generate telegram alert template!") | ||||||||||||||||||||||||||||||||||||||
// panic(err) | ||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// if body.Len() == 0 { | ||||||||||||||||||||||||||||||||||||||
// t.Log("Buffer for telegram alert is empty") | ||||||||||||||||||||||||||||||||||||||
// return | ||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// t.Log("Attempting to send telegram alert") | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// resp, err := http.Post( | ||||||||||||||||||||||||||||||||||||||
// fmt.Sprintf( | ||||||||||||||||||||||||||||||||||||||
// "https://api.telegram.org/bot%s/sendMessage", | ||||||||||||||||||||||||||||||||||||||
// util.Config.TelegramToken, | ||||||||||||||||||||||||||||||||||||||
// ), | ||||||||||||||||||||||||||||||||||||||
// "application/json", | ||||||||||||||||||||||||||||||||||||||
// body, | ||||||||||||||||||||||||||||||||||||||
// ) | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// if err != nil { | ||||||||||||||||||||||||||||||||||||||
// t.Log("Can't send telegram alert! Error: " + err.Error()) | ||||||||||||||||||||||||||||||||||||||
// } else if resp.StatusCode != 200 { | ||||||||||||||||||||||||||||||||||||||
// t.Log("Can't send telegram alert! Response code: " + strconv.Itoa(resp.StatusCode)) | ||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||
// t.Log("Sent successfully telegram alert") | ||||||||||||||||||||||||||||||||||||||
//} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
chatID := util.Config.TelegramChat | ||||||||||||||||||||||||||||||||||||||
if t.alertChat != nil && *t.alertChat != "" { | ||||||||||||||||||||||||||||||||||||||
chatID = *t.alertChat | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if chatID == "" { | ||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
body := bytes.NewBufferString("") | ||||||||||||||||||||||||||||||||||||||
author, version := t.alertInfos() | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
alert := Alert{ | ||||||||||||||||||||||||||||||||||||||
Name: t.Template.Name, | ||||||||||||||||||||||||||||||||||||||
Author: author, | ||||||||||||||||||||||||||||||||||||||
Color: t.alertColor("telegram"), | ||||||||||||||||||||||||||||||||||||||
Task: alertTask{ | ||||||||||||||||||||||||||||||||||||||
ID: strconv.Itoa(t.Task.ID), | ||||||||||||||||||||||||||||||||||||||
URL: t.taskLink(), | ||||||||||||||||||||||||||||||||||||||
Result: t.Task.Status.Format(), | ||||||||||||||||||||||||||||||||||||||
Version: version, | ||||||||||||||||||||||||||||||||||||||
Desc: t.Task.Message, | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
Chat: alertChat{ | ||||||||||||||||||||||||||||||||||||||
ID: chatID, | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
tpl, err := template.ParseFS(templates, "templates/telegram.tmpl") | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||
t.Log("Can't parse telegram alert template!") | ||||||||||||||||||||||||||||||||||||||
panic(err) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if err := tpl.Execute(body, alert); err != nil { | ||||||||||||||||||||||||||||||||||||||
t.Log("Can't generate telegram alert template!") | ||||||||||||||||||||||||||||||||||||||
panic(err) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if body.Len() == 0 { | ||||||||||||||||||||||||||||||||||||||
t.Log("Buffer for telegram alert is empty") | ||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
t.Log("Attempting to send telegram alert") | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
resp, err := http.Post( | ||||||||||||||||||||||||||||||||||||||
fmt.Sprintf( | ||||||||||||||||||||||||||||||||||||||
"https://api.telegram.org/bot%s/sendMessage", | ||||||||||||||||||||||||||||||||||||||
util.Config.TelegramToken, | ||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||
"application/json", | ||||||||||||||||||||||||||||||||||||||
body, | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||
t.Log("Can't send telegram alert! Error: " + err.Error()) | ||||||||||||||||||||||||||||||||||||||
} else if resp.StatusCode != 200 { | ||||||||||||||||||||||||||||||||||||||
t.Log("Can't send telegram alert! Response code: " + strconv.Itoa(resp.StatusCode)) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
t.Log("Sent successfully telegram alert") | ||||||||||||||||||||||||||||||||||||||
func (t *TaskRunner) sendTelegramAlert() { | ||||||||||||||||||||||||||||||||||||||
if !util.Config.TelegramAlert || !t.alert { | ||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if t.Template.SuppressSuccessAlerts && t.Task.Status == task_logger.TaskSuccessStatus { | ||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
chatID := util.Config.TelegramChat | ||||||||||||||||||||||||||||||||||||||
threadID := util.Config.TelegramThreadID // Новый параметр конфига | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if t.alertChat != nil && *t.alertChat != "" { | ||||||||||||||||||||||||||||||||||||||
chatID = *t.alertChat | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if chatID == "" { | ||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
body := bytes.NewBufferString("") | ||||||||||||||||||||||||||||||||||||||
author, version := t.alertInfos() | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
alert := Alert{ | ||||||||||||||||||||||||||||||||||||||
Name: t.Template.Name, | ||||||||||||||||||||||||||||||||||||||
Author: author, | ||||||||||||||||||||||||||||||||||||||
Color: t.alertColor("telegram"), | ||||||||||||||||||||||||||||||||||||||
Task: alertTask{ | ||||||||||||||||||||||||||||||||||||||
ID: strconv.Itoa(t.Task.ID), | ||||||||||||||||||||||||||||||||||||||
URL: t.taskLink(), | ||||||||||||||||||||||||||||||||||||||
Result: t.Task.Status.Format(), | ||||||||||||||||||||||||||||||||||||||
Version: version, | ||||||||||||||||||||||||||||||||||||||
Desc: t.Task.Message, | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
Chat: alertChat{ | ||||||||||||||||||||||||||||||||||||||
ID: chatID, | ||||||||||||||||||||||||||||||||||||||
ThreadID: threadID, // Добавляем ID темы | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
tpl, err := template.ParseFS(templates, "templates/telegram.tmpl") | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||
t.Log("Can't parse telegram alert template!") | ||||||||||||||||||||||||||||||||||||||
panic(err) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if err := tpl.Execute(body, alert); err != nil { | ||||||||||||||||||||||||||||||||||||||
t.Log("Can't generate telegram alert template!") | ||||||||||||||||||||||||||||||||||||||
panic(err) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if body.Len() == 0 { | ||||||||||||||||||||||||||||||||||||||
t.Log("Buffer for telegram alert is empty") | ||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
t.Log("Attempting to send telegram alert") | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Формируем базовый URL | ||||||||||||||||||||||||||||||||||||||
apiUrl := fmt.Sprintf( | ||||||||||||||||||||||||||||||||||||||
"https://api.telegram.org/bot%s/sendMessage", | ||||||||||||||||||||||||||||||||||||||
util.Config.TelegramToken, | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Если указан threadID, добавляем его как параметр запроса | ||||||||||||||||||||||||||||||||||||||
if threadID != "" { | ||||||||||||||||||||||||||||||||||||||
apiUrl += fmt.Sprintf("?message_thread_id=%s", threadID) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
resp, err := http.Post( | ||||||||||||||||||||||||||||||||||||||
apiUrl, | ||||||||||||||||||||||||||||||||||||||
"application/json", | ||||||||||||||||||||||||||||||||||||||
body, | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+253
to
+261
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Appending
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||
t.Log("Can't send telegram alert! Error: " + err.Error()) | ||||||||||||||||||||||||||||||||||||||
} else if resp.StatusCode != 200 { | ||||||||||||||||||||||||||||||||||||||
t.Log("Can't send telegram alert! Response code: " + strconv.Itoa(resp.StatusCode)) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
t.Log("Sent successfully telegram alert") | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
func (t *TaskRunner) sendSlackAlert() { | ||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"chat_id": "{{ .Chat.ID }}", | ||
{{ if .Chat.ThreadID }}"message_thread_id": "{{ .Chat.ThreadID }}",{{ end }} | ||
"parse_mode": "HTML", | ||
"text": "<code>{{ .Name }}</code>\n#{{ .Task.ID }} <b>{{ .Task.Result }}</b> <code>{{ .Task.Version }}</code> - {{ .Task.Desc }}\nby {{ .Author }}\n{{ .Task.URL }}" | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -261,6 +261,7 @@ type ConfigType struct { | |||||
TelegramAlert bool `json:"telegram_alert,omitempty" env:"SEMAPHORE_TELEGRAM_ALERT"` | ||||||
TelegramChat string `json:"telegram_chat,omitempty" env:"SEMAPHORE_TELEGRAM_CHAT"` | ||||||
TelegramToken string `json:"telegram_token,omitempty" env:"SEMAPHORE_TELEGRAM_TOKEN"` | ||||||
TelegramThreadID string `json:"telegram_thread_id" env:"SEMAPHORE_TELEGRAM_THREAD_ID"` // Новый параметр | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
SlackAlert bool `json:"slack_alert,omitempty" env:"SEMAPHORE_SLACK_ALERT"` | ||||||
SlackUrl string `json:"slack_url,omitempty" env:"SEMAPHORE_SLACK_URL"` | ||||||
RocketChatAlert bool `json:"rocketchat_alert,omitempty" env:"SEMAPHORE_ROCKETCHAT_ALERT"` | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider removing the large commented-out block of the old
sendTelegramAlert
implementation to reduce dead code and improve readability.Copilot uses AI. Check for mistakes.