This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
forked from jazibjohar/intercom-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
102 lines (84 loc) · 3.14 KB
/
message.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
100
101
102
package intercom
import (
"fmt"
"strings"
)
// MessageService handles interactions with the API through an MessageRepository.
type MessageService struct {
Repository MessageRepository
}
// MessageTemplate determines the template used for email messages to Users or Contacts (plain or personal)
type MessageTemplate int
const (
NO_TEMPLATE MessageTemplate = iota
PERSONAL_TEMPLATE
PLAIN_TEMPLATE
)
var templates = [...]string{
"",
"personal",
"plain",
}
func (template MessageTemplate) String() string {
return templates[template]
}
func (template *MessageTemplate) UnmarshalJSON(b []byte) error {
str := strings.Trim(string(b), `"`)
switch {
case str == "personal":
*template = PERSONAL_TEMPLATE
case str == "plain":
*template = PLAIN_TEMPLATE
default:
*template = NO_TEMPLATE
}
return nil
}
// MessageRequest represents a Message to be sent through Intercom from/to an Admin, User, or Contact.
type MessageRequest struct {
MessageType string `json:"message_type,omitempty"`
Subject string `json:"subject,omitempty"`
Body string `json:"body,omitempty"`
Template string `json:"template,omitempty"`
From MessageAddress `json:"from,omitempty"`
To MessageAddress `json:"to,omitempty"`
}
// MessageResponse represents a Message to be sent through Intercom from/to an Admin, User, or Contact.
type MessageResponse struct {
MessageType string `json:"message_type,omitempty"`
ID string `json:"id"`
CreatedAt int64 `json:"created_at,omitempty"`
Owner MessageAddress `json:"owner,omitempty"`
Subject string `json:"subject,omitempty"`
Body string `json:"body,omitempty"`
Template MessageTemplate `json:"template,omitempty"`
}
func (m MessageResponse) String() string {
return fmt.Sprintf("[intercom] message { id: %s, message_type: %s, body: %s }", m.ID, m.MessageType, m.Body)
}
// Save (send) a Message
func (m *MessageService) Save(message *MessageRequest) (MessageResponse, error) {
return m.Repository.save(message)
}
// NewEmailMessage creates a new *Message of email type.
func NewEmailMessage(template MessageTemplate, from, to MessagePerson, subject, body string) MessageRequest {
return MessageRequest{MessageType: "email", Template: template.String(), From: from.MessageAddress(), To: to.MessageAddress(), Subject: subject, Body: body}
}
// NewInAppMessage creates a new *Message of InApp (widget) type.
func NewInAppMessage(from, to MessagePerson, body string) MessageRequest {
return MessageRequest{MessageType: "inapp", From: from.MessageAddress(), To: to.MessageAddress(), Body: body}
}
// NewUserMessage creates a new *Message from a User.
func NewUserMessage(from MessagePerson, body string) MessageRequest {
return MessageRequest{MessageType: "inapp", From: from.MessageAddress(), Body: body}
}
// A MessagePerson is someone to send a Message to and from.
type MessagePerson interface {
MessageAddress() MessageAddress
}
type MessageAddress struct {
Type string `json:"type,omitempty"`
ID string `json:"id,omitempty"`
Email string `json:"email,omitempty"`
UserID string `json:"user_id,omitempty"`
}