-
Notifications
You must be signed in to change notification settings - Fork 3
/
room_service.go
78 lines (62 loc) · 1.79 KB
/
room_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package service
import (
"log"
"time"
"github.com/chasonnchen/wechat_bot/dao"
"github.com/chasonnchen/wechat_bot/entity"
"github.com/wechaty/go-wechaty/wechaty-puppet/schemas"
"github.com/wechaty/go-wechaty/wechaty/interface"
"github.com/wechaty/go-wechaty/wechaty/user"
)
var (
roomServiceInstance = &RoomService{}
)
func NewRoomService() *RoomService {
return roomServiceInstance
}
type RoomService struct {
InviteConf []entity.SkillInviteEntity
}
func (r *RoomService) AutoInvite(contact _interface.IContact, message *user.Message, hello string) {
// 1. 检查获取文本消息
var msgText string
if message.Type() == schemas.MessageTypeText {
msgText = message.Text()
}
if len(hello) > 0 {
msgText = hello
}
if len(msgText) < 1 {
return
}
// 2. 开始匹配是否触发邀请
for _, invite := range r.InviteConf {
//if strings.Contains(msgText, invite.Keyword) {
if msgText == invite.Keyword {
// 命中关键字,先回复一句提示
NewGlobleService().GetBot().Contact().Load(contact.ID()).Say("群暗号正确,已发起邀请,(可能需要群管理员同意,请您耐心等待~)")
// 发送邀请, 并打日志
NewGlobleService().GetBot().Room().Load(invite.ContactId).Add(contact)
// TODO 加入消息队列,加群成功后 定时删除好友?否则到上限之后功能就废了
// 这里continue有可能命中多个群的邀请, 在配置上尽量避免命中多个
break
}
}
}
func (r *RoomService) init() {
r.loadInviteConf()
go func() {
for {
select {
case <-time.After(time.Second * 60):
r.loadInviteConf()
}
}
}()
}
func (r *RoomService) loadInviteConf() {
var inviteList []entity.SkillInviteEntity
dao.Webot().Find(&inviteList)
log.Printf("room invite conf is %#v", inviteList)
r.InviteConf = inviteList
}