-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
323 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package gcloudcx | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/gildas/go-core" | ||
"github.com/gildas/go-errors" | ||
) | ||
|
||
type OpenMessageEvent interface { | ||
core.TypeCarrier | ||
} | ||
|
||
var openMessageEventRegistry = core.TypeRegistry{} | ||
|
||
func UnmarshalOpenMessageEvent(payload []byte) (OpenMessageEvent, error) { | ||
message, err := openMessageEventRegistry.UnmarshalJSON(payload, "eventType") | ||
if err == nil { | ||
return message.(OpenMessageEvent), nil | ||
} | ||
if strings.HasPrefix(err.Error(), "Missing JSON Property") { | ||
return nil, errors.JSONUnmarshalError.Wrap(errors.ArgumentMissing.With("type")) | ||
} | ||
if strings.HasPrefix(err.Error(), "Unsupported Type") { | ||
return nil, errors.JSONUnmarshalError.Wrap(errors.InvalidType.With(strings.TrimSuffix(strings.TrimPrefix(err.Error(), `Unsupported Type "`), `"`))) | ||
} | ||
if errors.Is(err, errors.JSONUnmarshalError) { | ||
return nil, err | ||
} | ||
return nil, errors.JSONUnmarshalError.Wrap(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package gcloudcx | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/gildas/go-errors" | ||
) | ||
|
||
// OpenMessageTypingEvent is a typing event sent or received by the Open Messaging API | ||
type OpenMessageTypingEvent struct { | ||
IsTyping bool `json:"-"` | ||
Duration time.Duration `json:"-"` | ||
} | ||
|
||
func init() { | ||
openMessageEventRegistry.Add(OpenMessageTypingEvent{}) | ||
} | ||
|
||
// GetType returns the type of this event | ||
// | ||
// implements core.TypeCarrier | ||
func (event OpenMessageTypingEvent) GetType() string { | ||
return "Typing" | ||
} | ||
|
||
// MarshalJSON marshals this into JSON | ||
// | ||
// implements json.Marshaler | ||
func (event OpenMessageTypingEvent) MarshalJSON() (data []byte, err error) { | ||
if !event.IsTyping || event.Duration > 0 { | ||
type TypingInfo struct { | ||
Type string `json:"type"` | ||
Duration int `json:"duration"` | ||
} | ||
newTypingInfo := func(isTyping bool, duration time.Duration) TypingInfo { | ||
if !isTyping { | ||
return TypingInfo{ | ||
Type: "On", | ||
Duration: int(duration.Milliseconds()), | ||
} | ||
} | ||
return TypingInfo{ | ||
Type: "Off", | ||
Duration: int(duration.Milliseconds()), | ||
} | ||
} | ||
data, err = json.Marshal(struct { | ||
Type string `json:"eventType"` | ||
Typing TypingInfo `json:"typing"` | ||
}{ | ||
Type: event.GetType(), | ||
Typing: newTypingInfo(event.IsTyping, event.Duration), | ||
}) | ||
} else { | ||
data, err = json.Marshal(struct { | ||
Type string `json:"eventType"` | ||
}{ | ||
Type: event.GetType(), | ||
}) | ||
} | ||
return data, errors.JSONMarshalError.Wrap(err) | ||
} | ||
|
||
// UnmarshalJSON unmarshals JSON into this | ||
// | ||
// implements json.Unmarshaler | ||
func (event *OpenMessageTypingEvent) UnmarshalJSON(payload []byte) (err error) { | ||
type surrogate OpenMessageTypingEvent | ||
var inner struct { | ||
surrogate | ||
Type string `json:"eventType"` | ||
Typing *struct { | ||
Type string `json:"type"` | ||
Duration int `json:"duration"` | ||
} `json:"typing"` | ||
} | ||
if err = json.Unmarshal(payload, &inner); errors.Is(err, errors.JSONUnmarshalError) { | ||
return err | ||
} else if err != nil { | ||
return errors.JSONUnmarshalError.Wrap(err) | ||
} | ||
*event = OpenMessageTypingEvent(inner.surrogate) | ||
|
||
if inner.Typing != nil { | ||
event.IsTyping = inner.Typing.Type == "On" | ||
event.Duration = time.Duration(inner.Typing.Duration) * time.Millisecond | ||
} else { | ||
event.IsTyping = true | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package gcloudcx | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/gildas/go-errors" | ||
) | ||
|
||
// OpenMessageText is a text message sent or received by the Open Messaging API | ||
// | ||
// See https://developer.genesys.cloud/commdigital/digital/openmessaging/inboundEventMessages | ||
type OpenMessageEvents struct { | ||
ID string `json:"id,omitempty"` // Can be anything | ||
Channel *OpenMessageChannel `json:"channel"` | ||
Direction string `json:"direction,omitempty"` // Can be "Inbound" or "Outbound" | ||
Events []OpenMessageEvent `json:"events"` | ||
Metadata map[string]string `json:"metadata,omitempty"` | ||
} | ||
|
||
// init initializes this type | ||
func init() { | ||
openMessageRegistry.Add(OpenMessageEvents{}) | ||
} | ||
|
||
// GetType returns the type of this event | ||
// | ||
// implements core.TypeCarrier | ||
func (message OpenMessageEvents) GetType() string { | ||
return "Event" | ||
} | ||
|
||
// GetID gets the identifier of this | ||
// | ||
// implements OpenMessage | ||
func (message OpenMessageEvents) GetID() string { | ||
return message.ID | ||
} | ||
|
||
// MarshalJSON marshals this into JSON | ||
// | ||
// implements json.Marshaler | ||
func (message OpenMessageEvents) MarshalJSON() ([]byte, error) { | ||
type surrogate OpenMessageEvents | ||
|
||
data, err := json.Marshal(struct { | ||
surrogate | ||
Type string `json:"type"` | ||
}{ | ||
surrogate: surrogate(message), | ||
Type: message.GetType(), | ||
}) | ||
return data, errors.JSONMarshalError.Wrap(err) | ||
} | ||
|
||
// UnmarshalJSON unmarshals JSON into this | ||
// | ||
// implements json.Unmarshaler | ||
func (message *OpenMessageEvents) UnmarshalJSON(payload []byte) (err error) { | ||
type surrogate OpenMessageEvents | ||
var inner struct { | ||
surrogate | ||
Type string `json:"type"` | ||
Events []json.RawMessage `json:"events"` | ||
} | ||
if err = json.Unmarshal(payload, &inner); errors.Is(err, errors.JSONUnmarshalError) { | ||
return err | ||
} else if err != nil { | ||
return errors.JSONUnmarshalError.Wrap(err) | ||
} | ||
*message = OpenMessageEvents(inner.surrogate) | ||
|
||
message.Events = make([]OpenMessageEvent, 0, len(inner.Events)) | ||
for _, raw := range inner.Events { | ||
event, err := UnmarshalOpenMessageEvent(raw) | ||
if err != nil { | ||
return err | ||
} | ||
message.Events = append(message.Events, event) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"id": "6ffd815bca1570e46251fcc71c103837", | ||
"channel": { | ||
"id": "1af69355-f1b0-477e-8ed9-66baff370209", | ||
"platform": "Open", | ||
"type": "Private", | ||
"to": { | ||
"id": "abcdef12345" | ||
}, | ||
"from": { | ||
"nickname": "TEST-GO-PURECLOUD", | ||
"id": "1af69355-f1b0-477e-8ed9-66baff370209", | ||
"idType": "Opaque" | ||
}, | ||
"time": "2023-09-19T08:25:16.925Z", | ||
"messageId": "6ffd815bca1570e46251fcc71c103837" | ||
}, | ||
"type": "Event", | ||
"events": [ | ||
{ | ||
"eventType": "Typing", | ||
"typing": { | ||
"type": "On", | ||
"duration": 5000 | ||
} | ||
} | ||
], | ||
"direction": "Outbound" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"channel": { | ||
"platform": "Open", | ||
"type": "Private", | ||
"from": { | ||
"id": "abcdef12345", | ||
"idType": "Email", | ||
"firstName": "Bob", | ||
"lastName": "Minion", | ||
"nickname": "Bobby" | ||
}, | ||
"time": "2021-04-09T04:43:33Z" | ||
}, | ||
"type": "Event", | ||
"events": [ | ||
{ "eventType": "Typing" } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters