feat: api and proxy optional

This commit is contained in:
Vaala Cat 2023-06-27 12:45:55 +08:00
parent ac593ddbc8
commit d4f070f9ec
3 changed files with 16 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.env
run.sh
mcbot

View File

@ -12,6 +12,7 @@ type botSettings struct {
MCBotName string `env:"MC_BOT_NAME"`
GroupID int64 `env:"GROUP_ID"`
DBPath string `env:"DB_PATH"`
BotAPI string `env:"TG_BOT_API"`
}
var (

View File

@ -16,18 +16,26 @@ import (
func Run(sendFunc func(string)) {
var err error
api := conf.GetBotSettings().BotAPI
if len(api) == 0 {
api = tgbotapi.APIEndpoint
}
HttpProxy := conf.GetBotSettings().HTTPProxy
proxyUrl, err := url.Parse(HttpProxy)
if err != nil {
log.Panic(err, "HTTP_PROXY environment variable is not set correctly")
}
if len(HttpProxy) != 0 {
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
conf.Bot, err = tgbotapi.NewBotAPIWithClient(
conf.GetBotSettings().BotToken,
tgbotapi.APIEndpoint,
// conf.GetBotSettings().TGBotApi,
api,
client)
} else {
conf.Bot, err = tgbotapi.NewBotAPIWithAPIEndpoint(conf.GetBotSettings().BotToken, api)
}
if err != nil {
log.Panic(err)