[REFACTOR] webhook telegram endpoint

This commit is contained in:
oliverpool 2024-03-21 13:59:24 +01:00
parent 9dff719523
commit 46b71ec709
4 changed files with 22 additions and 43 deletions

View file

@ -292,20 +292,6 @@ func (f *NewGogshookForm) Validate(req *http.Request, errs binding.Errors) bindi
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
// NewTelegramHookForm form for creating telegram hook
type NewTelegramHookForm struct {
BotToken string `binding:"Required"`
ChatID string `binding:"Required"`
ThreadID string
WebhookForm
}
// Validate validates the fields
func (f *NewTelegramHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
// NewMSTeamsHookForm form for creating MS Teams hook
type NewMSTeamsHookForm struct {
PayloadURL string `binding:"Required;ValidUrl"`

View file

@ -7,6 +7,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
webhook_model "code.gitea.io/gitea/models/webhook"
@ -15,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs"
webhook_module "code.gitea.io/gitea/modules/webhook"
"code.gitea.io/gitea/services/forms"
)
type telegramHandler struct{}
@ -22,7 +24,26 @@ type telegramHandler struct{}
func (telegramHandler) Type() webhook_module.HookType { return webhook_module.TELEGRAM }
func (telegramHandler) FormFields(bind func(any)) FormFields {
panic("TODO")
var form struct {
forms.WebhookForm
BotToken string `binding:"Required"`
ChatID string `binding:"Required"`
ThreadID string
}
bind(&form)
return FormFields{
WebhookForm: form.WebhookForm,
URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&message_thread_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID), url.QueryEscape(form.ThreadID)),
ContentType: webhook_model.ContentTypeJSON,
Secret: "",
HTTPMethod: http.MethodPost,
Metadata: &TelegramMeta{
BotToken: form.BotToken,
ChatID: form.ChatID,
ThreadID: form.ThreadID,
},
}
}
type (