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_plus" yaml:"push_plus"`
} `json:"push" yaml:"push"` } `json:"push" yaml:"push"`
TG struct { TG struct {
Enable bool `json:"enable" yaml:"enable"` Enable bool `json:"enable" yaml:"enable"`
Token string `json:"token" yaml:"token"` Token string `json:"token" yaml:"token"`
ChatID int64 `json:"chat_id" yaml:"chat_id"` ChatID int64 `json:"chat_id" yaml:"chat_id"`
Proxy string `json:"proxy" yaml:"proxy"` Proxy string `json:"proxy" yaml:"proxy"`
CustomApi string `json:"custom_api" yaml:"custom_api"`
} `json:"tg" yaml:"tg"` } `json:"tg" yaml:"tg"`
QQ struct { QQ struct {
} }

View File

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

View File

@ -1,6 +1,7 @@
package lib package lib
import ( import (
"crypto/tls"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net/http" "net/http"
@ -68,15 +69,22 @@ func (t *Telegram) Init() {
newPlugin("/get_scores", getScores) newPlugin("/get_scores", getScores)
newPlugin("/quit", quit) newPlugin("/quit", quit)
newPlugin("/study_all", studyAll) newPlugin("/study_all", studyAll)
var err error
uri, err := url.Parse(t.Proxy) var uri *url.URL
if err != nil { if t.Proxy != "" {
log.Errorln("代理解析失败" + err.Error()) uri, err = url.Parse(t.Proxy)
err = nil 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 { if err != nil {

View File

@ -132,9 +132,7 @@ func main() {
} }
}() }()
log.Infoln("已采用定时执行模式") log.Infoln("已采用定时执行模式")
c := cron.New(func(c *cron.Cron) { c := cron.New()
})
_, err := c.AddFunc(config.Cron, func() { _, err := c.AddFunc(config.Cron, func() {
defer func() { defer func() {
@ -149,6 +147,7 @@ func main() {
if config.CronRandomWait > 0 { if config.CronRandomWait > 0 {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
r := rand.Intn(config.CronRandomWait) r := rand.Intn(config.CronRandomWait)
log.Infoln(fmt.Sprintf("随机延迟%d分钟", r))
time.Sleep(time.Duration(r) * time.Minute) time.Sleep(time.Duration(r) * time.Minute)
} }
do("cron") do("cron")

View File

@ -2,7 +2,6 @@ package push
import ( import (
"fmt" "fmt"
"strings"
"github.com/guonaihong/gout" "github.com/guonaihong/gout"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -12,8 +11,6 @@ type PushPlus struct {
Token string Token string
} }
var datas []string
func (p *PushPlus) Init() func(kind, message string) { func (p *PushPlus) Init() func(kind, message string) {
send := func(data string) { send := func(data string) {
err := gout.POST("http://www.pushplus.plus/send").SetJSON(gout.H{ 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) message = fmt.Sprintf("![](%v)", "data:image/png;base64,"+message)
send(message) send(message)
case kind == "flush": case kind == "flush":
if message == "" { if message != "" {
send(strings.Join(datas, " <br/> ")) send(message)
datas = []string{}
return
} }
datas = append(datas, message)
send(strings.Join(datas, " <br/> "))
datas = []string{}
default: default:
if len(datas) > 10 { if log.GetLevel() == log.DebugLevel {
send(strings.Join(datas, " <br/> ")) send(message)
datas = []string{}
} else {
datas = append(datas, message)
} }
} }
} }

View File

@ -13,6 +13,9 @@ import (
*/ */
func Restart() { func Restart() {
cmd := exec.Command("./study_xxqg") cmd := exec.Command("./study_xxqg")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
go func() { go func() {
cmd.Start() cmd.Start()
os.Exit(3) os.Exit(3)