From 21c3477875566b857ff65162337a08f5f5186de9 Mon Sep 17 00:00:00 2001
From: neveraskedtoexist <matikot415@gmail.com>
Date: Wed, 19 Jul 2023 21:46:29 +0200
Subject: [PATCH] [GITEA] Add ThreadID parameter for Telegram webhooks

(cherry picked from commit 29d77def40bbc614da7164b0525f4235cc96c844)
(cherry picked from commit c758aa56a2052a11aa6a41bf37f891a65e913779)
---
 options/locale/locale_en-US.ini               | 1 +
 routers/web/repo/setting/webhook.go           | 3 ++-
 services/forms/repo_form.go                   | 1 +
 services/webhook/telegram.go                  | 1 +
 templates/repo/settings/webhook/telegram.tmpl | 4 ++++
 5 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini
index e79a99086d..a88951bc2a 100644
--- a/options/locale/locale_en-US.ini
+++ b/options/locale/locale_en-US.ini
@@ -2307,6 +2307,7 @@ settings.tags.protection.none = There are no protected tags.
 settings.tags.protection.pattern.description = You can use a single name or a glob pattern or regular expression to match multiple tags. Read more in the <a target="_blank" rel="noopener" href="https://docs.gitea.io/en-us/protected-tags/">protected tags guide</a>.
 settings.bot_token = Bot Token
 settings.chat_id = Chat ID
+settings.thread_id = Thread ID
 settings.matrix.homeserver_url = Homeserver URL
 settings.matrix.room_id = Room ID
 settings.matrix.message_type = Message Type
diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go
index 5c4e1d47d0..bbcc36ee60 100644
--- a/routers/web/repo/setting/webhook.go
+++ b/routers/web/repo/setting/webhook.go
@@ -425,12 +425,13 @@ func telegramHookParams(ctx *context.Context) webhookParams {
 
 	return webhookParams{
 		Type:        webhook_module.TELEGRAM,
-		URL:         fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID)),
+		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,
 		},
 	}
 }
diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go
index a26a2d89c5..8c763e17cb 100644
--- a/services/forms/repo_form.go
+++ b/services/forms/repo_form.go
@@ -353,6 +353,7 @@ func (f *NewDingtalkHookForm) Validate(req *http.Request, errs binding.Errors) b
 type NewTelegramHookForm struct {
 	BotToken string `binding:"Required"`
 	ChatID   string `binding:"Required"`
+	ThreadID string
 	WebhookForm
 }
 
diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go
index 2d0484648b..ea7e8185de 100644
--- a/services/webhook/telegram.go
+++ b/services/webhook/telegram.go
@@ -28,6 +28,7 @@ type (
 	TelegramMeta struct {
 		BotToken string `json:"bot_token"`
 		ChatID   string `json:"chat_id"`
+		ThreadID string `json:"thread_id"`
 	}
 )
 
diff --git a/templates/repo/settings/webhook/telegram.tmpl b/templates/repo/settings/webhook/telegram.tmpl
index 4313980bf1..f618fea140 100644
--- a/templates/repo/settings/webhook/telegram.tmpl
+++ b/templates/repo/settings/webhook/telegram.tmpl
@@ -10,6 +10,10 @@
 			<label for="chat_id">{{.locale.Tr "repo.settings.chat_id"}}</label>
 			<input id="chat_id" name="chat_id" type="text" value="{{.TelegramHook.ChatID}}" required>
 		</div>
+		<div class="field {{if .Err_ThreadID}}error{{end}}">
+			<label for="thread_id">{{.locale.Tr "repo.settings.thread_id"}}</label>
+			<input id="thread_id" name="thread_id" type="text" value="{{.TelegramHook.ThreadID}}">
+		</div>
 		{{template "repo/settings/webhook/settings" .}}
 	</form>
 {{end}}