independent sign task
This commit is contained in:
parent
15e9d93abd
commit
956942419b
13
bots/bots.go
13
bots/bots.go
@ -5,9 +5,6 @@ import (
|
|||||||
"github.com/iyear/E5SubBot/config"
|
"github.com/iyear/E5SubBot/config"
|
||||||
"github.com/iyear/E5SubBot/logger"
|
"github.com/iyear/E5SubBot/logger"
|
||||||
"github.com/iyear/E5SubBot/model"
|
"github.com/iyear/E5SubBot/model"
|
||||||
"github.com/iyear/E5SubBot/task"
|
|
||||||
"github.com/robfig/cron/v3"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
tb "gopkg.in/tucnak/telebot.v2"
|
tb "gopkg.in/tucnak/telebot.v2"
|
||||||
@ -40,6 +37,9 @@ func BotStart() {
|
|||||||
logger.InitLogger()
|
logger.InitLogger()
|
||||||
//InitDB
|
//InitDB
|
||||||
model.InitDB()
|
model.InitDB()
|
||||||
|
//Init Task
|
||||||
|
InitTask()
|
||||||
|
|
||||||
Poller := &tb.LongPoller{Timeout: 15 * time.Second}
|
Poller := &tb.LongPoller{Timeout: 15 * time.Second}
|
||||||
spamPoller := tb.NewMiddlewarePoller(Poller, func(upd *tb.Update) bool {
|
spamPoller := tb.NewMiddlewarePoller(Poller, func(upd *tb.Update) bool {
|
||||||
if upd.Message == nil {
|
if upd.Message == nil {
|
||||||
@ -75,7 +75,6 @@ func BotStart() {
|
|||||||
fmt.Println("Bot: " + strconv.Itoa(bot.Me.ID) + " " + bot.Me.Username)
|
fmt.Println("Bot: " + strconv.Itoa(bot.Me.ID) + " " + bot.Me.Username)
|
||||||
|
|
||||||
MakeHandle()
|
MakeHandle()
|
||||||
TaskLaunch()
|
|
||||||
fmt.Println("Bot Start")
|
fmt.Println("Bot Start")
|
||||||
fmt.Println("------------")
|
fmt.Println("------------")
|
||||||
bot.Start()
|
bot.Start()
|
||||||
@ -94,9 +93,3 @@ func MakeHandle() {
|
|||||||
bot.Handle("/task", bTask)
|
bot.Handle("/task", bTask)
|
||||||
bot.Handle("/log", bLog)
|
bot.Handle("/log", bLog)
|
||||||
}
|
}
|
||||||
func TaskLaunch() {
|
|
||||||
c := cron.New()
|
|
||||||
c.AddFunc(viper.GetString("cron"), task.SignTask)
|
|
||||||
fmt.Println("Cron Task Start……")
|
|
||||||
c.Start()
|
|
||||||
}
|
|
||||||
|
|||||||
@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/iyear/E5SubBot/config"
|
"github.com/iyear/E5SubBot/config"
|
||||||
"github.com/iyear/E5SubBot/model"
|
"github.com/iyear/E5SubBot/model"
|
||||||
"github.com/iyear/E5SubBot/task"
|
|
||||||
"github.com/iyear/E5SubBot/util"
|
"github.com/iyear/E5SubBot/util"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
tb "gopkg.in/tucnak/telebot.v2"
|
tb "gopkg.in/tucnak/telebot.v2"
|
||||||
@ -226,7 +225,7 @@ func bOnText(m *tb.Message) {
|
|||||||
func bTask(m *tb.Message) {
|
func bTask(m *tb.Message) {
|
||||||
for _, a := range config.Admins {
|
for _, a := range config.Admins {
|
||||||
if a == m.Chat.ID {
|
if a == m.Chat.ID {
|
||||||
task.SignTask()
|
SignTask()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,12 +23,14 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
BotToken string
|
BotToken string
|
||||||
Socks5 string
|
Socks5 string
|
||||||
BindMaxNum int
|
BindMaxNum int
|
||||||
ErrMaxTimes int
|
MaxGoroutines int
|
||||||
Notice string
|
MaxErrTimes int
|
||||||
Admins []int64
|
Cron string
|
||||||
|
Notice string
|
||||||
|
Admins []int64
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitConfig() {
|
func InitConfig() {
|
||||||
@ -45,14 +47,17 @@ func InitConfig() {
|
|||||||
viper.SetDefault("bindmax", 5)
|
viper.SetDefault("bindmax", 5)
|
||||||
|
|
||||||
BindMaxNum = viper.GetInt("bindmax")
|
BindMaxNum = viper.GetInt("bindmax")
|
||||||
ErrMaxTimes = viper.GetInt("errlimit")
|
MaxErrTimes = viper.GetInt("errlimit")
|
||||||
Notice = viper.GetString("notice")
|
Notice = viper.GetString("notice")
|
||||||
|
Cron = viper.GetString("cron")
|
||||||
|
MaxGoroutines = viper.GetInt("goroutine")
|
||||||
Admins = getAdmins()
|
Admins = getAdmins()
|
||||||
|
|
||||||
viper.WatchConfig()
|
viper.WatchConfig()
|
||||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||||
|
MaxGoroutines = viper.GetInt("goroutine")
|
||||||
BindMaxNum = viper.GetInt("bindmax")
|
BindMaxNum = viper.GetInt("bindmax")
|
||||||
ErrMaxTimes = viper.GetInt("errlimit")
|
MaxErrTimes = viper.GetInt("errlimit")
|
||||||
Notice = viper.GetString("notice")
|
Notice = viper.GetString("notice")
|
||||||
Admins = getAdmins()
|
Admins = getAdmins()
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,25 +1,29 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
TgId int64 `gorm:"not null"`
|
TgId int64 `gorm:"not null"`
|
||||||
RefreshToken string `gorm:"not null"`
|
RefreshToken string `gorm:"not null"`
|
||||||
MsId string `gorm:"unique;primaryKey;not null"`
|
MsId string `gorm:"not null"`
|
||||||
Uptime int64 `gorm:"autoUpdateTime;not null"`
|
Uptime int64 `gorm:"autoUpdateTime;not null"`
|
||||||
Alias string `gorm:"not null"`
|
Alias string `gorm:"not null"`
|
||||||
ClientId string `gorm:"not null"`
|
ClientId string `gorm:"not null"`
|
||||||
ClientSecret string `gorm:"not null"`
|
ClientSecret string `gorm:"not null"`
|
||||||
Other string
|
Other string
|
||||||
}
|
}
|
||||||
|
type ErrClient struct {
|
||||||
|
*Client
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
msApiUrl string = "https://login.microsoftonline.com"
|
msApiUrl string = "https://login.microsoftonline.com"
|
||||||
@ -28,6 +32,15 @@ const (
|
|||||||
scope string = "openid offline_access mail.read user.read"
|
scope string = "openid offline_access mail.read user.read"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var client = &http.Client{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
client.Timeout = 10 * time.Second
|
||||||
|
tp := http.DefaultTransport.(*http.Transport).Clone()
|
||||||
|
tp.MaxIdleConns = 100
|
||||||
|
tp.MaxIdleConnsPerHost = 100
|
||||||
|
client.Transport = tp
|
||||||
|
}
|
||||||
func NewClient(clientId string, clientSecret string) *Client {
|
func NewClient(clientId string, clientSecret string) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
ClientId: clientId,
|
ClientId: clientId,
|
||||||
@ -47,7 +60,6 @@ func GetMSRegisterAppUrl() string {
|
|||||||
// GetTokenWithCode return access_token and refresh_token
|
// GetTokenWithCode return access_token and refresh_token
|
||||||
func (c *Client) GetTokenWithCode(code string) (error error) {
|
func (c *Client) GetTokenWithCode(code string) (error error) {
|
||||||
var r http.Request
|
var r http.Request
|
||||||
client := &http.Client{}
|
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
r.Form.Add("client_id", c.ClientId)
|
r.Form.Add("client_id", c.ClientId)
|
||||||
r.Form.Add("client_secret", c.ClientSecret)
|
r.Form.Add("client_secret", c.ClientSecret)
|
||||||
@ -60,17 +72,15 @@ func (c *Client) GetTokenWithCode(code string) (error error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//prevents the connection from being re-used
|
//prevents the connection from being re-used
|
||||||
////https://stackoverflow.com/questions/17714494/golang-http-request-results-in-eof-errors-when-making-multiple-requests-successi
|
////https://stackoverflow.com/questions/17714494/golang-http-request-results-in-eof-errors-when-making-multiple-requests-successi
|
||||||
req.Close = true
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
content, err := ioutil.ReadAll(resp.Body)
|
content, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -84,7 +94,6 @@ func (c *Client) GetTokenWithCode(code string) (error error) {
|
|||||||
//return access_token and new refresh token
|
//return access_token and new refresh token
|
||||||
func (c *Client) getToken() (access string) {
|
func (c *Client) getToken() (access string) {
|
||||||
var r http.Request
|
var r http.Request
|
||||||
client := &http.Client{}
|
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
r.Form.Add("client_id", c.ClientId)
|
r.Form.Add("client_id", c.ClientId)
|
||||||
r.Form.Add("client_secret", c.ClientSecret)
|
r.Form.Add("client_secret", c.ClientSecret)
|
||||||
@ -98,7 +107,6 @@ func (c *Client) getToken() (access string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
req.Close = true
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
@ -117,14 +125,11 @@ func (c *Client) getToken() (access string) {
|
|||||||
|
|
||||||
// GetUserInfo Get User's Information
|
// GetUserInfo Get User's Information
|
||||||
func (c *Client) GetUserInfo() (json string, error error) {
|
func (c *Client) GetUserInfo() (json string, error error) {
|
||||||
client := http.Client{}
|
|
||||||
req, err := http.NewRequest("GET", msGraUrl+"/v1.0/me", nil)
|
req, err := http.NewRequest("GET", msGraUrl+"/v1.0/me", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Close = true
|
|
||||||
|
|
||||||
req.Header.Set("Authorization", c.getToken())
|
req.Header.Set("Authorization", c.getToken())
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -132,7 +137,7 @@ func (c *Client) GetUserInfo() (json string, error error) {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
content, err := ioutil.ReadAll(resp.Body)
|
content, err := ioutil.ReadAll(resp.Body)
|
||||||
fmt.Println(string(content))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -144,12 +149,10 @@ func (c *Client) GetUserInfo() (json string, error error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetOutlookMails() error {
|
func (c *Client) GetOutlookMails() error {
|
||||||
client := http.Client{}
|
|
||||||
req, err := http.NewRequest("GET", msGraUrl+"/v1.0/me/messages", nil)
|
req, err := http.NewRequest("GET", msGraUrl+"/v1.0/me/messages", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req.Close = true
|
|
||||||
req.Header.Set("Authorization", c.getToken())
|
req.Header.Set("Authorization", c.getToken())
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -157,6 +160,7 @@ func (c *Client) GetOutlookMails() error {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
content, err := ioutil.ReadAll(resp.Body)
|
content, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user