This repository has been archived by the owner on Feb 14, 2019. It is now read-only.
/
subscribe.go
84 lines (71 loc) · 1.9 KB
/
subscribe.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
79
80
81
82
83
84
package plugin
import (
"log"
"strconv"
"strings"
"github.com/jqs7/Jqs7Bot/conf"
"github.com/jqs7/bb"
)
type Subscribe struct{ Default }
func (s *Subscribe) Run() {
userIDStr := strconv.Itoa(s.Message.From.ID)
isSubscribe, _ := strconv.ParseBool(conf.Redis.HGet("tgSubscribe",
userIDStr).Val())
if !s.isAuthed() {
s.sendQuestion()
return
}
if isSubscribe {
s.NewMessage(s.Message.From.ID,
"已经订阅过,就不要重复订阅啦😘").Send()
} else {
conf.Redis.HSet("tgSubscribe", userIDStr, strconv.FormatBool(true))
s.NewMessage(s.Message.From.ID, "订阅成功\n以后奴家知道新的群组的话,会第一时间告诉你哟😊").Send()
}
}
type UnSubscribe struct{ bb.Base }
func (u *UnSubscribe) Run() {
userIDStr := strconv.Itoa(u.Message.From.ID)
rc := conf.Redis
if rc.HExists("tgSubscribe", userIDStr).Val() {
rc.HDel("tgSubscribe", userIDStr)
u.NewMessage(u.Message.From.ID,
"好伤心,退订了就不能愉快的玩耍了呢😭").Send()
return
}
u.NewMessage(u.Message.From.ID,
"你都还没订阅,让人家怎么退订嘛!o(≧口≦)o").Send()
return
}
type Broadcast struct{ Default }
func (b *Broadcast) Run() {
if b.isMaster() {
if len(b.Args) == 1 && b.FromPrivate {
b.NewMessage(b.ChatID,
"Send me the Broadcast (^o^)ノ").Send()
b.setStatus("broadcast")
return
}
if len(b.Args) >= 2 {
text := strings.Join(b.Args[1:], " ")
b.bc(text)
}
}
}
func (b *Default) bc(text string) {
if b.isMaster() &&
conf.Redis.Exists("tgSubscribe").Val() {
subStates := conf.Redis.HGetAllMap("tgSubscribe").Val()
counter := 0
for k, v := range subStates {
chatid, _ := strconv.Atoi(k)
subState, _ := strconv.ParseBool(v)
if subState && chatid > 0 {
log.Printf("sending broadcast to %d ... \n", chatid)
go b.NewMessage(chatid, text).Send()
counter++
}
}
log.Printf("%d broadcasts sent", counter)
}
}