mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-10 04:05:42 +01:00
[REFACTOR] webhook telegram endpoint
This commit is contained in:
parent
9dff719523
commit
46b71ec709
4 changed files with 22 additions and 43 deletions
|
@ -388,32 +388,6 @@ func gogsHookParams(ctx *context.Context) webhookParams {
|
|||
}
|
||||
}
|
||||
|
||||
// TelegramHooksNewPost response for creating Telegram webhook
|
||||
func TelegramHooksNewPost(ctx *context.Context) {
|
||||
createWebhook(ctx, telegramHookParams(ctx))
|
||||
}
|
||||
|
||||
// TelegramHooksEditPost response for editing Telegram webhook
|
||||
func TelegramHooksEditPost(ctx *context.Context) {
|
||||
editWebhook(ctx, telegramHookParams(ctx))
|
||||
}
|
||||
|
||||
func telegramHookParams(ctx *context.Context) webhookParams {
|
||||
form := web.GetForm(ctx).(*forms.NewTelegramHookForm)
|
||||
|
||||
return webhookParams{
|
||||
Type: webhook_module.TELEGRAM,
|
||||
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.ContentTypeJSON,
|
||||
WebhookForm: form.WebhookForm,
|
||||
Meta: &webhook_service.TelegramMeta{
|
||||
BotToken: form.BotToken,
|
||||
ChatID: form.ChatID,
|
||||
ThreadID: form.ThreadID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// MSTeamsHooksNewPost response for creating MSTeams webhook
|
||||
func MSTeamsHooksNewPost(ctx *context.Context) {
|
||||
createWebhook(ctx, mSTeamsHookParams(ctx))
|
||||
|
|
|
@ -403,7 +403,6 @@ func registerRoutes(m *web.Route) {
|
|||
addWebhookAddRoutes := func() {
|
||||
m.Get("/{type}/new", repo_setting.WebhooksNew)
|
||||
m.Post("/gogs/new", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksNewPost)
|
||||
m.Post("/telegram/new", web.Bind(forms.NewTelegramHookForm{}), repo_setting.TelegramHooksNewPost)
|
||||
m.Post("/msteams/new", web.Bind(forms.NewMSTeamsHookForm{}), repo_setting.MSTeamsHooksNewPost)
|
||||
m.Post("/feishu/new", web.Bind(forms.NewFeishuHookForm{}), repo_setting.FeishuHooksNewPost)
|
||||
m.Post("/wechatwork/new", web.Bind(forms.NewWechatWorkHookForm{}), repo_setting.WechatworkHooksNewPost)
|
||||
|
@ -413,7 +412,6 @@ func registerRoutes(m *web.Route) {
|
|||
|
||||
addWebhookEditRoutes := func() {
|
||||
m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost)
|
||||
m.Post("/telegram/{id}", web.Bind(forms.NewTelegramHookForm{}), repo_setting.TelegramHooksEditPost)
|
||||
m.Post("/msteams/{id}", web.Bind(forms.NewMSTeamsHookForm{}), repo_setting.MSTeamsHooksEditPost)
|
||||
m.Post("/feishu/{id}", web.Bind(forms.NewFeishuHookForm{}), repo_setting.FeishuHooksEditPost)
|
||||
m.Post("/wechatwork/{id}", web.Bind(forms.NewWechatWorkHookForm{}), repo_setting.WechatworkHooksEditPost)
|
||||
|
|
|
@ -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"`
|
||||
|
|
|
@ -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 (
|
||||
|
|
Loading…
Reference in a new issue