-
Notifications
You must be signed in to change notification settings - Fork 25
/
bot_profile.go
99 lines (87 loc) · 2.49 KB
/
bot_profile.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
package botsfw
import (
"github.com/bots-go-framework/bots-fw-store/botsfwmodels"
"github.com/strongo/i18n"
"strings"
)
type BotProfile interface {
ID() string
Router() *WebhooksRouter
DefaultLocale() i18n.Locale
SupportedLocales() []i18n.Locale
NewBotChatData() botsfwmodels.BotChatData
NewBotUserData() botsfwmodels.BotUserData
NewAppUserData() botsfwmodels.AppUserData // TODO: Can we get rit of it and instead use GetAppUserByID/CreateAppUser?
}
var _ BotProfile = (*botProfile)(nil)
type botProfile struct {
id string
defaultLocale i18n.Locale
supportedLocales []i18n.Locale
newBotChatData func() botsfwmodels.BotChatData
newBotUserData func() botsfwmodels.BotUserData
newAppUserData func() botsfwmodels.AppUserData
getAppUserByID AppUserGetter
router *WebhooksRouter
}
func (v *botProfile) ID() string {
return v.id
}
func (v *botProfile) Router() *WebhooksRouter {
return v.router
}
func (v *botProfile) DefaultLocale() i18n.Locale {
return v.defaultLocale
}
func (v *botProfile) SupportedLocales() []i18n.Locale {
return v.supportedLocales[:]
}
func (v *botProfile) NewBotChatData() botsfwmodels.BotChatData {
return v.newBotChatData()
}
func (v *botProfile) NewBotUserData() botsfwmodels.BotUserData {
return v.newBotUserData()
}
func (v *botProfile) NewAppUserData() botsfwmodels.AppUserData {
return v.newAppUserData()
}
func NewBotProfile(
id string,
router *WebhooksRouter,
newBotChatData func() botsfwmodels.BotChatData,
newBotUserData func() botsfwmodels.BotUserData,
newAppUserData func() botsfwmodels.AppUserData,
getAppUserByID AppUserGetter,
defaultLocale i18n.Locale,
supportedLocales []i18n.Locale,
) BotProfile {
if strings.TrimSpace(id) == "" {
panic("missing required parameter: id")
}
if newBotChatData == nil {
panic("missing required parameter: newBotChatData")
}
if newBotUserData == nil {
panic("missing required parameter: newBotUserData")
}
var defaultLocaleInSupportedLocales bool
for _, locale := range supportedLocales {
if locale.Code5 == defaultLocale.Code5 {
defaultLocaleInSupportedLocales = true
break
}
}
if !defaultLocaleInSupportedLocales {
supportedLocales = append(supportedLocales, defaultLocale)
}
return &botProfile{
id: id,
router: router,
defaultLocale: defaultLocale,
supportedLocales: supportedLocales,
newBotChatData: newBotChatData,
newBotUserData: newBotUserData,
newAppUserData: newAppUserData,
getAppUserByID: getAppUserByID,
}
}