Skip to content

Commit 46e9e35

Browse files
committed
feat: chatVars
SetVar(key string, value string) GetVar(key string) string DelVar(key string) HasChangedVars() bool
1 parent 3c34561 commit 46e9e35

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

botsfwmodels/chat_base_data.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type ChatBaseData struct {
2424
BotBaseData
2525
chatState
2626
chatSettings
27+
chatVars
2728

2829
// AppUserIntIDs is kept for legacy reasons
2930
// Deprecated: replace with `AppUserIDs []string`

botsfwmodels/chat_data.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ type BotChatData interface {
8686
// PushStepToAwaitingReplyTo pushes step to awaiting reply to
8787
PushStepToAwaitingReplyTo(code string)
8888
//GetGaClientID() string
89+
90+
SetVar(key string, value string)
91+
GetVar(key string) string
92+
DelVar(key string)
93+
HasChangedVars() bool
8994
}
9095

9196
// NewChatID create a new bot chat ID, returns string

botsfwmodels/chat_vars.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package botsfwmodels
2+
3+
import (
4+
"github.com/strongo/slice"
5+
"slices"
6+
)
7+
8+
type chatVars struct {
9+
Vars map[string]string `dalgo:"vars,omitempty" firestore:"vars,omitempty"`
10+
changed []string
11+
deleted []string
12+
}
13+
14+
// GetVar returns a chat variable
15+
func (v *chatVars) GetVar(key string) string {
16+
if v.Vars == nil {
17+
return ""
18+
}
19+
return v.Vars[key]
20+
}
21+
22+
// SetVar sets a chat variable
23+
func (v *chatVars) SetVar(key, value string) {
24+
if v.Vars == nil {
25+
v.Vars = make(map[string]string)
26+
} else if v.Vars[key] == value {
27+
return
28+
}
29+
v.Vars[key] = value
30+
slice.RemoveInPlaceByValue(v.deleted, key)
31+
if !slices.Contains(v.changed, key) {
32+
v.changed = append(v.changed, key)
33+
}
34+
}
35+
36+
// DelVar deletes a chat variable
37+
func (v *chatVars) DelVar(key string) {
38+
if v.Vars == nil {
39+
return
40+
}
41+
if _, ok := v.Vars[key]; !ok {
42+
return
43+
}
44+
delete(v.Vars, key)
45+
v.changed = slice.RemoveInPlaceByValue(v.changed, key)
46+
if !slices.Contains(v.deleted, key) {
47+
v.deleted = append(v.deleted, key)
48+
}
49+
}
50+
51+
// HasChangedVars returns true if vars have been changed
52+
func (v *chatVars) HasChangedVars() bool {
53+
return len(v.changed) > 0 || len(v.deleted) > 0
54+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ module github.com/bots-go-framework/bots-fw-store
33
go 1.22
44

55
require github.com/strongo/validation v0.0.7
6+
7+
require github.com/strongo/slice v0.3.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
github.com/strongo/slice v0.3.1 h1:VWkyYBgcVJn6Hs7wYhL9Vxwgb7V3zQAUFTBV9wo5lc4=
2+
github.com/strongo/slice v0.3.1/go.mod h1:B5ODKCkl0rp2oiG0UBqkN1cCOrSCU2cUuhqCM1sC8r4=
13
github.com/strongo/validation v0.0.7 h1:gs6YkwPsYtVsepQaQOB+ZF+T0Gu5+nk4ZMND8F85e+U=
24
github.com/strongo/validation v0.0.7/go.mod h1:YUwoPEItLJd/Bc9X1OCUm03ofhvm3kwZvuihU7/jz58=

0 commit comments

Comments
 (0)