This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
/
name.go
53 lines (42 loc) 路 1.49 KB
/
name.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
package modules
import (
"fmt"
"strings"
"github.com/olivia-ai/olivia/language"
"github.com/olivia-ai/olivia/user"
"github.com/olivia-ai/olivia/util"
)
var (
// NameGetterTag is the intent tag for its module
NameGetterTag = "name getter"
// NameSetterTag is the intent tag for its module
NameSetterTag = "name setter"
)
// NameGetterReplacer replaces the pattern contained inside the response by the user's name.
// See modules/modules.go#Module.Replacer() for more details.
func NameGetterReplacer(locale, _, response, token string) (string, string) {
name := user.GetUserInformation(token).Name
if strings.TrimSpace(name) == "" {
responseTag := "don't know name"
return responseTag, util.GetMessage(locale, responseTag)
}
return NameGetterTag, fmt.Sprintf(response, name)
}
// NameSetterReplacer gets the name specified in the message and save it in the user's information.
// See modules/modules.go#Module.Replacer() for more details.
func NameSetterReplacer(locale, entry, response, token string) (string, string) {
name := language.FindName(entry)
// If there is no name in the entry string
if name == "" {
responseTag := "no name"
return responseTag, util.GetMessage(locale, responseTag)
}
// Capitalize the name
name = strings.Title(name)
// Change the name inside the user information
user.ChangeUserInformation(token, func(information user.Information) user.Information {
information.Name = name
return information
})
return NameSetterTag, fmt.Sprintf(response, name)
}