forked from jqs7/Jqs7Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.go
91 lines (79 loc) · 2.51 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
85
86
87
88
89
90
91
package main
import (
"log"
"strconv"
"github.com/Syfaro/telegram-bot-api"
)
func (u *Updater) Subscribe() {
chatIDStr := strconv.Itoa(u.update.Message.Chat.ID)
isSubscribe, _ := strconv.ParseBool(u.redis.HGet("tgSubscribe",
chatIDStr).Val())
if u.update.Message.Chat.ID < 0 {
msg := tgbotapi.NewMessage(u.update.Message.Chat.ID,
"群组订阅功能已取消,需要订阅功能的话,请私聊奴家呢o( ̄ˇ ̄)o")
u.bot.SendMessage(msg)
return
}
if !u.IsAuthed() {
u.SendQuestion()
return
}
if isSubscribe {
msg := tgbotapi.NewMessage(u.update.Message.Chat.ID,
"已经订阅过,就不要重复订阅啦😘")
u.bot.SendMessage(msg)
} else {
u.redis.HSet("tgSubscribe", chatIDStr, strconv.FormatBool(true))
u.redis.HIncrBy("tgSubscribeTimes", chatIDStr, 1)
msg := tgbotapi.NewMessage(u.update.Message.Chat.ID,
"订阅成功\n以后奴家知道新的群组的话,会第一时间告诉你哟😊\n"+
"(订阅仅对当前会话有效)")
u.bot.SendMessage(msg)
}
}
func (u *Updater) UnSubscribe() {
chatIDStr := strconv.Itoa(u.update.Message.Chat.ID)
var msg tgbotapi.MessageConfig
if u.redis.HExists("tgSubscribe", chatIDStr).Val() {
u.redis.HDel("tgSubscribe", chatIDStr)
times, _ := u.redis.HIncrBy("tgSubscribeTimes", chatIDStr, 1).Result()
if times > 5 {
msg = tgbotapi.NewMessage(u.update.Message.Chat.ID,
"订了退,退了订,你烦不烦嘛!!!⊂彡☆))∀`)`")
u.redis.HDel("tgSubscribeTimes", chatIDStr)
} else {
msg = tgbotapi.NewMessage(u.update.Message.Chat.ID,
"好伤心,退订了就不能愉快的玩耍了呢😭")
}
} else {
msg = tgbotapi.NewMessage(u.update.Message.Chat.ID,
"你都还没订阅,让人家怎么退订嘛!o(≧口≦)o")
}
u.bot.SendMessage(msg)
}
func (u *Updater) PreBroadcast() {
master, _ := u.conf.Get("master")
if u.update.Message.Chat.UserName == master {
u.BotReply("Send me the Broadcast (^o^)ノ")
u.SetStatus("broadcast")
}
}
func (u *Updater) Broadcast(msgText string) {
master, _ := u.conf.Get("master")
if u.update.Message.Chat.UserName == master &&
u.redis.Exists("tgSubscribe").Val() {
subStates := u.redis.HGetAllMap("tgSubscribe").Val()
for k, v := range subStates {
chatid, _ := strconv.Atoi(k)
subState, _ := strconv.ParseBool(v)
if subState && chatid > 0 {
log.Printf("sending boardcast to %d ...", chatid)
msg := tgbotapi.NewMessage(chatid, msgText)
go func(k string) {
u.bot.SendMessage(msg)
log.Printf("%s --- done", k)
}(k)
}
}
}
}