-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chatwork.node.ts
180 lines (167 loc) · 5.75 KB
/
Chatwork.node.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {
IExecuteFunctions,
IHttpRequestMethods,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { chatworkApiRequest } from '../../shared/GenericFunctions';
import {
BodyProperty,
ContactProperty,
DescriptionProperty,
IconPresetProperty,
LimitProperty,
MeProperty,
MessageIdProperty,
MessageProperty,
MyProperty,
NameProperty,
ResourceOptionsValue,
ResourceProperty,
RoomIdProperty,
RoomOptionsValue,
RoomProperty,
TaskIdProperty,
ToIdsProperty,
} from './properties';
export class Chatwork implements INodeType {
description: INodeTypeDescription = {
displayName: 'Chatwork',
name: 'chatwork',
icon: 'file:../../assets/chatwork.png',
group: ['transform'],
version: 1,
description: 'Retrieve data from Chatwork API.',
defaults: {
name: 'Chatwork',
color: '#EF3646',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'chatworkApi',
required: true,
},
],
properties: [
// Node properties which the user gets displayed and can change on the node
ResourceProperty,
MeProperty,
MyProperty,
ContactProperty,
RoomProperty,
RoomIdProperty,
MessageProperty,
DescriptionProperty,
NameProperty,
IconPresetProperty,
MessageIdProperty,
TaskIdProperty,
BodyProperty,
LimitProperty,
ToIdsProperty,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnItems: INodeExecutionData[] = [];
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
// tslint:disable-next-line: prefer-for-of
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
let endpoint = `/${resource}`;
let method: IHttpRequestMethods = 'GET';
let body = null;
let messageId: string;
if (resource === 'my') {
endpoint += `/${operation}`;
}
if (resource === ResourceOptionsValue.ROOMS) {
if (operation !== RoomOptionsValue.GET) {
const defaultRoomId = this.getNodeParameter(RoomIdProperty.name, 0);
const roomId = this.getNodeParameter(RoomIdProperty.name, itemIndex) || defaultRoomId;
if (typeof roomId === 'number' && roomId !== 0) {
endpoint += `/${roomId}`;
switch (operation) {
case RoomOptionsValue.SEND_MESSAGE:
method = 'POST';
endpoint += '/messages';
const message = this.getNodeParameter(MessageProperty.name, itemIndex) as string;
body = { body: message };
break;
case RoomOptionsValue.GET_MEMBERS:
endpoint += '/members';
break;
case RoomOptionsValue.GET_MESSAGES:
endpoint += '/messages?force=1';
break;
case RoomOptionsValue.GET_DETAIL:
break;
case RoomOptionsValue.UPDATE_INFO:
method = 'PUT';
const description = this.getNodeParameter(DescriptionProperty.name, itemIndex) as string;
const name = this.getNodeParameter(NameProperty.name, itemIndex) as string;
const iconPreset = this.getNodeParameter(IconPresetProperty.name, itemIndex) as string;
body = {
icon_preset: iconPreset,
} as unknown as { name?: string, description?: string, icon_preset: string };
if (description) {
body.description = description;
}
if (name) {
body.name = name;
}
break;
case RoomOptionsValue.GET_MESSAGE_DETAIL:
messageId = this.getNodeParameter(MessageIdProperty.name, itemIndex) as string;
endpoint += `/messages/${messageId}`;
break;
case RoomOptionsValue.DELETE_MESSAGE:
messageId = this.getNodeParameter(MessageIdProperty.name, itemIndex) as string;
method = 'DELETE';
endpoint += `/messages/${messageId}`;
break;
case RoomOptionsValue.GET_ROOM_TASKS:
endpoint += '/tasks';
break;
case RoomOptionsValue.GET_ROOM_TASK_DETAIL:
const taskId = this.getNodeParameter(TaskIdProperty.name, itemIndex);
endpoint += `/tasks/${taskId}`;
break;
case RoomOptionsValue.CREATE_ROOM_TASK:
const taskDes = this.getNodeParameter(BodyProperty.name, itemIndex);
const limit = Math.round(
(new Date(this.getNodeParameter(LimitProperty.name, itemIndex) as string)).valueOf() / 1000,
);
const toIds = this.getNodeParameter(ToIdsProperty.name, itemIndex);
body = {
body: taskDes,
limit,
to_ids: toIds,
}
method = 'POST';
endpoint += '/tasks';
break;
default:
throw new Error(`${operation} is not supported.`)
}
}
}
}
const response = await chatworkApiRequest.call(this, method, endpoint, body);
if (Array.isArray(response)) {
// flatten response
returnItems.push(...response);
} else {
returnItems.push({ json: response });
}
}
if (returnItems.some((i) => i.json !== undefined)) {
return [returnItems];
} else {
return [this.helpers.returnJsonArray(returnItems)];
}
}
}