-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathticket_comment.go
133 lines (113 loc) · 4.39 KB
/
ticket_comment.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package zendesk
import (
"context"
"encoding/json"
"fmt"
"time"
)
// TicketCommentAPI is an interface containing all ticket comment related API methods
type TicketCommentAPI interface {
CreateTicketComment(ctx context.Context, ticketID int64, ticketComment TicketComment) (TicketComment, error)
ListTicketComments(ctx context.Context, ticketID int64) ([]TicketComment, error)
MakeCommentPrivate(ctx context.Context, ticketID int64, ticketCommentID int64) error
}
// TicketComment is a struct for ticket comment payload
// Via and Metadata are currently unused
// https://developer.zendesk.com/rest_api/docs/support/ticket_comments
type TicketComment struct {
ID int64 `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Body string `json:"body,omitempty"`
HTMLBody string `json:"html_body,omitempty"`
PlainBody string `json:"plain_body,omitempty"`
Public *bool `json:"public,omitempty"`
AuthorID int64 `json:"author_id,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Uploads []string `json:"uploads,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Via *Via `json:"via,omitempty"`
}
// RedactTicketCommentRequest contains the body of the RedactTicketComment PUT request
type RedactTicketCommentRequest struct {
TicketID int64 `json:"ticket_id"` // Required
HTMLBody string `json:"html_body,omitempty"`
ExternalAttachmentUrls []string `json:"external_attachment_urls,omitempty"`
}
// NewPublicTicketComment generates and returns a new TicketComment
func NewPublicTicketComment(body string, authorID int64) TicketComment {
public := true
return TicketComment{
Body: body,
Public: &public,
AuthorID: authorID,
}
}
// NewPrivateTicketComment generates and returns a new private TicketComment
func NewPrivateTicketComment(body string, authorID int64) TicketComment {
public := false
return TicketComment{
Body: body,
Public: &public,
AuthorID: authorID,
}
}
// CreateTicketComment creates a comment on a ticket
//
// ref: https://developer.zendesk.com/rest_api/docs/support/ticket_comments#create-ticket-comment
func (z *Client) CreateTicketComment(ctx context.Context, ticketID int64, ticketComment TicketComment) (TicketComment, error) {
type comment struct {
Ticket struct {
TicketComment TicketComment `json:"comment"`
} `json:"ticket"`
}
data := &comment{}
data.Ticket.TicketComment = ticketComment
body, err := z.put(ctx, fmt.Sprintf("/tickets/%d.json", ticketID), data)
if err != nil {
return TicketComment{}, err
}
result := TicketComment{}
err = json.Unmarshal(body, &result)
if err != nil {
return TicketComment{}, err
}
return result, err
}
// ListTicketComments gets a list of comment for a specified ticket
//
// ref: https://developer.zendesk.com/rest_api/docs/support/ticket_comments#list-comments
func (z *Client) ListTicketComments(ctx context.Context, ticketID int64) ([]TicketComment, error) {
var result struct {
TicketComments []TicketComment `json:"comments"`
}
body, err := z.get(ctx, fmt.Sprintf("/tickets/%d/comments.json", ticketID))
if err != nil {
return []TicketComment{}, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return []TicketComment{}, err
}
return result.TicketComments, err
}
// MakeCommentPrivate converts an existing ticket comment to an internal note that is not publicly viewable.
//
// ref: https://developer.zendesk.com/api-reference/ticketing/tickets/ticket_comments/#make-comment-private
func (z *Client) MakeCommentPrivate(ctx context.Context, ticketID int64, ticketCommentID int64) error {
path := fmt.Sprintf("/tickets/%d/comments/%d/make_private", ticketID, ticketCommentID)
_, err := z.put(ctx, path, nil)
return err
}
// RedactTicketComment permanently removes words, strings, or attachments from a ticket comment
//
// ref: https://developer.zendesk.com/api-reference/ticketing/tickets/ticket_comments/#redact-ticket-comment-in-agent-workspace
func (z *Client) RedactTicketComment(
ctx context.Context,
ticketCommentID int64,
body RedactTicketCommentRequest,
) error {
path := fmt.Sprintf("/api/v2/comment_redactions/%d.json", ticketCommentID)
_, err := z.put(ctx, path, body)
return err
}