-
Notifications
You must be signed in to change notification settings - Fork 19
/
set_contact_language.go
67 lines (56 loc) · 1.88 KB
/
set_contact_language.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
package actions
import (
"strings"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/flows/modifiers"
)
func init() {
registerType(TypeSetContactLanguage, func() flows.Action { return &SetContactLanguageAction{} })
}
// TypeSetContactLanguage is the type for the set contact Language action
const TypeSetContactLanguage string = "set_contact_language"
// SetContactLanguageAction can be used to update the name of the contact. The language is a localizable
// template and white space is trimmed from the final value. An empty string clears the language.
// A [event:contact_language_changed] event will be created with the corresponding value.
//
// {
// "uuid": "8eebd020-1af5-431c-b943-aa670fc74da9",
// "type": "set_contact_language",
// "language": "eng"
// }
//
// @action set_contact_language
type SetContactLanguageAction struct {
baseAction
universalAction
Language string `json:"language" engine:"evaluated"`
}
// NewSetContactLanguage creates a new set language action
func NewSetContactLanguage(uuid flows.ActionUUID, language string) *SetContactLanguageAction {
return &SetContactLanguageAction{
baseAction: newBaseAction(TypeSetContactLanguage, uuid),
Language: language,
}
}
// Execute runs this action
func (a *SetContactLanguageAction) Execute(run flows.Run, step flows.Step, logModifier flows.ModifierCallback, logEvent flows.EventCallback) error {
language, ok := run.EvaluateTemplate(a.Language, logEvent)
language = strings.TrimSpace(language)
if !ok {
return nil
}
// language must be empty or valid language code
lang := i18n.NilLanguage
var err error
if language != "" {
lang, err = i18n.ParseLanguage(language)
if err != nil {
logEvent(events.NewError(err))
return nil
}
}
a.applyModifier(run, modifiers.NewLanguage(lang), logModifier, logEvent)
return nil
}