-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwelcome.go
153 lines (135 loc) · 3.81 KB
/
welcome.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
package main
/*
- Show welcome message on join with inline-button.
- Set lowest rights
- If button clicked, set guest rights, remove welcome message.
*/
import (
"fmt"
"log"
"log/slog"
"strconv"
"strings"
"time"
tgbotapi "github.com/OvyFlash/telegram-bot-api"
)
type WelcomeMessage struct {
ID int
UserID int64
ChatID int64
Timestamp time.Time
}
func welcomeNewUser(update tgbotapi.Update, user tgbotapi.User) {
var chatid int64
if update.Message == nil {
chatid = update.ChatMember.Chat.ID
} else {
chatid = update.Message.Chat.ID
}
// Greet new user
welcomeMessage := strings.Replace(MainConfig.WelcomeMessage, "{namelink}", getNameLink(user), -1)
msg := tgbotapi.NewMessage(chatid, welcomeMessage)
//Add Inline callback
userid := strconv.Itoa(int(user.ID))
callbackData := "{\"command\": \"upgrade_rights\", \"data\": \"" + userid + "\"}"
var keyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData(MainConfig.WelcomeButtonMessage, callbackData),
),
)
msg.LinkPreviewOptions.IsDisabled = true
msg.ParseMode = "HTML"
msg.ReplyMarkup = keyboard
if emulate {
log.Println("Welcome for " + user.UserName)
} else {
messageSent, _ := bot.Send(msg)
welcomeSent(messageSent, user.ID)
slog.Debug(fmt.Sprintf("Welcome sent %d, user %s(%d)", messageSent.MessageID, user.UserName, user.ID))
saveCache()
}
}
func setInitialRights(update tgbotapi.Update, user tgbotapi.User) {
slog.Info(fmt.Sprintf("Setting rights for user: %s(%d)", user.UserName, user.ID))
//Set user rights to read-only initially
if emulate {
return
}
var chatid int64
if update.Message == nil {
chatid = update.ChatMember.Chat.ID
} else {
chatid = update.Message.Chat.ID
}
initialRights := tgbotapi.ChatPermissions{
CanSendMessages: false,
}
config := tgbotapi.RestrictChatMemberConfig{
ChatMemberConfig: tgbotapi.ChatMemberConfig{
ChatConfig: tgbotapi.ChatConfig{
ChatID: chatid,
},
UserID: user.ID,
},
Permissions: &initialRights,
}
_, err := bot.Request(config)
if err != nil {
log.Print(err)
}
}
func upgradeUserRights(chatID int64, userid int64) {
// Logic to upgrade user rights
// Giving the user the ability to send messages
defaultRights := tgbotapi.ChatPermissions{
CanSendMessages: true,
}
defaultRights.SetCanSendMediaMessages(true)
config := tgbotapi.RestrictChatMemberConfig{
ChatMemberConfig: tgbotapi.ChatMemberConfig{
ChatConfig: tgbotapi.ChatConfig{
ChatID: chatID,
},
UserID: userid,
},
UseIndependentChatPermissions: true,
Permissions: &defaultRights,
}
bot.Send(config)
clearCachedUser(userid)
clearDeleteListByUser(userid)
}
func answerCallbackQuery(callbackQueryID string, text string) {
callbackConfig := tgbotapi.NewCallback(callbackQueryID, text)
bot.Send(callbackConfig)
}
func welcomeSent(message tgbotapi.Message, userID int64) {
// Add message with timestamp + 2 hours
cache.DeleteList = append(cache.DeleteList, WelcomeMessage{
ID: message.MessageID,
UserID: userID,
ChatID: message.Chat.ID,
Timestamp: time.Now().UTC().Add(time.Hour * 2),
})
}
func CleanUpWelcome() int {
// Remove entries older than now
now := time.Now().UTC()
counter := 0
if len(cache.DeleteList) > 0 {
for id := 0; id < len(cache.DeleteList); id++ {
if cache.DeleteList[id].Timestamp.Before(now) {
slog.Info(fmt.Sprintf("Deleting message id %d", cache.DeleteList[id].ID))
deleteMessage(cache.DeleteList[id].ChatID, cache.DeleteList[id].ID)
kickChatMember(cache.DeleteList[id].ChatID, cache.DeleteList[id].UserID)
clearCachedUser(cache.DeleteList[id].UserID)
//clearDeleteListByUser(message.UserID)
cache.DeleteList = append(cache.DeleteList[:id], cache.DeleteList[id+1:]...)
id--
counter++
}
}
saveCache()
}
return counter
}