tg支持自定义api
This commit is contained in:
parent
f59c425735
commit
66b35f3585
|
@ -32,6 +32,7 @@ type Config struct {
|
|||
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 {
|
||||
}
|
||||
|
|
|
@ -38,6 +38,8 @@ tg:
|
|||
chat_id: 0
|
||||
token: ""
|
||||
proxy: ""
|
||||
# 自定义tg的api,可通过cloudflare搭建,需自备域名
|
||||
custom_api: "https://api.telegram.org"
|
||||
|
||||
# 网页端配置
|
||||
web:
|
||||
|
|
16
lib/tg.go
16
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)
|
||||
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 {
|
||||
|
|
5
main.go
5
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")
|
||||
|
|
|
@ -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("", "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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue