mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-28 01:53:55 +00:00
feat(webhook): support Telegram Bot API 10.1 Rich Messages (#38298)
Upgrades Gitea's Telegram webhook integration to support Telegram Bot API 10.1 (June 2026 release). This enables Gitea webhooks to take advantage of rich formatted messages (tables, nested blocks, collapsible details, etc.) by routing them through the /sendRichMessage endpoint with the new rich_message payload structure. Old `/sendMessage` webhook URLs are written to `/sendRichMessage` at runtime to prevent the need for database migrations Fixes https://github.com/go-gitea/gitea/issues/38118 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
webhook_model "gitea.dev/models/webhook"
|
||||
@@ -23,9 +24,12 @@ import (
|
||||
type (
|
||||
// TelegramPayload represents
|
||||
TelegramPayload struct {
|
||||
Message string `json:"text"`
|
||||
ParseMode string `json:"parse_mode"`
|
||||
DisableWebPreview bool `json:"disable_web_page_preview"`
|
||||
RichMessage InputRichMessage `json:"rich_message"`
|
||||
}
|
||||
|
||||
// InputRichMessage represents input rich message
|
||||
InputRichMessage struct {
|
||||
HTML string `json:"html"`
|
||||
}
|
||||
|
||||
// TelegramMeta contains the telegram metadata
|
||||
@@ -195,13 +199,21 @@ func (telegramConvertor) WorkflowJob(p *api.WorkflowJobPayload) (TelegramPayload
|
||||
func createTelegramPayloadHTML(msgHTML string) TelegramPayload {
|
||||
// https://core.telegram.org/bots/api#formatting-options
|
||||
return TelegramPayload{
|
||||
Message: strings.TrimSpace(string(markup.Sanitize(msgHTML))),
|
||||
ParseMode: "HTML",
|
||||
DisableWebPreview: true,
|
||||
RichMessage: InputRichMessage{
|
||||
HTML: strings.TrimSpace(string(markup.Sanitize(msgHTML))),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newTelegramRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
|
||||
u, err := url.Parse(w.URL)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if urlPrefix, ok := strings.CutSuffix(u.Path, "/sendMessage"); ok {
|
||||
u.Path = urlPrefix + "/sendRichMessage"
|
||||
w.URL = u.String()
|
||||
}
|
||||
var pc payloadConvertor[TelegramPayload] = telegramConvertor{}
|
||||
return newJSONRequest(pc, w, t, true)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user