🔖 chore: TGBot add http proxy (#192)

This commit is contained in:
MartialBE
2024-05-19 21:48:39 +08:00
parent 424f255acc
commit 96f19fb35f
5 changed files with 69 additions and 10 deletions

View File

@@ -109,7 +109,8 @@ func TestTelegramSend(t *testing.T) {
InitConfig()
secret := viper.GetString("notify.telegram.bot_api_key")
chatID := viper.GetString("notify.telegram.chat_id")
dingTalk := channel.NewTelegram(secret, chatID)
httpProxy := viper.GetString("notify.telegram.http_proxy")
dingTalk := channel.NewTelegram(secret, chatID, httpProxy)
err := dingTalk.Send(context.Background(), "Test Title", "*Test Message*")
fmt.Println(err)
@@ -120,7 +121,7 @@ func TestTelegramSendError(t *testing.T) {
InitConfig()
secret := "test"
chatID := viper.GetString("notify.telegram.chat_id")
dingTalk := channel.NewTelegram(secret, chatID)
dingTalk := channel.NewTelegram(secret, chatID, "")
err := dingTalk.Send(context.Background(), "Test Title", "*Test Message*")
fmt.Println(err)

View File

@@ -12,8 +12,9 @@ import (
const telegramURL = "https://api.telegram.org/bot"
type Telegram struct {
secret string
chatID string
secret string
chatID string
httpProxy string
}
type telegramMessage struct {
@@ -27,10 +28,11 @@ type telegramResponse struct {
Description string `json:"description"`
}
func NewTelegram(secret string, chatID string) *Telegram {
func NewTelegram(secret, chatID, httpProxy string) *Telegram {
return &Telegram{
secret: secret,
chatID: chatID,
secret: secret,
chatID: chatID,
httpProxy: httpProxy,
}
}
@@ -43,7 +45,7 @@ func (t *Telegram) Send(ctx context.Context, title, message string) error {
message = fmt.Sprintf("*%s*\n%s", title, message)
messages := splitTelegramMessageIntoParts(message, maxMessageLength)
client := requester.NewHTTPRequester("", telegramErrFunc)
client := requester.NewHTTPRequester(t.httpProxy, telegramErrFunc)
client.Context = ctx
client.IsOpenAI = false

View File

@@ -87,11 +87,12 @@ func InitPushdeerNotifier() {
func InitTelegramNotifier() {
bot_token := viper.GetString("notify.telegram.bot_api_key")
chat_id := viper.GetString("notify.telegram.chat_id")
httpProxy := viper.GetString("notify.telegram.http_proxy")
if bot_token == "" || chat_id == "" {
return
}
telegramNotifier := channel.NewTelegram(bot_token, chat_id)
telegramNotifier := channel.NewTelegram(bot_token, chat_id, httpProxy)
AddNotifiers(telegramNotifier)
common.SysLog("telegram notifier enable")