From 66b35f3585d478864ebf7275665ca8cedfce1c8a Mon Sep 17 00:00:00 2001 From: johlanse Date: Wed, 10 Aug 2022 18:17:49 +0800 Subject: [PATCH] =?UTF-8?q?tg=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/config.go | 9 +++++---- conf/config_default.yml | 2 ++ lib/tg.go | 22 +++++++++++++++------- main.go | 5 ++--- push/pushPlus.go | 19 ++++--------------- utils/util.go | 3 +++ 6 files changed, 31 insertions(+), 29 deletions(-) diff --git a/conf/config.go b/conf/config.go index ec6da14..2836217 100644 --- a/conf/config.go +++ b/conf/config.go @@ -28,10 +28,11 @@ type Config struct { } `json:"push_plus" yaml:"push_plus"` } `json:"push" yaml:"push"` TG struct { - Enable bool `json:"enable" yaml:"enable"` - Token string `json:"token" yaml:"token"` - ChatID int64 `json:"chat_id" yaml:"chat_id"` - Proxy string `json:"proxy" yaml:"proxy"` + Enable bool `json:"enable" yaml:"enable"` + Token string `json:"token" yaml:"token"` + ChatID int64 `json:"chat_id" yaml:"chat_id"` + Proxy string `json:"proxy" yaml:"proxy"` + CustomApi string `json:"custom_api" yaml:"custom_api"` } `json:"tg" yaml:"tg"` QQ struct { } diff --git a/conf/config_default.yml b/conf/config_default.yml index bd72a5e..59efb98 100644 --- a/conf/config_default.yml +++ b/conf/config_default.yml @@ -38,6 +38,8 @@ tg: chat_id: 0 token: "" proxy: "" + # 自定义tg的api,可通过cloudflare搭建,需自备域名 + custom_api: "https://api.telegram.org" # 网页端配置 web: diff --git a/lib/tg.go b/lib/tg.go index 48ff7c0..9326625 100644 --- a/lib/tg.go +++ b/lib/tg.go @@ -1,6 +1,7 @@ package lib import ( + "crypto/tls" "encoding/base64" "fmt" "net/http" @@ -68,15 +69,22 @@ func (t *Telegram) Init() { newPlugin("/get_scores", getScores) newPlugin("/quit", quit) newPlugin("/study_all", studyAll) - - uri, err := url.Parse(t.Proxy) - if err != nil { - log.Errorln("代理解析失败" + err.Error()) - err = nil + var err error + var uri *url.URL + if t.Proxy != "" { + uri, err = url.Parse(t.Proxy) + if err != nil { + log.Errorln("代理解析失败" + err.Error()) + err = nil + } } - t.bot, err = tgbotapi.NewBotAPIWithClient(t.Token, tgbotapi.APIEndpoint, &http.Client{Transport: &http.Transport{ + + t.bot, err = tgbotapi.NewBotAPIWithClient(t.Token, conf.GetConfig().TG.CustomApi+"/bot%s/%s", &http.Client{Transport: &http.Transport{ // 设置代理 - Proxy: http.ProxyURL(uri), + Proxy: func(r *http.Request) (*url.URL, error) { + return uri, nil + }, + TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper), }}) if err != nil { diff --git a/main.go b/main.go index 35047e9..dc28118 100644 --- a/main.go +++ b/main.go @@ -132,9 +132,7 @@ func main() { } }() log.Infoln("已采用定时执行模式") - c := cron.New(func(c *cron.Cron) { - - }) + c := cron.New() _, err := c.AddFunc(config.Cron, func() { defer func() { @@ -149,6 +147,7 @@ func main() { if config.CronRandomWait > 0 { rand.Seed(time.Now().UnixNano()) r := rand.Intn(config.CronRandomWait) + log.Infoln(fmt.Sprintf("随机延迟%d分钟", r)) time.Sleep(time.Duration(r) * time.Minute) } do("cron") diff --git a/push/pushPlus.go b/push/pushPlus.go index df04b1f..2058780 100644 --- a/push/pushPlus.go +++ b/push/pushPlus.go @@ -2,7 +2,6 @@ package push import ( "fmt" - "strings" "github.com/guonaihong/gout" log "github.com/sirupsen/logrus" @@ -12,8 +11,6 @@ type PushPlus struct { Token string } -var datas []string - func (p *PushPlus) Init() func(kind, message string) { send := func(data string) { err := gout.POST("http://www.pushplus.plus/send").SetJSON(gout.H{ @@ -35,20 +32,12 @@ func (p *PushPlus) Init() func(kind, message string) { message = fmt.Sprintf("![](%v)", "data:image/png;base64,"+message) send(message) case kind == "flush": - if message == "" { - send(strings.Join(datas, "
")) - datas = []string{} - return + if message != "" { + send(message) } - datas = append(datas, message) - send(strings.Join(datas, "
")) - datas = []string{} default: - if len(datas) > 10 { - send(strings.Join(datas, "
")) - datas = []string{} - } else { - datas = append(datas, message) + if log.GetLevel() == log.DebugLevel { + send(message) } } } diff --git a/utils/util.go b/utils/util.go index 72854cf..2459ddb 100644 --- a/utils/util.go +++ b/utils/util.go @@ -13,6 +13,9 @@ import ( */ func Restart() { cmd := exec.Command("./study_xxqg") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin go func() { cmd.Start() os.Exit(3)