tg支持自定义api

This commit is contained in:
johlanse 2022-08-10 18:17:49 +08:00
parent f59c425735
commit 66b35f3585
6 changed files with 31 additions and 29 deletions

View File

@ -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 {
}

View File

@ -38,6 +38,8 @@ tg:
chat_id: 0
token: ""
proxy: ""
# 自定义tg的api,可通过cloudflare搭建需自备域名
custom_api: "https://api.telegram.org"
# 网页端配置
web:

View File

@ -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 {

View File

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

View File

@ -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, " <br/> "))
datas = []string{}
return
if message != "" {
send(message)
}
datas = append(datas, message)
send(strings.Join(datas, " <br/> "))
datas = []string{}
default:
if len(datas) > 10 {
send(strings.Join(datas, " <br/> "))
datas = []string{}
} else {
datas = append(datas, message)
if log.GetLevel() == log.DebugLevel {
send(message)
}
}
}

View File

@ -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)