-
Notifications
You must be signed in to change notification settings - Fork 10
/
tgbot-defaultoptions.go
131 lines (114 loc) · 3.26 KB
/
tgbot-defaultoptions.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
package tgbot
import "github.com/oleiade/reflections"
// DefaultOptionsBot represents the options that the bot will try to apply automatically
type DefaultOptionsBot struct {
DisableWebURL *bool
Selective *bool
OneTimeKeyboard *bool
CleanInitialUsername bool
AllowWithoutSlashInMention bool
LowerText bool
RecoverPanic bool
}
func (bot *TgBot) SetRecoverPanic(b bool) *TgBot {
bot.DefaultOptions.RecoverPanic = b
return bot
}
func (bot *TgBot) SetLowerText(b bool) *TgBot {
bot.DefaultOptions.LowerText = b
return bot
}
// DefaultDisableWebpagePreview ...
func (bot *TgBot) DefaultDisableWebpagePreview(b bool) *TgBot {
bot.DefaultOptions.DisableWebURL = &b
return bot
}
// DefaultSelective ...
func (bot *TgBot) DefaultSelective(b bool) *TgBot {
bot.DefaultOptions.Selective = &b
return bot
}
// DefaultOneTimeKeyboard ...
func (bot *TgBot) DefaultOneTimeKeyboard(b bool) *TgBot {
bot.DefaultOptions.OneTimeKeyboard = &b
return bot
}
// DefaultCleanInitialUsername ...
func (bot *TgBot) DefaultCleanInitialUsername(b bool) *TgBot {
bot.DefaultOptions.CleanInitialUsername = b
return bot
}
// DefaultAllowWithoutSlashInMention ...
func (bot *TgBot) DefaultAllowWithoutSlashInMention(b bool) *TgBot {
bot.DefaultOptions.AllowWithoutSlashInMention = b
return bot
}
func hookDisableWebpage(payload interface{}, nv *bool) {
if nv != nil {
has, _ := reflections.HasField(payload, "DisableWebPagePreview")
if has {
value, _ := reflections.GetField(payload, "DisableWebPagePreview")
bvalue := value.(*bool)
if bvalue == nil {
reflections.SetField(payload, "DisableWebPagePreview", nv)
}
}
}
}
func hookReplyToMessageID(payload interface{}, nv *bool) {
if nv != nil {
has, _ := reflections.HasField(payload, "ReplyToMessageID")
if has {
value, _ := reflections.GetField(payload, "ReplyToMessageID")
bvalue := value.(*int)
if bvalue == nil {
reflections.SetField(payload, "ReplyToMessageID", nv)
}
}
}
}
func hookSelective(payload interface{}, nv *bool) {
if nv != nil {
has, _ := reflections.HasField(payload, "Selective")
if has {
value, _ := reflections.GetField(payload, "Selective")
bvalue := value.(*bool)
if bvalue == nil {
reflections.SetField(payload, "Selective", nv)
}
}
}
}
func hookOneTimeKeyboard(payload interface{}, nv *bool) {
if nv != nil {
has, _ := reflections.HasField(payload, "OneTimeKeyboard")
if has {
value, _ := reflections.GetField(payload, "OneTimeKeyboard")
bvalue := value.(*bool)
if bvalue == nil {
reflections.SetField(payload, "OneTimeKeyboard", nv)
}
}
}
}
// HookPayload I hate reflection, sorry for that <3
func hookPayload(payload interface{}, opts DefaultOptionsBot) {
hookDisableWebpage(payload, opts.DisableWebURL)
// HookReplyToMessageID(payload, opts.ReplyToMessageID)
has, _ := reflections.HasField(payload, "ReplyMarkup")
if has {
keyint, _ := reflections.GetField(payload, "ReplyMarkup")
switch val := keyint.(type) {
case *ForceReply, *ReplyKeyboardHide:
if val != nil {
hookSelective(val, opts.Selective)
}
case *ReplyKeyboardMarkup:
if val != nil {
hookOneTimeKeyboard(val, opts.OneTimeKeyboard)
hookSelective(val, opts.Selective)
}
default:
}
}
}