natureco-sdk 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/README.md +292 -0
- package/index.js +327 -0
- package/package.json +35 -0
package/README.md
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# NatureCo JavaScript SDK
|
|
2
|
+
|
|
3
|
+
Official JavaScript SDK for NatureCo API. Build powerful AI bots and integrate them with Discord, Telegram, Slack, and Instagram.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install natureco-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
const { NatureCoClient } = require('natureco-sdk');
|
|
15
|
+
|
|
16
|
+
// Initialize client
|
|
17
|
+
const client = new NatureCoClient('nco_your_api_key_here');
|
|
18
|
+
|
|
19
|
+
// Create a bot
|
|
20
|
+
const bot = await client.bots.create({
|
|
21
|
+
name: 'My First Bot',
|
|
22
|
+
systemPrompt: 'You are a helpful assistant',
|
|
23
|
+
model: 'gpt-4'
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
console.log('Bot created:', bot);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Modules
|
|
30
|
+
|
|
31
|
+
### Bot Management
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
// List all bots
|
|
35
|
+
const bots = await client.bots.list();
|
|
36
|
+
|
|
37
|
+
// Get bot details
|
|
38
|
+
const bot = await client.bots.get('bot_id');
|
|
39
|
+
|
|
40
|
+
// Update bot
|
|
41
|
+
await client.bots.update('bot_id', {
|
|
42
|
+
name: 'Updated Name',
|
|
43
|
+
systemPrompt: 'New system prompt'
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Delete bot
|
|
47
|
+
await client.bots.delete('bot_id');
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Channel Integration
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
// Connect Discord
|
|
54
|
+
await client.channels.connect({
|
|
55
|
+
botId: 'bot_id',
|
|
56
|
+
platform: 'discord',
|
|
57
|
+
token: 'discord_bot_token'
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Connect Telegram
|
|
61
|
+
await client.channels.connect({
|
|
62
|
+
botId: 'bot_id',
|
|
63
|
+
platform: 'telegram',
|
|
64
|
+
token: 'telegram_bot_token'
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// List channels
|
|
68
|
+
const channels = await client.channels.list('bot_id');
|
|
69
|
+
|
|
70
|
+
// Disconnect channel
|
|
71
|
+
await client.channels.disconnect('bot_id', 'channel_id');
|
|
72
|
+
|
|
73
|
+
// Test channel connection
|
|
74
|
+
await client.channels.test('bot_id', 'channel_id');
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Messaging
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
// Send message
|
|
81
|
+
await client.messages.send({
|
|
82
|
+
botId: 'bot_id',
|
|
83
|
+
message: 'Hello, world!',
|
|
84
|
+
channel: 'discord',
|
|
85
|
+
userId: 'user_123'
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Get message history
|
|
89
|
+
const messages = await client.messages.history({
|
|
90
|
+
botId: 'bot_id',
|
|
91
|
+
limit: 50,
|
|
92
|
+
offset: 0
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Get specific message
|
|
96
|
+
const message = await client.messages.get('bot_id', 'message_id');
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Webhooks
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
// Create webhook
|
|
103
|
+
const webhook = await client.webhooks.create({
|
|
104
|
+
botId: 'bot_id',
|
|
105
|
+
url: 'https://your-server.com/webhook',
|
|
106
|
+
events: ['message.received', 'message.sent'],
|
|
107
|
+
secret: 'your_webhook_secret'
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// List webhooks
|
|
111
|
+
const webhooks = await client.webhooks.list('bot_id');
|
|
112
|
+
|
|
113
|
+
// Update webhook
|
|
114
|
+
await client.webhooks.update('bot_id', 'webhook_id', {
|
|
115
|
+
url: 'https://new-url.com/webhook',
|
|
116
|
+
active: true
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Test webhook
|
|
120
|
+
await client.webhooks.test('bot_id', 'webhook_id');
|
|
121
|
+
|
|
122
|
+
// Get webhook logs
|
|
123
|
+
const logs = await client.webhooks.logs('bot_id', 'webhook_id', 20);
|
|
124
|
+
|
|
125
|
+
// Delete webhook
|
|
126
|
+
await client.webhooks.delete('bot_id', 'webhook_id');
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Analytics
|
|
130
|
+
|
|
131
|
+
```javascript
|
|
132
|
+
// Get analytics overview
|
|
133
|
+
const analytics = await client.analytics.get({
|
|
134
|
+
botId: 'bot_id',
|
|
135
|
+
period: 'week' // 'day', 'week', or 'month'
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Get message analytics
|
|
139
|
+
const messageStats = await client.analytics.messages('bot_id', {
|
|
140
|
+
startDate: '2026-05-01',
|
|
141
|
+
endDate: '2026-05-09'
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Get channel analytics
|
|
145
|
+
const channelStats = await client.analytics.channels('bot_id');
|
|
146
|
+
|
|
147
|
+
// Get user analytics
|
|
148
|
+
const userStats = await client.analytics.users('bot_id', { limit: 100 });
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### User Management
|
|
152
|
+
|
|
153
|
+
```javascript
|
|
154
|
+
// Get current user
|
|
155
|
+
const user = await client.user.me();
|
|
156
|
+
|
|
157
|
+
// Update user profile
|
|
158
|
+
await client.user.update({
|
|
159
|
+
displayName: 'New Name',
|
|
160
|
+
bio: 'My bio',
|
|
161
|
+
avatar: 'https://example.com/avatar.png'
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### API Key Management
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
// List API keys
|
|
169
|
+
const keys = await client.apiKeys.list();
|
|
170
|
+
|
|
171
|
+
// Create new API key
|
|
172
|
+
const newKey = await client.apiKeys.create({
|
|
173
|
+
name: 'Production Key',
|
|
174
|
+
scopes: ['bots:read', 'bots:write']
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Revoke API key
|
|
178
|
+
await client.apiKeys.revoke('key_id');
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Web Widget
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
// Get embed code
|
|
185
|
+
const embedCode = client.widget.getEmbedCode({
|
|
186
|
+
botId: 'bot_id',
|
|
187
|
+
theme: 'light', // 'light' or 'dark'
|
|
188
|
+
position: 'bottom-right' // 'bottom-right', 'bottom-left', etc.
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// Update widget settings
|
|
192
|
+
await client.widget.updateSettings({
|
|
193
|
+
botId: 'bot_id',
|
|
194
|
+
theme: 'dark',
|
|
195
|
+
position: 'bottom-right',
|
|
196
|
+
welcomeMessage: 'Hello! How can I help you?',
|
|
197
|
+
primaryColor: '#10B981'
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// Get widget settings
|
|
201
|
+
const settings = await client.widget.getSettings('bot_id');
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Error Handling
|
|
205
|
+
|
|
206
|
+
```javascript
|
|
207
|
+
const { NatureCoClient, NatureCoError } = require('natureco-sdk');
|
|
208
|
+
|
|
209
|
+
const client = new NatureCoClient('nco_your_api_key');
|
|
210
|
+
|
|
211
|
+
try {
|
|
212
|
+
const bot = await client.bots.create({
|
|
213
|
+
name: 'Test Bot',
|
|
214
|
+
systemPrompt: 'You are helpful'
|
|
215
|
+
});
|
|
216
|
+
} catch (error) {
|
|
217
|
+
if (error instanceof NatureCoError) {
|
|
218
|
+
console.error('API Error:', error.message);
|
|
219
|
+
console.error('Status Code:', error.statusCode);
|
|
220
|
+
console.error('Response:', error.response);
|
|
221
|
+
} else {
|
|
222
|
+
console.error('Unexpected error:', error);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Configuration Options
|
|
228
|
+
|
|
229
|
+
```javascript
|
|
230
|
+
const client = new NatureCoClient('nco_your_api_key', {
|
|
231
|
+
baseURL: 'https://api.natureco.me/api/v1', // Custom API base URL
|
|
232
|
+
timeout: 30000 // Request timeout in milliseconds (default: 30000)
|
|
233
|
+
});
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## TypeScript Support
|
|
237
|
+
|
|
238
|
+
TypeScript definitions are included. Import types:
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
import { NatureCoClient, NatureCoError } from 'natureco-sdk';
|
|
242
|
+
|
|
243
|
+
const client = new NatureCoClient('nco_your_api_key');
|
|
244
|
+
|
|
245
|
+
// TypeScript will provide autocomplete and type checking
|
|
246
|
+
const bot = await client.bots.create({
|
|
247
|
+
name: 'My Bot',
|
|
248
|
+
systemPrompt: 'You are helpful',
|
|
249
|
+
model: 'gpt-4'
|
|
250
|
+
});
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## Browser Usage
|
|
254
|
+
|
|
255
|
+
You can also use the SDK in the browser via CDN:
|
|
256
|
+
|
|
257
|
+
```html
|
|
258
|
+
<script src="https://cdn.natureco.me/sdk/v1/natureco-sdk.min.js"></script>
|
|
259
|
+
<script>
|
|
260
|
+
const client = new NatureCoClient('nco_your_api_key');
|
|
261
|
+
|
|
262
|
+
client.bots.list().then(bots => {
|
|
263
|
+
console.log('Bots:', bots);
|
|
264
|
+
});
|
|
265
|
+
</script>
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Rate Limiting
|
|
269
|
+
|
|
270
|
+
The SDK automatically handles rate limiting. When you hit a rate limit, the API will return a `429` status code with a `X-RateLimit-Reset` header indicating when you can retry.
|
|
271
|
+
|
|
272
|
+
```javascript
|
|
273
|
+
try {
|
|
274
|
+
await client.bots.create({ name: 'Bot' });
|
|
275
|
+
} catch (error) {
|
|
276
|
+
if (error.statusCode === 429) {
|
|
277
|
+
const resetTime = error.response.headers['X-RateLimit-Reset'];
|
|
278
|
+
console.log('Rate limited. Retry after:', new Date(resetTime * 1000));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Support
|
|
284
|
+
|
|
285
|
+
- Documentation: https://natureco.me/docs
|
|
286
|
+
- SDK Documentation: https://natureco.me/docs/sdk
|
|
287
|
+
- Email: support@natureco.me
|
|
288
|
+
- Developers Portal: https://developers.natureco.me
|
|
289
|
+
|
|
290
|
+
## License
|
|
291
|
+
|
|
292
|
+
MIT License - see LICENSE file for details
|
package/index.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NatureCo JavaScript SDK
|
|
3
|
+
* @version 1.0.0
|
|
4
|
+
* @description Official JavaScript SDK for NatureCo API
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
class NatureCoClient {
|
|
8
|
+
constructor(apiKey, options = {}) {
|
|
9
|
+
if (!apiKey) {
|
|
10
|
+
throw new Error('API key is required');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
this.apiKey = apiKey;
|
|
14
|
+
this.baseURL = options.baseURL || 'https://api.natureco.me/api/v1';
|
|
15
|
+
this.timeout = options.timeout || 30000;
|
|
16
|
+
|
|
17
|
+
// Initialize modules
|
|
18
|
+
this.bots = new BotsModule(this);
|
|
19
|
+
this.channels = new ChannelsModule(this);
|
|
20
|
+
this.messages = new MessagesModule(this);
|
|
21
|
+
this.webhooks = new WebhooksModule(this);
|
|
22
|
+
this.analytics = new AnalyticsModule(this);
|
|
23
|
+
this.user = new UserModule(this);
|
|
24
|
+
this.apiKeys = new ApiKeysModule(this);
|
|
25
|
+
this.widget = new WidgetModule(this);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async request(method, endpoint, data = null) {
|
|
29
|
+
const url = `${this.baseURL}${endpoint}`;
|
|
30
|
+
const headers = {
|
|
31
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
32
|
+
'Content-Type': 'application/json',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const config = {
|
|
36
|
+
method,
|
|
37
|
+
headers,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
if (data) {
|
|
41
|
+
config.body = JSON.stringify(data);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const controller = new AbortController();
|
|
46
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
47
|
+
|
|
48
|
+
const response = await fetch(url, {
|
|
49
|
+
...config,
|
|
50
|
+
signal: controller.signal,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
clearTimeout(timeoutId);
|
|
54
|
+
|
|
55
|
+
const responseData = await response.json();
|
|
56
|
+
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
throw new NatureCoError(
|
|
59
|
+
responseData.error || 'Request failed',
|
|
60
|
+
response.status,
|
|
61
|
+
responseData
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return responseData;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (error.name === 'AbortError') {
|
|
68
|
+
throw new NatureCoError('Request timeout', 408);
|
|
69
|
+
}
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Bot Management Module
|
|
76
|
+
class BotsModule {
|
|
77
|
+
constructor(client) {
|
|
78
|
+
this.client = client;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async list() {
|
|
82
|
+
return this.client.request('GET', '/bots');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async create({ name, systemPrompt, model = 'gpt-4' }) {
|
|
86
|
+
return this.client.request('POST', '/bots', {
|
|
87
|
+
name,
|
|
88
|
+
system_prompt: systemPrompt,
|
|
89
|
+
model,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async get(botId) {
|
|
94
|
+
return this.client.request('GET', `/bots/${botId}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async update(botId, { name, systemPrompt, model }) {
|
|
98
|
+
const data = {};
|
|
99
|
+
if (name) data.name = name;
|
|
100
|
+
if (systemPrompt) data.system_prompt = systemPrompt;
|
|
101
|
+
if (model) data.model = model;
|
|
102
|
+
|
|
103
|
+
return this.client.request('PATCH', `/bots/${botId}`, data);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async delete(botId) {
|
|
107
|
+
return this.client.request('DELETE', `/bots/${botId}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Channel Integration Module
|
|
112
|
+
class ChannelsModule {
|
|
113
|
+
constructor(client) {
|
|
114
|
+
this.client = client;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async connect({ botId, platform, token, config = {} }) {
|
|
118
|
+
const validPlatforms = ['discord', 'telegram', 'slack', 'instagram'];
|
|
119
|
+
if (!validPlatforms.includes(platform)) {
|
|
120
|
+
throw new Error(`Invalid platform. Must be one of: ${validPlatforms.join(', ')}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return this.client.request('POST', `/bots/${botId}/channels`, {
|
|
124
|
+
platform,
|
|
125
|
+
token,
|
|
126
|
+
config,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async list(botId) {
|
|
131
|
+
return this.client.request('GET', `/bots/${botId}/channels`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async disconnect(botId, channelId) {
|
|
135
|
+
return this.client.request('DELETE', `/bots/${botId}/channels/${channelId}`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async test(botId, channelId) {
|
|
139
|
+
return this.client.request('POST', `/bots/${botId}/channels/${channelId}/test`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Messaging Module
|
|
144
|
+
class MessagesModule {
|
|
145
|
+
constructor(client) {
|
|
146
|
+
this.client = client;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async send({ botId, message, channel, userId }) {
|
|
150
|
+
return this.client.request('POST', `/bots/${botId}/messages`, {
|
|
151
|
+
message,
|
|
152
|
+
channel,
|
|
153
|
+
user_id: userId,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async history({ botId, limit = 50, offset = 0 }) {
|
|
158
|
+
return this.client.request('GET', `/bots/${botId}/messages?limit=${limit}&offset=${offset}`);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async get(botId, messageId) {
|
|
162
|
+
return this.client.request('GET', `/bots/${botId}/messages/${messageId}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Webhook Module
|
|
167
|
+
class WebhooksModule {
|
|
168
|
+
constructor(client) {
|
|
169
|
+
this.client = client;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async create({ botId, url, events, secret }) {
|
|
173
|
+
return this.client.request('POST', `/bots/${botId}/webhooks`, {
|
|
174
|
+
url,
|
|
175
|
+
events,
|
|
176
|
+
secret,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async list(botId) {
|
|
181
|
+
return this.client.request('GET', `/bots/${botId}/webhooks`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async get(botId, webhookId) {
|
|
185
|
+
return this.client.request('GET', `/bots/${botId}/webhooks/${webhookId}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async update(botId, webhookId, { url, events, active }) {
|
|
189
|
+
const data = {};
|
|
190
|
+
if (url) data.url = url;
|
|
191
|
+
if (events) data.events = events;
|
|
192
|
+
if (typeof active === 'boolean') data.active = active;
|
|
193
|
+
|
|
194
|
+
return this.client.request('PATCH', `/bots/${botId}/webhooks/${webhookId}`, data);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async delete(botId, webhookId) {
|
|
198
|
+
return this.client.request('DELETE', `/bots/${botId}/webhooks/${webhookId}`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async test(botId, webhookId) {
|
|
202
|
+
return this.client.request('POST', `/bots/${botId}/webhooks/${webhookId}/test`);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async logs(botId, webhookId, limit = 20) {
|
|
206
|
+
return this.client.request('GET', `/bots/${botId}/webhooks/${webhookId}/logs?limit=${limit}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Analytics Module
|
|
211
|
+
class AnalyticsModule {
|
|
212
|
+
constructor(client) {
|
|
213
|
+
this.client = client;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async get({ botId, period = 'week' }) {
|
|
217
|
+
const validPeriods = ['day', 'week', 'month'];
|
|
218
|
+
if (!validPeriods.includes(period)) {
|
|
219
|
+
throw new Error(`Invalid period. Must be one of: ${validPeriods.join(', ')}`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return this.client.request('GET', `/bots/${botId}/analytics?period=${period}`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async messages(botId, { startDate, endDate } = {}) {
|
|
226
|
+
let endpoint = `/bots/${botId}/analytics/messages`;
|
|
227
|
+
const params = [];
|
|
228
|
+
if (startDate) params.push(`start_date=${startDate}`);
|
|
229
|
+
if (endDate) params.push(`end_date=${endDate}`);
|
|
230
|
+
if (params.length) endpoint += `?${params.join('&')}`;
|
|
231
|
+
|
|
232
|
+
return this.client.request('GET', endpoint);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async channels(botId) {
|
|
236
|
+
return this.client.request('GET', `/bots/${botId}/analytics/channels`);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
async users(botId, { limit = 100 } = {}) {
|
|
240
|
+
return this.client.request('GET', `/bots/${botId}/analytics/users?limit=${limit}`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// User Module
|
|
245
|
+
class UserModule {
|
|
246
|
+
constructor(client) {
|
|
247
|
+
this.client = client;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async me() {
|
|
251
|
+
return this.client.request('GET', '/user/me');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async update({ displayName, bio, avatar }) {
|
|
255
|
+
const data = {};
|
|
256
|
+
if (displayName) data.display_name = displayName;
|
|
257
|
+
if (bio) data.bio = bio;
|
|
258
|
+
if (avatar) data.avatar = avatar;
|
|
259
|
+
|
|
260
|
+
return this.client.request('PATCH', '/user/me', data);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// API Keys Module
|
|
265
|
+
class ApiKeysModule {
|
|
266
|
+
constructor(client) {
|
|
267
|
+
this.client = client;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async list() {
|
|
271
|
+
return this.client.request('GET', '/api-keys');
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async create({ name, scopes = [] }) {
|
|
275
|
+
return this.client.request('POST', '/api-keys', {
|
|
276
|
+
name,
|
|
277
|
+
scopes,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async revoke(keyId) {
|
|
282
|
+
return this.client.request('DELETE', `/api-keys/${keyId}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Widget Module
|
|
287
|
+
class WidgetModule {
|
|
288
|
+
constructor(client) {
|
|
289
|
+
this.client = client;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
getEmbedCode({ botId, theme = 'light', position = 'bottom-right' }) {
|
|
293
|
+
return `<script src="https://cdn.natureco.me/widget.js" data-bot-id="${botId}" data-theme="${theme}" data-position="${position}"></script>`;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async updateSettings({ botId, theme, position, welcomeMessage, primaryColor }) {
|
|
297
|
+
const data = {};
|
|
298
|
+
if (theme) data.theme = theme;
|
|
299
|
+
if (position) data.position = position;
|
|
300
|
+
if (welcomeMessage) data.welcome_message = welcomeMessage;
|
|
301
|
+
if (primaryColor) data.primary_color = primaryColor;
|
|
302
|
+
|
|
303
|
+
return this.client.request('PATCH', `/bots/${botId}/widget`, data);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async getSettings(botId) {
|
|
307
|
+
return this.client.request('GET', `/bots/${botId}/widget`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Custom Error Class
|
|
312
|
+
class NatureCoError extends Error {
|
|
313
|
+
constructor(message, statusCode, response) {
|
|
314
|
+
super(message);
|
|
315
|
+
this.name = 'NatureCoError';
|
|
316
|
+
this.statusCode = statusCode;
|
|
317
|
+
this.response = response;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Export for Node.js and browser
|
|
322
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
323
|
+
module.exports = { NatureCoClient, NatureCoError };
|
|
324
|
+
} else if (typeof window !== 'undefined') {
|
|
325
|
+
window.NatureCoClient = NatureCoClient;
|
|
326
|
+
window.NatureCoError = NatureCoError;
|
|
327
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "natureco-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Official JavaScript SDK for NatureCo API",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"natureco",
|
|
12
|
+
"api",
|
|
13
|
+
"sdk",
|
|
14
|
+
"bot",
|
|
15
|
+
"ai",
|
|
16
|
+
"chatbot",
|
|
17
|
+
"discord",
|
|
18
|
+
"telegram",
|
|
19
|
+
"slack",
|
|
20
|
+
"instagram"
|
|
21
|
+
],
|
|
22
|
+
"author": "NatureCo",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/natureco/natureco-sdk"
|
|
27
|
+
},
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/natureco/natureco-sdk/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://natureco.me/docs/sdk",
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=14.0.0"
|
|
34
|
+
}
|
|
35
|
+
}
|