Skip to content

Added support for custom Telegram proxy host #29648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2433,6 +2433,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.com/usage/protected-tags">protected tags guide</a>.
settings.bot_token = Bot Token
settings.chat_id = Chat ID
settings.custom_host= Custom Host
settings.thread_id = Thread ID
settings.matrix.homeserver_url = Homeserver URL
settings.matrix.room_id = Room ID
Expand Down
12 changes: 8 additions & 4 deletions routers/web/repo/setting/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,19 @@ func TelegramHooksEditPost(ctx *context.Context) {
func telegramHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewTelegramHookForm)

if form.CustomHost == "" {
form.CustomHost = "https://api.telegram.org"
Copy link
Contributor

@yp05327 yp05327 Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convert the default server host into a const. and add it to ctx.Data, then we can use it in template.
Then there's no need to maintain both frontend and backend.

}
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)),
URL: fmt.Sprintf("%s/bot%s/sendMessage?chat_id=%s&message_thread_id=%s", form.CustomHost, 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,
BotToken: form.BotToken,
ChatID: form.ChatID,
CustomHost: form.CustomHost,
ThreadID: form.ThreadID,
},
}
}
Expand Down
7 changes: 4 additions & 3 deletions services/forms/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,10 @@ func (f *NewDingtalkHookForm) Validate(req *http.Request, errs binding.Errors) b

// NewTelegramHookForm form for creating telegram hook
type NewTelegramHookForm struct {
BotToken string `binding:"Required"`
ChatID string `binding:"Required"`
ThreadID string
BotToken string `binding:"Required"`
ChatID string `binding:"Required"`
CustomHost string
ThreadID string
WebhookForm
}

Expand Down
7 changes: 4 additions & 3 deletions services/webhook/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ type (

// TelegramMeta contains the telegram metadata
TelegramMeta struct {
BotToken string `json:"bot_token"`
ChatID string `json:"chat_id"`
ThreadID string `json:"thread_id"`
BotToken string `json:"bot_token"`
ChatID string `json:"chat_id"`
CustomHost string `json:"custom_host"`
ThreadID string `json:"thread_id"`
}
)

Expand Down
4 changes: 4 additions & 0 deletions templates/repo/settings/webhook/telegram.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<label for="chat_id">{{ctx.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_CustomHost}}error{{end}}">
<label for="custom_host">{{ctx.Locale.Tr "repo.settings.custom_host"}}</label>
<input id="custom_host" name="custom_host" type="text" value="{{.TelegramHook.CustomHost}}" placeholder="https://api.telegram.org">
</div>
<div class="field {{if .Err_ThreadID}}error{{end}}">
<label for="thread_id">{{ctx.Locale.Tr "repo.settings.thread_id"}}</label>
<input id="thread_id" name="thread_id" type="text" value="{{.TelegramHook.ThreadID}}">
Expand Down