删除冗余的代码
This commit is contained in:
parent
66acd48e3a
commit
24a43987e3
16
bot.go
16
bot.go
@ -1,6 +1,7 @@
|
|||||||
package openwechat
|
package openwechat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -14,7 +15,8 @@ type Bot struct {
|
|||||||
GetMessageErrorHandler func(err error) // 获取消息发生错误的handle
|
GetMessageErrorHandler func(err error) // 获取消息发生错误的handle
|
||||||
isHot bool
|
isHot bool
|
||||||
err error
|
err error
|
||||||
exit chan bool
|
context context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
Caller *Caller
|
Caller *Caller
|
||||||
self *Self
|
self *Self
|
||||||
storage *Storage
|
storage *Storage
|
||||||
@ -27,7 +29,7 @@ func (b *Bot) Alive() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case <-b.exit:
|
case <-b.context.Done():
|
||||||
return false
|
return false
|
||||||
default:
|
default:
|
||||||
return true
|
return true
|
||||||
@ -236,7 +238,7 @@ func (b *Bot) asyncCall() error {
|
|||||||
|
|
||||||
// 当获取消息发生错误时, 默认的错误处理行为
|
// 当获取消息发生错误时, 默认的错误处理行为
|
||||||
func (b *Bot) stopAsyncCALL(err error) {
|
func (b *Bot) stopAsyncCALL(err error) {
|
||||||
b.exit <- true
|
b.cancel()
|
||||||
b.err = err
|
b.err = err
|
||||||
b.self = nil
|
b.self = nil
|
||||||
log.Printf("exit with : %s", err.Error())
|
log.Printf("exit with : %s", err.Error())
|
||||||
@ -267,10 +269,7 @@ func (b *Bot) Block() error {
|
|||||||
if b.self == nil {
|
if b.self == nil {
|
||||||
return errors.New("`Block` must be called after user login")
|
return errors.New("`Block` must be called after user login")
|
||||||
}
|
}
|
||||||
if _, notClose := <-b.exit; !notClose {
|
<-b.context.Done()
|
||||||
return errors.New("can not call `Block` after user logout")
|
|
||||||
}
|
|
||||||
close(b.exit)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -291,7 +290,8 @@ func (b *Bot) MessageOnError(h func(err error)) {
|
|||||||
|
|
||||||
// Bot的构造方法,需要自己传入Caller
|
// Bot的构造方法,需要自己传入Caller
|
||||||
func NewBot(caller *Caller) *Bot {
|
func NewBot(caller *Caller) *Bot {
|
||||||
return &Bot{Caller: caller, storage: &Storage{}, exit: make(chan bool)}
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
return &Bot{Caller: caller, storage: &Storage{}, context: ctx, cancel: cancel}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 默认的Bot的构造方法,
|
// 默认的Bot的构造方法,
|
||||||
|
@ -230,8 +230,6 @@ func (c *Client) sendMessage(request *BaseRequest, url string, msg *SendMessage)
|
|||||||
body, _ := ToBuffer(content)
|
body, _ := ToBuffer(content)
|
||||||
req, _ := http.NewRequest(http.MethodPost, url, body)
|
req, _ := http.NewRequest(http.MethodPost, url, body)
|
||||||
req.Header.Add("Content-Type", jsonContentType)
|
req.Header.Add("Content-Type", jsonContentType)
|
||||||
fmt.Println(6666)
|
|
||||||
fmt.Println(body.String())
|
|
||||||
return c.Do(req)
|
return c.Do(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,3 +124,5 @@ var imageType = map[string]bool{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var videoType = "mp4"
|
var videoType = "mp4"
|
||||||
|
|
||||||
|
const appMessageAppId = "wxeb7ec651dd0aefa9"
|
||||||
|
@ -495,7 +495,7 @@ func (f appmsg) XmlByte() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewFileAppMessage(stat os.FileInfo, attachId string) *appmsg {
|
func NewFileAppMessage(stat os.FileInfo, attachId string) *appmsg {
|
||||||
m := &appmsg{AppId: "wxeb7ec651dd0aefa9", Title: stat.Name()}
|
m := &appmsg{AppId: appMessageAppId, Title: stat.Name()}
|
||||||
m.AppAttach.AttachId = attachId
|
m.AppAttach.AttachId = attachId
|
||||||
m.AppAttach.TotalLen = stat.Size()
|
m.AppAttach.TotalLen = stat.Size()
|
||||||
m.Type = 6
|
m.Type = 6
|
||||||
|
Loading…
x
Reference in New Issue
Block a user