🔖 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")

View File

@@ -3,6 +3,8 @@ package telegram
import (
"errors"
"fmt"
"net/http"
"net/url"
"one-api/common"
"one-api/model"
"strings"
@@ -14,6 +16,7 @@ import (
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters/callbackquery"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters/message"
"github.com/spf13/viper"
"golang.org/x/net/proxy"
)
var TGupdater *ext.Updater
@@ -35,7 +38,7 @@ func InitTelegramBot() {
}
var err error
TGBot, err = gotgbot.NewBot(botKey, nil)
TGBot, err = gotgbot.NewBot(botKey, getBotOpts())
if err != nil {
common.SysLog("failed to create new telegram bot: " + err.Error())
return
@@ -220,3 +223,53 @@ func getBindUser(b *gotgbot.Bot, ctx *ext.Context) *model.User {
return user
}
func getHttpClient() (httpClient *http.Client) {
proxyAddr := viper.GetString("tg.http_proxy") // http/socks5
if proxyAddr == "" {
return
}
proxyURL, err := url.Parse(proxyAddr)
if err != nil {
common.SysLog("failed to parse TG proxy URL: " + err.Error())
return
}
switch proxyURL.Scheme {
case "http", "https":
httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
},
}
case "socks5":
dialer, err := proxy.SOCKS5("tcp", proxyURL.Host, nil, proxy.Direct)
if err != nil {
common.SysLog("failed to create TG SOCKS5 dialer: " + err.Error())
return
}
httpClient = &http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
},
}
default:
common.SysLog("unknown TG proxy type: " + proxyAddr)
}
return
}
func getBotOpts() *gotgbot.BotOpts {
httpClient := getHttpClient()
if httpClient == nil {
return nil
}
return &gotgbot.BotOpts{
BotClient: &gotgbot.BaseBotClient{
Client: *httpClient,
},
}
}