优化群发休眠逻辑
This commit is contained in:
parent
2acac1db49
commit
55cf87e294
68
parser.go
68
parser.go
@ -1,46 +1,54 @@
|
||||
package openwechat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ToBuffer(v interface{}) (*bytes.Buffer, error) {
|
||||
buf, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewBuffer(buf), nil
|
||||
buf, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewBuffer(buf), nil
|
||||
}
|
||||
|
||||
func GetRandomDeviceId() string {
|
||||
rand.Seed(time.Now().Unix())
|
||||
var builder strings.Builder
|
||||
builder.WriteString("e")
|
||||
for i := 0; i < 15; i++ {
|
||||
r := rand.Intn(9)
|
||||
builder.WriteString(strconv.Itoa(r))
|
||||
}
|
||||
return builder.String()
|
||||
rand.Seed(time.Now().Unix())
|
||||
var builder strings.Builder
|
||||
builder.WriteString("e")
|
||||
for i := 0; i < 15; i++ {
|
||||
r := rand.Intn(9)
|
||||
builder.WriteString(strconv.Itoa(r))
|
||||
}
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func getWebWxDataTicket(cookies []*http.Cookie) string {
|
||||
for _, cookie := range cookies {
|
||||
if cookie.Name == "webwx_data_ticket" {
|
||||
return cookie.Value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
for _, cookie := range cookies {
|
||||
if cookie.Name == "webwx_data_ticket" {
|
||||
return cookie.Value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func XmlFormString(text string) string {
|
||||
lt := strings.ReplaceAll(text, "<", "<")
|
||||
gt := strings.ReplaceAll(lt, ">", ">")
|
||||
br := strings.ReplaceAll(gt, "<br/>", "\n")
|
||||
return strings.ReplaceAll(br, "&amp;", "&")
|
||||
lt := strings.ReplaceAll(text, "<", "<")
|
||||
gt := strings.ReplaceAll(lt, ">", ">")
|
||||
br := strings.ReplaceAll(gt, "<br/>", "\n")
|
||||
return strings.ReplaceAll(br, "&amp;", "&")
|
||||
}
|
||||
|
||||
func getTotalDuration(delay ...time.Duration) time.Duration {
|
||||
var total time.Duration
|
||||
for _, d := range delay {
|
||||
total += d
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
30
relations.go
30
relations.go
@ -104,10 +104,9 @@ func (f Friends) Search(limit int, condFuncList ...func(friend *Friend) bool) (r
|
||||
|
||||
// 向slice的好友依次发送消息
|
||||
func (f Friends) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
||||
total := getTotalDuration(delay...)
|
||||
for _, friend := range f {
|
||||
if len(delay) != 0 {
|
||||
time.Sleep(delay[0])
|
||||
}
|
||||
time.Sleep(total)
|
||||
if err := friend.SendMsg(msg); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -117,10 +116,9 @@ func (f Friends) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
||||
|
||||
// 向slice的好友依次发送文本消息
|
||||
func (f Friends) SendText(text string, delay ...time.Duration) error {
|
||||
total := getTotalDuration(delay...)
|
||||
for _, friend := range f {
|
||||
if len(delay) != 0 {
|
||||
time.Sleep(delay[0])
|
||||
}
|
||||
time.Sleep(total)
|
||||
if err := friend.SendText(text); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -130,10 +128,9 @@ func (f Friends) SendText(text string, delay ...time.Duration) error {
|
||||
|
||||
// 向slice的好友依次发送图片消息
|
||||
func (f Friends) SendImage(file *os.File, delay ...time.Duration) error {
|
||||
total := getTotalDuration(delay...)
|
||||
for _, friend := range f {
|
||||
if len(delay) != 0 {
|
||||
time.Sleep(delay[0])
|
||||
}
|
||||
time.Sleep(total)
|
||||
if err := friend.SendImage(file); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -209,10 +206,7 @@ func (g Groups) Last() *Group {
|
||||
|
||||
// 向群组依次发送消息, 支持发送延迟
|
||||
func (g Groups) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
||||
var total time.Duration
|
||||
for _, d := range delay {
|
||||
total += d
|
||||
}
|
||||
total := getTotalDuration(delay...)
|
||||
for _, group := range g {
|
||||
time.Sleep(total)
|
||||
if err := group.SendMsg(msg); err != nil {
|
||||
@ -224,10 +218,7 @@ func (g Groups) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
||||
|
||||
// 向群组依次发送文本消息, 支持发送延迟
|
||||
func (g Groups) SendText(text string, delay ...time.Duration) error {
|
||||
var total time.Duration
|
||||
for _, d := range delay {
|
||||
total += d
|
||||
}
|
||||
total := getTotalDuration(delay...)
|
||||
for _, group := range g {
|
||||
time.Sleep(total)
|
||||
if err := group.SendText(text); err != nil {
|
||||
@ -239,10 +230,7 @@ func (g Groups) SendText(text string, delay ...time.Duration) error {
|
||||
|
||||
// 向群组依次发送图片消息, 支持发送延迟
|
||||
func (g Groups) SendImage(file *os.File, delay ...time.Duration) error {
|
||||
var total time.Duration
|
||||
for _, d := range delay {
|
||||
total += d
|
||||
}
|
||||
total := getTotalDuration(delay...)
|
||||
for _, group := range g {
|
||||
time.Sleep(total)
|
||||
if err := group.SendImage(file); err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user