mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-05-06 05:38:50 +00:00
feat: add native Telegram notification provider
This commit is contained in:
+36
-4
@@ -36,17 +36,18 @@ const (
|
|||||||
_ = iota
|
_ = iota
|
||||||
NotificationTypeWebhook
|
NotificationTypeWebhook
|
||||||
NotificationTypeSMTP
|
NotificationTypeSMTP
|
||||||
|
NotificationTypeTelegram
|
||||||
)
|
)
|
||||||
|
|
||||||
type Notification struct {
|
type Notification struct {
|
||||||
Common
|
Common
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Type uint8 `json:"type"` // 0: Webhook, 1: SMTP
|
Type uint8 `json:"type"` // 1: Webhook, 2: SMTP, 3: Telegram
|
||||||
URL string `json:"url"` // SMTP: host:port, Webhook: url
|
URL string `json:"url"` // SMTP: host:port, Webhook: url, Telegram: bot_token
|
||||||
RequestMethod uint8 `json:"request_method"`
|
RequestMethod uint8 `json:"request_method"`
|
||||||
RequestType uint8 `json:"request_type"`
|
RequestType uint8 `json:"request_type"`
|
||||||
RequestHeader string `json:"request_header" gorm:"type:longtext"` // SMTP: user, Webhook: header
|
RequestHeader string `json:"request_header" gorm:"type:longtext"` // SMTP: user:pass, Webhook: header, Telegram: chat_id
|
||||||
RequestBody string `json:"request_body" gorm:"type:longtext"` // SMTP: pass, Webhook: body
|
RequestBody string `json:"request_body" gorm:"type:longtext"` // SMTP: recipient, Webhook: body, Telegram: (ignored)
|
||||||
VerifyTLS *bool `json:"verify_tls,omitempty"`
|
VerifyTLS *bool `json:"verify_tls,omitempty"`
|
||||||
FormatMetricUnits *bool `json:"format_metric_units,omitempty"`
|
FormatMetricUnits *bool `json:"format_metric_units,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -123,6 +124,9 @@ func (ns *NotificationServerBundle) Send(message string) error {
|
|||||||
if n.Type == NotificationTypeSMTP {
|
if n.Type == NotificationTypeSMTP {
|
||||||
return ns.sendSMTP(message)
|
return ns.sendSMTP(message)
|
||||||
}
|
}
|
||||||
|
if n.Type == NotificationTypeTelegram {
|
||||||
|
return ns.sendTelegram(message)
|
||||||
|
}
|
||||||
|
|
||||||
var client *http.Client
|
var client *http.Client
|
||||||
if n.VerifyTLS != nil && *n.VerifyTLS {
|
if n.VerifyTLS != nil && *n.VerifyTLS {
|
||||||
@@ -204,6 +208,34 @@ func (ns *NotificationServerBundle) sendSMTP(message string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ns *NotificationServerBundle) sendTelegram(message string) error {
|
||||||
|
n := ns.Notification
|
||||||
|
// URL: bot_token
|
||||||
|
// RequestHeader: chat_id
|
||||||
|
token := n.URL
|
||||||
|
chatID := n.RequestHeader
|
||||||
|
|
||||||
|
apiURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", token)
|
||||||
|
|
||||||
|
params := url.Values{}
|
||||||
|
params.Add("chat_id", chatID)
|
||||||
|
params.Add("text", message)
|
||||||
|
params.Add("parse_mode", "HTML")
|
||||||
|
|
||||||
|
resp, err := http.PostForm(apiURL, params)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
return fmt.Errorf("Telegram API Error (%d): %s", resp.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// replaceParamInString 替换字符串中的占位符
|
// replaceParamInString 替换字符串中的占位符
|
||||||
func (ns *NotificationServerBundle) replaceParamsInString(str string, message string, mod func(string) string) string {
|
func (ns *NotificationServerBundle) replaceParamsInString(str string, message string, mod func(string) string) string {
|
||||||
if mod == nil {
|
if mod == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user