socialdesk-node 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +42 -0
- package/dist/client.js +122 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +13 -0
- package/dist/types.d.ts +212 -0
- package/dist/types.js +31 -0
- package/dist/webhook.d.ts +35 -0
- package/dist/webhook.js +108 -0
- package/package.json +34 -0
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AddLabelOptions, AssignConversationOptions, DirectMessageOptions, HintCallbackOptions, HintMessageOptions, HSMMessageOptions } from './types';
|
|
2
|
+
export declare class SocialdeskClient {
|
|
3
|
+
private accessToken;
|
|
4
|
+
private http;
|
|
5
|
+
constructor(accessToken: string, baseURL?: string);
|
|
6
|
+
/**
|
|
7
|
+
* Send a hint message visible only to the agent.
|
|
8
|
+
*/
|
|
9
|
+
sendHintMessage(options: HintMessageOptions): Promise<{
|
|
10
|
+
success: boolean;
|
|
11
|
+
}>;
|
|
12
|
+
/**
|
|
13
|
+
* Send a direct message to the end participant (requires "automatic replies" permission).
|
|
14
|
+
*/
|
|
15
|
+
sendDirectMessage(options: DirectMessageOptions): Promise<{
|
|
16
|
+
success: boolean;
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* Execute a hint callback action.
|
|
20
|
+
*/
|
|
21
|
+
sendHintCallback(options: HintCallbackOptions): Promise<{
|
|
22
|
+
success: boolean;
|
|
23
|
+
}>;
|
|
24
|
+
/**
|
|
25
|
+
* Send an HSM (template) message to a participant.
|
|
26
|
+
*/
|
|
27
|
+
sendHSMMessage(options: HSMMessageOptions): Promise<{
|
|
28
|
+
success: boolean;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* Assign a conversation to an agent or team.
|
|
32
|
+
*/
|
|
33
|
+
assignConversation(options: AssignConversationOptions): Promise<{
|
|
34
|
+
success: boolean;
|
|
35
|
+
}>;
|
|
36
|
+
/**
|
|
37
|
+
* Add a label to a conversation.
|
|
38
|
+
*/
|
|
39
|
+
addLabel(options: AddLabelOptions): Promise<{
|
|
40
|
+
success: boolean;
|
|
41
|
+
}>;
|
|
42
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SocialdeskClient = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
class SocialdeskClient {
|
|
9
|
+
constructor(accessToken, baseURL = 'https://api.socialdesk.com') {
|
|
10
|
+
this.accessToken = accessToken;
|
|
11
|
+
this.http = axios_1.default.create({
|
|
12
|
+
baseURL,
|
|
13
|
+
headers: {
|
|
14
|
+
Authorization: `Bearer ${accessToken}`,
|
|
15
|
+
'Content-Type': 'application/json',
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Send a hint message visible only to the agent.
|
|
21
|
+
*/
|
|
22
|
+
async sendHintMessage(options) {
|
|
23
|
+
const { data } = await this.http.post('/web-api/messages/reply', {
|
|
24
|
+
text: options.text,
|
|
25
|
+
attachments: options.attachments ?? [],
|
|
26
|
+
interactions: options.interactions ?? [],
|
|
27
|
+
message_id: options.message_id,
|
|
28
|
+
conversation_id: options.conversation_id,
|
|
29
|
+
callbacks: options.callbacks ?? [],
|
|
30
|
+
quick_replies: options.quick_replies ?? [],
|
|
31
|
+
send_as_direct_message: false,
|
|
32
|
+
application_context: options.application_context,
|
|
33
|
+
});
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Send a direct message to the end participant (requires "automatic replies" permission).
|
|
38
|
+
*/
|
|
39
|
+
async sendDirectMessage(options) {
|
|
40
|
+
const { data } = await this.http.post('/web-api/messages/reply', {
|
|
41
|
+
text: options.text,
|
|
42
|
+
attachments: options.attachments ?? [],
|
|
43
|
+
interactions: options.interactions ?? [],
|
|
44
|
+
message_id: options.message_id,
|
|
45
|
+
conversation_id: options.conversation_id,
|
|
46
|
+
callbacks: [],
|
|
47
|
+
quick_replies: [],
|
|
48
|
+
send_as_direct_message: true,
|
|
49
|
+
application_context: options.application_context,
|
|
50
|
+
});
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Execute a hint callback action.
|
|
55
|
+
*/
|
|
56
|
+
async sendHintCallback(options) {
|
|
57
|
+
const { data } = await this.http.post('/web-api/messages/reply/callback', {
|
|
58
|
+
callback_id: options.callback_id,
|
|
59
|
+
message_id: options.message_id,
|
|
60
|
+
reply_id: options.reply_id,
|
|
61
|
+
conversation_id: options.conversation_id,
|
|
62
|
+
app_instance: options.app_instance,
|
|
63
|
+
callback_value: options.callback_value,
|
|
64
|
+
});
|
|
65
|
+
return data;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Send an HSM (template) message to a participant.
|
|
69
|
+
*/
|
|
70
|
+
async sendHSMMessage(options) {
|
|
71
|
+
const body = {
|
|
72
|
+
channel_id: options.channel_id,
|
|
73
|
+
raw_text: options.raw_text,
|
|
74
|
+
};
|
|
75
|
+
// Identificar destinatario: account_contact_id O contact, no ambos
|
|
76
|
+
if (options.account_contact_id) {
|
|
77
|
+
body.account_contact_id = options.account_contact_id;
|
|
78
|
+
}
|
|
79
|
+
else if (options.contact) {
|
|
80
|
+
body.contact = options.contact;
|
|
81
|
+
}
|
|
82
|
+
if (options.parameters)
|
|
83
|
+
body.parameters = options.parameters;
|
|
84
|
+
if (options.external_template_id)
|
|
85
|
+
body.external_template_id = options.external_template_id;
|
|
86
|
+
if (options.assign_entity)
|
|
87
|
+
body.assign_entity = options.assign_entity;
|
|
88
|
+
if (options.assign_entity_id)
|
|
89
|
+
body.assign_entity_id = options.assign_entity_id;
|
|
90
|
+
if (options.conversation_id)
|
|
91
|
+
body.conversation_id = options.conversation_id;
|
|
92
|
+
if (options.send_at)
|
|
93
|
+
body.send_at = options.send_at;
|
|
94
|
+
const { data } = await this.http.post('/web-api/messages/HSM', body);
|
|
95
|
+
return data;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Assign a conversation to an agent or team.
|
|
99
|
+
*/
|
|
100
|
+
async assignConversation(options) {
|
|
101
|
+
const { data } = await this.http.post('/web-api/conversations/assign', {
|
|
102
|
+
conversation_id: options.conversation_id,
|
|
103
|
+
account_id: options.account_id ?? null,
|
|
104
|
+
entity: options.entity,
|
|
105
|
+
entity_id: options.entity_id,
|
|
106
|
+
force_assign: options.force_assign ?? false,
|
|
107
|
+
});
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Add a label to a conversation.
|
|
112
|
+
*/
|
|
113
|
+
async addLabel(options) {
|
|
114
|
+
const { data } = await this.http.post('/web-api/conversations/labels/add', {
|
|
115
|
+
conversation_id: options.conversation_id,
|
|
116
|
+
account_id: options.account_id,
|
|
117
|
+
labelKey: options.labelKey,
|
|
118
|
+
});
|
|
119
|
+
return data;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
exports.SocialdeskClient = SocialdeskClient;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { SocialdeskClient } from './client';
|
|
2
|
+
export { WebhookHelper } from './webhook';
|
|
3
|
+
export { EventType, MessageDirection, SenderType, AttachmentType, WebhookEvent, WebhookPayload, DirectMessagePayload, ReplyMessagePayload, ReplyMessageCallbackPayload, HSMMessagePayload, ConversationFinalizedPayload, MessageSender, Participant, Agent, Attachment, Interaction, QuickReply, Callback, HintMessageOptions, DirectMessageOptions, HintCallbackOptions, HSMMessageOptions, AssignConversationOptions, AddLabelOptions, } from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AttachmentType = exports.SenderType = exports.MessageDirection = exports.EventType = exports.WebhookHelper = exports.SocialdeskClient = void 0;
|
|
4
|
+
var client_1 = require("./client");
|
|
5
|
+
Object.defineProperty(exports, "SocialdeskClient", { enumerable: true, get: function () { return client_1.SocialdeskClient; } });
|
|
6
|
+
var webhook_1 = require("./webhook");
|
|
7
|
+
Object.defineProperty(exports, "WebhookHelper", { enumerable: true, get: function () { return webhook_1.WebhookHelper; } });
|
|
8
|
+
var types_1 = require("./types");
|
|
9
|
+
// Enums
|
|
10
|
+
Object.defineProperty(exports, "EventType", { enumerable: true, get: function () { return types_1.EventType; } });
|
|
11
|
+
Object.defineProperty(exports, "MessageDirection", { enumerable: true, get: function () { return types_1.MessageDirection; } });
|
|
12
|
+
Object.defineProperty(exports, "SenderType", { enumerable: true, get: function () { return types_1.SenderType; } });
|
|
13
|
+
Object.defineProperty(exports, "AttachmentType", { enumerable: true, get: function () { return types_1.AttachmentType; } });
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
export declare enum EventType {
|
|
2
|
+
DirectMessage = "DIRECT_MESSAGE",
|
|
3
|
+
ReplyMessage = "REPLY_MESSAGE",
|
|
4
|
+
HSMMessage = "HSM_MESSAGE",
|
|
5
|
+
ReplyMessageCallback = "REPLY_MESSAGE_CALLBACK",
|
|
6
|
+
ConversationFinalized = "CONVERSATION_FINALIZED"
|
|
7
|
+
}
|
|
8
|
+
export declare enum MessageDirection {
|
|
9
|
+
Incoming = "INCOMING",
|
|
10
|
+
Outgoing = "OUTGOING"
|
|
11
|
+
}
|
|
12
|
+
export declare enum SenderType {
|
|
13
|
+
Participant = "PARTICIPANT",
|
|
14
|
+
Extension = "EXTENSION",
|
|
15
|
+
Agent = "AGENT"
|
|
16
|
+
}
|
|
17
|
+
export declare enum AttachmentType {
|
|
18
|
+
IMAGE = "IMAGE",
|
|
19
|
+
VIDEO = "VIDEO",
|
|
20
|
+
AUDIO = "AUDIO",
|
|
21
|
+
TEXT = "TEXT",
|
|
22
|
+
CONTACTS = "CONTACTS"
|
|
23
|
+
}
|
|
24
|
+
export interface MessageSender {
|
|
25
|
+
name: string;
|
|
26
|
+
profile_id: string;
|
|
27
|
+
picture: string;
|
|
28
|
+
type: SenderType;
|
|
29
|
+
}
|
|
30
|
+
export interface Participant {
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
picture: string;
|
|
34
|
+
channelInstances?: string[];
|
|
35
|
+
}
|
|
36
|
+
export interface Agent {
|
|
37
|
+
id: string;
|
|
38
|
+
name: string;
|
|
39
|
+
picture: string;
|
|
40
|
+
}
|
|
41
|
+
export interface Attachment {
|
|
42
|
+
type: AttachmentType;
|
|
43
|
+
url?: string;
|
|
44
|
+
fileName?: string;
|
|
45
|
+
}
|
|
46
|
+
export interface Interaction {
|
|
47
|
+
label: string;
|
|
48
|
+
config: unknown;
|
|
49
|
+
}
|
|
50
|
+
export interface QuickReply {
|
|
51
|
+
text: string;
|
|
52
|
+
attachments?: Attachment[];
|
|
53
|
+
}
|
|
54
|
+
export interface Callback {
|
|
55
|
+
id: string;
|
|
56
|
+
label: string;
|
|
57
|
+
value: string;
|
|
58
|
+
}
|
|
59
|
+
export interface DirectMessagePayload {
|
|
60
|
+
id: string;
|
|
61
|
+
direction: MessageDirection;
|
|
62
|
+
sender: MessageSender;
|
|
63
|
+
text: string;
|
|
64
|
+
attachments: Attachment[];
|
|
65
|
+
interactions: Interaction[];
|
|
66
|
+
send_at: string;
|
|
67
|
+
conversation: {
|
|
68
|
+
id: string;
|
|
69
|
+
topic: string;
|
|
70
|
+
participants: Participant[];
|
|
71
|
+
agents: Agent[];
|
|
72
|
+
context: any;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export interface ReplyMessagePayload {
|
|
76
|
+
id: string;
|
|
77
|
+
text: string;
|
|
78
|
+
callbacks: Callback[];
|
|
79
|
+
quick_replies: QuickReply[];
|
|
80
|
+
send_at: string;
|
|
81
|
+
conversation: {
|
|
82
|
+
id: string;
|
|
83
|
+
context: any;
|
|
84
|
+
};
|
|
85
|
+
message: {
|
|
86
|
+
id: string;
|
|
87
|
+
direction: MessageDirection;
|
|
88
|
+
text: string;
|
|
89
|
+
attachments: Attachment[];
|
|
90
|
+
send_at: string;
|
|
91
|
+
};
|
|
92
|
+
sender: MessageSender;
|
|
93
|
+
}
|
|
94
|
+
export interface ReplyMessageCallbackPayload {
|
|
95
|
+
id: string;
|
|
96
|
+
label: string;
|
|
97
|
+
value: any;
|
|
98
|
+
conversation: {
|
|
99
|
+
id: string;
|
|
100
|
+
context: any;
|
|
101
|
+
};
|
|
102
|
+
message: {
|
|
103
|
+
id: string;
|
|
104
|
+
direction: MessageDirection;
|
|
105
|
+
text: string;
|
|
106
|
+
attachments: Attachment[];
|
|
107
|
+
send_at: string;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
export interface HSMMessagePayload {
|
|
111
|
+
id: string;
|
|
112
|
+
direction: MessageDirection;
|
|
113
|
+
text: string;
|
|
114
|
+
rawText: string;
|
|
115
|
+
to: string;
|
|
116
|
+
parameters: any;
|
|
117
|
+
externalTemplateId: string;
|
|
118
|
+
send_at: string;
|
|
119
|
+
conversation: {
|
|
120
|
+
id: string;
|
|
121
|
+
topic: string;
|
|
122
|
+
participants: Participant[];
|
|
123
|
+
agents: Agent[];
|
|
124
|
+
context: any;
|
|
125
|
+
};
|
|
126
|
+
attachments: Attachment[];
|
|
127
|
+
}
|
|
128
|
+
export interface ConversationFinalizedPayload {
|
|
129
|
+
id: string;
|
|
130
|
+
conversation: {
|
|
131
|
+
id: string;
|
|
132
|
+
context: any;
|
|
133
|
+
closedBySchedule?: boolean;
|
|
134
|
+
};
|
|
135
|
+
message: {
|
|
136
|
+
id: string;
|
|
137
|
+
direction: MessageDirection;
|
|
138
|
+
text: string;
|
|
139
|
+
send_at: string;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
export type WebhookPayload = DirectMessagePayload | ReplyMessagePayload | ReplyMessageCallbackPayload | HSMMessagePayload | ConversationFinalizedPayload;
|
|
143
|
+
export interface WebhookEvent {
|
|
144
|
+
type: EventType;
|
|
145
|
+
payload: WebhookPayload;
|
|
146
|
+
channel_instance: string;
|
|
147
|
+
instance: {
|
|
148
|
+
type: string;
|
|
149
|
+
id: string;
|
|
150
|
+
settings?: string;
|
|
151
|
+
};
|
|
152
|
+
access_token: string;
|
|
153
|
+
event_id: string;
|
|
154
|
+
event_time: string;
|
|
155
|
+
}
|
|
156
|
+
export interface HintMessageOptions {
|
|
157
|
+
text?: string;
|
|
158
|
+
attachments?: Attachment[];
|
|
159
|
+
interactions?: Interaction[];
|
|
160
|
+
message_id: string;
|
|
161
|
+
conversation_id: string;
|
|
162
|
+
callbacks?: Callback[];
|
|
163
|
+
quick_replies?: QuickReply[];
|
|
164
|
+
application_context?: Record<string, any>;
|
|
165
|
+
}
|
|
166
|
+
export interface DirectMessageOptions {
|
|
167
|
+
text?: string;
|
|
168
|
+
attachments?: Attachment[];
|
|
169
|
+
interactions?: Interaction[];
|
|
170
|
+
message_id: string;
|
|
171
|
+
conversation_id: string;
|
|
172
|
+
application_context?: Record<string, any>;
|
|
173
|
+
}
|
|
174
|
+
export interface HintCallbackOptions {
|
|
175
|
+
callback_id: string;
|
|
176
|
+
message_id: string;
|
|
177
|
+
reply_id: string;
|
|
178
|
+
conversation_id: string;
|
|
179
|
+
app_instance: string;
|
|
180
|
+
callback_value: any;
|
|
181
|
+
}
|
|
182
|
+
export interface HSMMessageOptions {
|
|
183
|
+
channel_id: string;
|
|
184
|
+
account_contact_id?: string;
|
|
185
|
+
contact?: {
|
|
186
|
+
account_id: string;
|
|
187
|
+
first_name?: string;
|
|
188
|
+
last_name?: string;
|
|
189
|
+
phone: string;
|
|
190
|
+
email?: string;
|
|
191
|
+
image_url?: string;
|
|
192
|
+
};
|
|
193
|
+
raw_text: string;
|
|
194
|
+
parameters?: any[];
|
|
195
|
+
external_template_id?: string;
|
|
196
|
+
assign_entity?: 'USER' | 'TEAM';
|
|
197
|
+
assign_entity_id?: string;
|
|
198
|
+
conversation_id?: string;
|
|
199
|
+
send_at?: string;
|
|
200
|
+
}
|
|
201
|
+
export interface AssignConversationOptions {
|
|
202
|
+
conversation_id: string;
|
|
203
|
+
account_id?: string;
|
|
204
|
+
entity: 'USER' | 'TEAM';
|
|
205
|
+
entity_id: string;
|
|
206
|
+
force_assign?: boolean;
|
|
207
|
+
}
|
|
208
|
+
export interface AddLabelOptions {
|
|
209
|
+
conversation_id: string;
|
|
210
|
+
account_id?: string;
|
|
211
|
+
labelKey: string;
|
|
212
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ── Event Types ──
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.AttachmentType = exports.SenderType = exports.MessageDirection = exports.EventType = void 0;
|
|
5
|
+
var EventType;
|
|
6
|
+
(function (EventType) {
|
|
7
|
+
EventType["DirectMessage"] = "DIRECT_MESSAGE";
|
|
8
|
+
EventType["ReplyMessage"] = "REPLY_MESSAGE";
|
|
9
|
+
EventType["HSMMessage"] = "HSM_MESSAGE";
|
|
10
|
+
EventType["ReplyMessageCallback"] = "REPLY_MESSAGE_CALLBACK";
|
|
11
|
+
EventType["ConversationFinalized"] = "CONVERSATION_FINALIZED";
|
|
12
|
+
})(EventType || (exports.EventType = EventType = {}));
|
|
13
|
+
var MessageDirection;
|
|
14
|
+
(function (MessageDirection) {
|
|
15
|
+
MessageDirection["Incoming"] = "INCOMING";
|
|
16
|
+
MessageDirection["Outgoing"] = "OUTGOING";
|
|
17
|
+
})(MessageDirection || (exports.MessageDirection = MessageDirection = {}));
|
|
18
|
+
var SenderType;
|
|
19
|
+
(function (SenderType) {
|
|
20
|
+
SenderType["Participant"] = "PARTICIPANT";
|
|
21
|
+
SenderType["Extension"] = "EXTENSION";
|
|
22
|
+
SenderType["Agent"] = "AGENT";
|
|
23
|
+
})(SenderType || (exports.SenderType = SenderType = {}));
|
|
24
|
+
var AttachmentType;
|
|
25
|
+
(function (AttachmentType) {
|
|
26
|
+
AttachmentType["IMAGE"] = "IMAGE";
|
|
27
|
+
AttachmentType["VIDEO"] = "VIDEO";
|
|
28
|
+
AttachmentType["AUDIO"] = "AUDIO";
|
|
29
|
+
AttachmentType["TEXT"] = "TEXT";
|
|
30
|
+
AttachmentType["CONTACTS"] = "CONTACTS";
|
|
31
|
+
})(AttachmentType || (exports.AttachmentType = AttachmentType = {}));
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EventType, WebhookEvent, WebhookPayload } from './types';
|
|
2
|
+
import { SocialdeskClient } from './client';
|
|
3
|
+
/**
|
|
4
|
+
* Helper class to parse and extract data from Socialdesk webhook events.
|
|
5
|
+
*/
|
|
6
|
+
export declare class WebhookHelper {
|
|
7
|
+
private event;
|
|
8
|
+
constructor(event: WebhookEvent);
|
|
9
|
+
/** Get the event type. */
|
|
10
|
+
getEventType(): EventType;
|
|
11
|
+
/** Get the raw payload. */
|
|
12
|
+
getPayload(): WebhookPayload;
|
|
13
|
+
/** Get the access token to call the API. */
|
|
14
|
+
getAccessToken(): string;
|
|
15
|
+
/** Get the channel instance ID. */
|
|
16
|
+
getChannelInstance(): string;
|
|
17
|
+
/** Get the app instance ID. */
|
|
18
|
+
getInstanceId(): string;
|
|
19
|
+
/** Get the parsed app settings (from instance.settings JSON string). */
|
|
20
|
+
getSettings<T = any>(): T;
|
|
21
|
+
/** Get the conversation ID from any event type. */
|
|
22
|
+
getConversationId(): string;
|
|
23
|
+
/** Get the message ID from any event type. */
|
|
24
|
+
getMessageId(): string;
|
|
25
|
+
/** Get the message text from any event type. */
|
|
26
|
+
getMessageText(): string;
|
|
27
|
+
/** Get the full conversation context from any event type. */
|
|
28
|
+
getConversationContext<T = any>(): T;
|
|
29
|
+
/** Get the application_context for this app from the conversation context (assistants.{appInstanceId}). */
|
|
30
|
+
getApplicationContext<T = any>(): T;
|
|
31
|
+
/** Check if the event is a specific type. */
|
|
32
|
+
isEventType(type: EventType): boolean;
|
|
33
|
+
/** Create a SocialdeskClient pre-authenticated with the event's access token. */
|
|
34
|
+
createClient(baseURL?: string): SocialdeskClient;
|
|
35
|
+
}
|
package/dist/webhook.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebhookHelper = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const client_1 = require("./client");
|
|
6
|
+
/**
|
|
7
|
+
* Helper class to parse and extract data from Socialdesk webhook events.
|
|
8
|
+
*/
|
|
9
|
+
class WebhookHelper {
|
|
10
|
+
constructor(event) {
|
|
11
|
+
this.event = event;
|
|
12
|
+
}
|
|
13
|
+
/** Get the event type. */
|
|
14
|
+
getEventType() {
|
|
15
|
+
return this.event.type;
|
|
16
|
+
}
|
|
17
|
+
/** Get the raw payload. */
|
|
18
|
+
getPayload() {
|
|
19
|
+
return this.event.payload;
|
|
20
|
+
}
|
|
21
|
+
/** Get the access token to call the API. */
|
|
22
|
+
getAccessToken() {
|
|
23
|
+
return this.event.access_token;
|
|
24
|
+
}
|
|
25
|
+
/** Get the channel instance ID. */
|
|
26
|
+
getChannelInstance() {
|
|
27
|
+
return this.event.channel_instance;
|
|
28
|
+
}
|
|
29
|
+
/** Get the app instance ID. */
|
|
30
|
+
getInstanceId() {
|
|
31
|
+
return this.event.instance.id;
|
|
32
|
+
}
|
|
33
|
+
/** Get the parsed app settings (from instance.settings JSON string). */
|
|
34
|
+
getSettings() {
|
|
35
|
+
try {
|
|
36
|
+
return typeof this.event.instance.settings === 'string'
|
|
37
|
+
? JSON.parse(this.event.instance.settings)
|
|
38
|
+
: this.event.instance.settings ?? {};
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Get the conversation ID from any event type. */
|
|
45
|
+
getConversationId() {
|
|
46
|
+
const payload = this.event.payload;
|
|
47
|
+
if ('conversation' in payload && payload.conversation) {
|
|
48
|
+
return payload.conversation.id;
|
|
49
|
+
}
|
|
50
|
+
return '';
|
|
51
|
+
}
|
|
52
|
+
/** Get the message ID from any event type. */
|
|
53
|
+
getMessageId() {
|
|
54
|
+
const payload = this.event.payload;
|
|
55
|
+
const type = this.event.type;
|
|
56
|
+
if (type === types_1.EventType.DirectMessage || type === types_1.EventType.HSMMessage) {
|
|
57
|
+
return payload.id;
|
|
58
|
+
}
|
|
59
|
+
if (type === types_1.EventType.ReplyMessage || type === types_1.EventType.ReplyMessageCallback) {
|
|
60
|
+
return payload.message.id;
|
|
61
|
+
}
|
|
62
|
+
if (type === types_1.EventType.ConversationFinalized) {
|
|
63
|
+
return payload.message.id;
|
|
64
|
+
}
|
|
65
|
+
return '';
|
|
66
|
+
}
|
|
67
|
+
/** Get the message text from any event type. */
|
|
68
|
+
getMessageText() {
|
|
69
|
+
const payload = this.event.payload;
|
|
70
|
+
const type = this.event.type;
|
|
71
|
+
if (type === types_1.EventType.DirectMessage) {
|
|
72
|
+
return payload.text;
|
|
73
|
+
}
|
|
74
|
+
if (type === types_1.EventType.HSMMessage) {
|
|
75
|
+
return payload.text;
|
|
76
|
+
}
|
|
77
|
+
if (type === types_1.EventType.ReplyMessage || type === types_1.EventType.ReplyMessageCallback) {
|
|
78
|
+
return payload.message.text;
|
|
79
|
+
}
|
|
80
|
+
if (type === types_1.EventType.ConversationFinalized) {
|
|
81
|
+
return payload.message.text;
|
|
82
|
+
}
|
|
83
|
+
return '';
|
|
84
|
+
}
|
|
85
|
+
/** Get the full conversation context from any event type. */
|
|
86
|
+
getConversationContext() {
|
|
87
|
+
const payload = this.event.payload;
|
|
88
|
+
if ('conversation' in payload && payload.conversation) {
|
|
89
|
+
return payload.conversation.context ?? {};
|
|
90
|
+
}
|
|
91
|
+
return {};
|
|
92
|
+
}
|
|
93
|
+
/** Get the application_context for this app from the conversation context (assistants.{appInstanceId}). */
|
|
94
|
+
getApplicationContext() {
|
|
95
|
+
const context = this.getConversationContext();
|
|
96
|
+
const appInstanceId = this.getInstanceId();
|
|
97
|
+
return (context?.assistants?.[appInstanceId] ?? {});
|
|
98
|
+
}
|
|
99
|
+
/** Check if the event is a specific type. */
|
|
100
|
+
isEventType(type) {
|
|
101
|
+
return this.event.type === type;
|
|
102
|
+
}
|
|
103
|
+
/** Create a SocialdeskClient pre-authenticated with the event's access token. */
|
|
104
|
+
createClient(baseURL) {
|
|
105
|
+
return new client_1.SocialdeskClient(this.event.access_token, baseURL);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.WebhookHelper = WebhookHelper;
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "socialdesk-node",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "SDK oficial de Socialdesk para Node.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"prepublishOnly": "npm run build"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"socialdesk",
|
|
16
|
+
"sdk",
|
|
17
|
+
"node",
|
|
18
|
+
"whatsapp",
|
|
19
|
+
"chatbot",
|
|
20
|
+
"messaging"
|
|
21
|
+
],
|
|
22
|
+
"author": "Socialdesk",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"axios": "^1.7.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"typescript": "^5.4.0",
|
|
32
|
+
"@types/node": "^20.0.0"
|
|
33
|
+
}
|
|
34
|
+
}
|