This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
179 lines (155 loc) · 4.17 KB
/
handler.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SensitiveCleaner - handler
// 2021-02-04 13:43
// Benny <[email protected]>
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
"io/ioutil"
"os"
"regexp"
"strconv"
"time"
)
var ctx = context.Background()
var rdb = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:6379", redisHost),
Password: "", // no password set
DB: 1, // use default DB
})
func messageHandler(m *tb.Message) {
if m.ReplyTo != nil && m.ReplyTo.Text == "Choose your time-to-live:" {
if !permissionCheck(m) {
return
}
data := readJSON()
re := regexp.MustCompile(`\d+`)
result := re.FindAllString(m.Text, -1)[0]
i, _ := strconv.Atoi(result)
strID := strconv.FormatInt(m.Chat.ID, 10)
data[strID] = int64(i * 3600)
saveJSON(data)
_, _ = b.Send(m.Chat, "Oorah!", &tb.ReplyMarkup{ReplyKeyboardRemove: true})
return
}
log.Infof("Receiving message ID %d at %s", m.ID, time.Now())
// check group
if m.Private() {
_, _ = b.Send(m.Chat, "Add me to a group or channel, and I'll start working.")
return
}
rdb.HSet(ctx, fmt.Sprintf("%v", m.Chat.ID), m.ID, time.Now().Unix())
}
func onJoinHandler(m *tb.Message) {
rdb.HSet(ctx, fmt.Sprintf("%v", m.Chat.ID), m.ID, time.Now().Unix())
_ = b.Notify(m.Chat, tb.Typing)
_, _ = b.Send(m.Chat, "You need to promote me to admin first. And then optionally call /settings to set TTL."+
"Default TTL is 48h.")
}
func startHandler(m *tb.Message) {
rdb.HSet(ctx, fmt.Sprintf("%v", m.Chat.ID), m.ID, time.Now().Unix())
_ = b.Notify(m.Chat, tb.Typing)
_, _ = b.Send(m.Chat, "Welcome! I can help deleting group/channel messages. Please add me to your group as admin."+
"See /settings for more.")
}
func helpHandler(m *tb.Message) {
rdb.HSet(ctx, fmt.Sprintf("%v", m.Chat.ID), m.ID, time.Now().Unix())
_ = b.Notify(m.Chat, tb.Typing)
helpMsg := `A bot that will help you automatically delete group messages.
You need to:
1. add me to a group/channel as admin.
2. optionally set TTL, default is 48h`
if m.Private() {
_, _ = b.Send(m.Chat, helpMsg)
return
}
data := readJSON()
strID := strconv.FormatInt(m.Chat.ID, 10)
value := data[strID] / 3600
var usable = false
admins, _ := b.AdminsOf(m.Chat)
for _, admin := range admins {
if admin.User.ID == b.Me.ID {
usable = true
}
}
if usable || m.Chat.Type == "channel" {
_, _ = b.Send(m.Chat, fmt.Sprintf("I'm working, your TTL is %dh", value))
} else {
_, _ = b.Send(m.Chat, helpMsg)
}
}
func settingsHandler(m *tb.Message) {
rdb.HSet(ctx, fmt.Sprintf("%v", m.Chat.ID), m.ID, time.Now().Unix())
if !permissionCheck(m) {
return
}
var menu = &tb.ReplyMarkup{ForceReply: true, ResizeReplyKeyboard: true}
var rows []tb.Row
var btns []tb.Btn
var count = 1
for i := 1; i <= 48; i += 2 {
// 1 3 5 7
btn := menu.Text(fmt.Sprintf("%dh", i))
btns = append(btns, btn)
if count > 5 {
rows = append(rows, menu.Row(btns...))
btns = []tb.Btn{}
count = 1
} else {
count += 1
}
}
menu.Reply(rows...)
_, _ = b.Send(m.Chat, "Choose your time-to-live:", menu)
}
func permissionCheck(m *tb.Message) bool {
var botAdmin = false
var senderAdmin = false
if m.Chat.Type == "channel" {
botAdmin = true
senderAdmin = true
} else {
admins, _ := b.AdminsOf(m.Chat)
var adminMap = make(map[int]int)
for _, admin := range admins {
adminMap[admin.User.ID] = 1
}
if _, ok := adminMap[m.Sender.ID]; ok {
senderAdmin = true
}
if _, ok := adminMap[b.Me.ID]; ok {
botAdmin = true
}
}
_ = b.Notify(m.Chat, tb.Typing)
if !botAdmin {
_, _ = b.Send(m.Chat, "Please promote me to admin.")
return false
} else if !senderAdmin {
_, _ = b.Send(m.Chat, "Are you admin? ")
return false
} else {
return true
}
}
func readJSON() userConfig {
log.Debugf("Read json file...")
jsonFile, _ := os.Open("settings.json")
decoder := json.NewDecoder(jsonFile)
var config = make(userConfig)
err = decoder.Decode(&config)
_ = jsonFile.Close()
return config
}
func saveJSON(current userConfig) {
e, _ := json.MarshalIndent(current, "", "\t")
err := ioutil.WriteFile("settings.json", e, 0644)
if err != nil {
log.Errorf("Write json failed %v", err)
}
}