Telegram webhook (#4227)

This commit is contained in:
techknowlogick 2019-04-18 22:45:02 -04:00 committed by GitHub
parent 6dbd261852
commit 56da256853
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 478 additions and 1 deletions

View file

@ -331,6 +331,55 @@ func DingtalkHooksNewPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
ctx.Redirect(orCtx.Link)
}
// TelegramHooksNewPost response for creating telegram hook
func TelegramHooksNewPost(ctx *context.Context, form auth.NewTelegramHookForm) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
ctx.ServerError("getOrgRepoCtx", err)
return
}
if ctx.HasError() {
ctx.HTML(200, orCtx.NewTemplate)
return
}
meta, err := json.Marshal(&models.TelegramMeta{
BotToken: form.BotToken,
ChatID: form.ChatID,
})
if err != nil {
ctx.ServerError("Marshal", err)
return
}
w := &models.Webhook{
RepoID: orCtx.RepoID,
URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", form.BotToken, form.ChatID),
ContentType: models.ContentTypeJSON,
HookEvent: ParseHookEvent(form.WebhookForm),
IsActive: form.Active,
HookTaskType: models.TELEGRAM,
Meta: string(meta),
OrgID: orCtx.OrgID,
}
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
} else if err := models.CreateWebhook(w); err != nil {
ctx.ServerError("CreateWebhook", err)
return
}
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
ctx.Redirect(orCtx.Link)
}
// SlackHooksNewPost response for creating slack hook
func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
@ -421,6 +470,8 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
ctx.Data["SlackHook"] = w.GetSlackHook()
case models.DISCORD:
ctx.Data["DiscordHook"] = w.GetDiscordHook()
case models.TELEGRAM:
ctx.Data["TelegramHook"] = w.GetTelegramHook()
}
ctx.Data["History"], err = w.History(1)
@ -647,6 +698,46 @@ func DingtalkHooksEditPost(ctx *context.Context, form auth.NewDingtalkHookForm)
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
}
// TelegramHooksEditPost response for editing discord hook
func TelegramHooksEditPost(ctx *context.Context, form auth.NewTelegramHookForm) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksEdit"] = true
orCtx, w := checkWebhook(ctx)
if ctx.Written() {
return
}
ctx.Data["Webhook"] = w
if ctx.HasError() {
ctx.HTML(200, orCtx.NewTemplate)
return
}
meta, err := json.Marshal(&models.TelegramMeta{
BotToken: form.BotToken,
ChatID: form.ChatID,
})
if err != nil {
ctx.ServerError("Marshal", err)
return
}
w.Meta = string(meta)
w.URL = fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", form.BotToken, form.ChatID)
w.HookEvent = ParseHookEvent(form.WebhookForm)
w.IsActive = form.Active
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
} else if err := models.UpdateWebhook(w); err != nil {
ctx.ServerError("UpdateWebhook", err)
return
}
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
}
// TestWebhook test if web hook is work fine
func TestWebhook(ctx *context.Context) {
hookID := ctx.ParamsInt64(":id")