natureco-sdk 1.0.3 → 1.0.5
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 +608 -155
- package/index.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,228 +1,548 @@
|
|
|
1
|
-
# NatureCo
|
|
1
|
+
# NatureCo SDK
|
|
2
2
|
|
|
3
|
-
Official JavaScript SDK for NatureCo API
|
|
3
|
+
Official JavaScript SDK for NatureCo API - Build powerful AI chatbots and integrate them across multiple platforms.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/natureco-sdk)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## 📦 Installation
|
|
6
9
|
|
|
7
10
|
```bash
|
|
8
11
|
npm install natureco-sdk
|
|
9
12
|
```
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
Or using yarn:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
yarn add natureco-sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
For browser usage via CDN:
|
|
21
|
+
|
|
22
|
+
```html
|
|
23
|
+
<script src="https://unpkg.com/natureco-sdk@latest/index.js"></script>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 🚀 Quick Start
|
|
12
27
|
|
|
13
28
|
```javascript
|
|
14
29
|
const { NatureCoClient } = require('natureco-sdk');
|
|
15
30
|
|
|
16
|
-
// Initialize client
|
|
17
|
-
const client = new NatureCoClient('
|
|
31
|
+
// Initialize the client with your API key
|
|
32
|
+
const client = new NatureCoClient('nc_your_api_key_here');
|
|
18
33
|
|
|
19
|
-
// Create a bot
|
|
34
|
+
// Create a new bot
|
|
20
35
|
const bot = await client.bots.create({
|
|
21
36
|
name: 'My First Bot',
|
|
22
|
-
systemPrompt: 'You are a helpful assistant',
|
|
37
|
+
systemPrompt: 'You are a helpful assistant.',
|
|
23
38
|
model: 'gpt-4'
|
|
24
39
|
});
|
|
25
40
|
|
|
26
41
|
console.log('Bot created:', bot);
|
|
27
42
|
```
|
|
28
43
|
|
|
29
|
-
##
|
|
44
|
+
## 🔑 Authentication
|
|
45
|
+
|
|
46
|
+
### Getting Your API Key
|
|
47
|
+
|
|
48
|
+
1. Go to [NatureCo Developers Portal](https://natureco.me/developers)
|
|
49
|
+
2. Navigate to **API Keys** section
|
|
50
|
+
3. Click **Generate New Key**
|
|
51
|
+
4. Copy your API key (starts with `nc_` or `nco_`)
|
|
52
|
+
|
|
53
|
+
### Initialize Client
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
const { NatureCoClient } = require('natureco-sdk');
|
|
57
|
+
|
|
58
|
+
const client = new NatureCoClient('nc_your_api_key_here', {
|
|
59
|
+
baseURL: 'https://api.natureco.me/api/v1', // Optional: custom API endpoint
|
|
60
|
+
timeout: 30000 // Optional: request timeout in ms (default: 30000)
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## 🤖 Bot Management
|
|
65
|
+
|
|
66
|
+
### Create a Bot
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
const bot = await client.bots.create({
|
|
70
|
+
name: 'Customer Support Bot',
|
|
71
|
+
systemPrompt: 'You are a friendly customer support assistant. Help users with their questions.',
|
|
72
|
+
model: 'gpt-4' // Options: 'gpt-4', 'gpt-3.5-turbo', 'claude-3'
|
|
73
|
+
});
|
|
30
74
|
|
|
31
|
-
|
|
75
|
+
console.log('Bot ID:', bot.botId);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### List All Bots
|
|
32
79
|
|
|
33
80
|
```javascript
|
|
34
|
-
// List all bots
|
|
35
81
|
const bots = await client.bots.list();
|
|
36
82
|
|
|
37
|
-
|
|
38
|
-
|
|
83
|
+
bots.forEach(bot => {
|
|
84
|
+
console.log(`${bot.name} (${bot.botId})`);
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Get Bot Details
|
|
89
|
+
|
|
90
|
+
```javascript
|
|
91
|
+
const bot = await client.bots.get('bot_123456');
|
|
39
92
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
93
|
+
console.log('Bot name:', bot.name);
|
|
94
|
+
console.log('System prompt:', bot.systemPrompt);
|
|
95
|
+
console.log('Model:', bot.model);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Update a Bot
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
const updatedBot = await client.bots.update('bot_123456', {
|
|
102
|
+
name: 'Updated Bot Name',
|
|
103
|
+
systemPrompt: 'New system prompt',
|
|
104
|
+
model: 'gpt-4'
|
|
44
105
|
});
|
|
106
|
+
```
|
|
45
107
|
|
|
46
|
-
|
|
47
|
-
|
|
108
|
+
### Delete a Bot
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
await client.bots.delete('bot_123456');
|
|
112
|
+
console.log('Bot deleted successfully');
|
|
48
113
|
```
|
|
49
114
|
|
|
50
|
-
|
|
115
|
+
## 📱 Channel Integration
|
|
116
|
+
|
|
117
|
+
Connect your bot to Discord, Telegram, Slack, Instagram, and more.
|
|
118
|
+
|
|
119
|
+
### Connect Discord
|
|
51
120
|
|
|
52
121
|
```javascript
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
botId: 'bot_id',
|
|
122
|
+
const channel = await client.channels.connect({
|
|
123
|
+
botId: 'bot_123456',
|
|
56
124
|
platform: 'discord',
|
|
57
|
-
token: '
|
|
125
|
+
token: 'your_discord_bot_token',
|
|
126
|
+
config: {
|
|
127
|
+
guildId: 'your_guild_id', // Optional
|
|
128
|
+
commandPrefix: '!' // Optional
|
|
129
|
+
}
|
|
58
130
|
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Connect Telegram
|
|
59
134
|
|
|
60
|
-
|
|
61
|
-
await client.channels.connect({
|
|
62
|
-
botId: '
|
|
135
|
+
```javascript
|
|
136
|
+
const channel = await client.channels.connect({
|
|
137
|
+
botId: 'bot_123456',
|
|
63
138
|
platform: 'telegram',
|
|
64
|
-
token: '
|
|
139
|
+
token: 'your_telegram_bot_token'
|
|
65
140
|
});
|
|
141
|
+
```
|
|
66
142
|
|
|
67
|
-
|
|
68
|
-
|
|
143
|
+
### Connect Slack
|
|
144
|
+
|
|
145
|
+
```javascript
|
|
146
|
+
const channel = await client.channels.connect({
|
|
147
|
+
botId: 'bot_123456',
|
|
148
|
+
platform: 'slack',
|
|
149
|
+
token: 'xoxb-your-slack-bot-token',
|
|
150
|
+
config: {
|
|
151
|
+
signingSecret: 'your_signing_secret'
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
```
|
|
69
155
|
|
|
70
|
-
|
|
71
|
-
await client.channels.disconnect('bot_id', 'channel_id');
|
|
156
|
+
### Connect Instagram
|
|
72
157
|
|
|
73
|
-
|
|
74
|
-
await client.channels.
|
|
158
|
+
```javascript
|
|
159
|
+
const channel = await client.channels.connect({
|
|
160
|
+
botId: 'bot_123456',
|
|
161
|
+
platform: 'instagram',
|
|
162
|
+
token: 'your_instagram_access_token',
|
|
163
|
+
config: {
|
|
164
|
+
pageId: 'your_page_id',
|
|
165
|
+
verifyToken: 'your_verify_token'
|
|
166
|
+
}
|
|
167
|
+
});
|
|
75
168
|
```
|
|
76
169
|
|
|
77
|
-
###
|
|
170
|
+
### List Connected Channels
|
|
78
171
|
|
|
79
172
|
```javascript
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
173
|
+
const channels = await client.channels.list('bot_123456');
|
|
174
|
+
|
|
175
|
+
channels.forEach(channel => {
|
|
176
|
+
console.log(`${channel.platform}: ${channel.isActive ? 'Active' : 'Inactive'}`);
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Disconnect a Channel
|
|
181
|
+
|
|
182
|
+
```javascript
|
|
183
|
+
await client.channels.disconnect('bot_123456', 'channel_789');
|
|
184
|
+
console.log('Channel disconnected');
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Test Channel Connection
|
|
188
|
+
|
|
189
|
+
```javascript
|
|
190
|
+
const result = await client.channels.test('bot_123456', 'channel_789');
|
|
191
|
+
console.log('Test result:', result.success ? 'Passed' : 'Failed');
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## 💬 Messaging
|
|
195
|
+
|
|
196
|
+
### Send a Message
|
|
197
|
+
|
|
198
|
+
```javascript
|
|
199
|
+
const response = await client.messages.send({
|
|
200
|
+
botId: 'bot_123456',
|
|
201
|
+
message: 'Hello! How can I help you today?',
|
|
202
|
+
channel: 'web', // 'web', 'discord', 'telegram', etc.
|
|
85
203
|
userId: 'user_123'
|
|
86
204
|
});
|
|
87
205
|
|
|
88
|
-
|
|
206
|
+
console.log('Bot response:', response.reply);
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Get Message History
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
89
212
|
const messages = await client.messages.history({
|
|
90
|
-
botId: '
|
|
213
|
+
botId: 'bot_123456',
|
|
91
214
|
limit: 50,
|
|
92
215
|
offset: 0
|
|
93
216
|
});
|
|
94
217
|
|
|
95
|
-
|
|
96
|
-
|
|
218
|
+
messages.forEach(msg => {
|
|
219
|
+
console.log(`[${msg.timestamp}] ${msg.userId}: ${msg.message}`);
|
|
220
|
+
console.log(`Bot: ${msg.reply}`);
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Get Specific Message
|
|
225
|
+
|
|
226
|
+
```javascript
|
|
227
|
+
const message = await client.messages.get('bot_123456', 'msg_789');
|
|
228
|
+
console.log('Message:', message);
|
|
97
229
|
```
|
|
98
230
|
|
|
99
|
-
|
|
231
|
+
## 🔔 Webhooks
|
|
232
|
+
|
|
233
|
+
Receive real-time notifications when events occur.
|
|
234
|
+
|
|
235
|
+
### Create a Webhook
|
|
100
236
|
|
|
101
237
|
```javascript
|
|
102
|
-
// Create webhook
|
|
103
238
|
const webhook = await client.webhooks.create({
|
|
104
|
-
botId: '
|
|
105
|
-
url: 'https://your-
|
|
106
|
-
events: ['message.received', 'message.sent'],
|
|
107
|
-
secret: 'your_webhook_secret'
|
|
239
|
+
botId: 'bot_123456',
|
|
240
|
+
url: 'https://your-domain.com/webhook',
|
|
241
|
+
events: ['message.received', 'message.sent', 'bot.error'],
|
|
242
|
+
secret: 'your_webhook_secret' // Optional: for signature verification
|
|
108
243
|
});
|
|
109
244
|
|
|
110
|
-
|
|
111
|
-
|
|
245
|
+
console.log('Webhook ID:', webhook.id);
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Available Events
|
|
249
|
+
|
|
250
|
+
- `message.received` - New message received from user
|
|
251
|
+
- `message.sent` - Bot sent a message
|
|
252
|
+
- `bot.error` - Bot encountered an error
|
|
253
|
+
- `channel.connected` - New channel connected
|
|
254
|
+
- `channel.disconnected` - Channel disconnected
|
|
255
|
+
- `user.joined` - New user started conversation
|
|
256
|
+
- `analytics.daily` - Daily analytics summary
|
|
257
|
+
|
|
258
|
+
### List Webhooks
|
|
112
259
|
|
|
113
|
-
|
|
114
|
-
await client.webhooks.
|
|
115
|
-
|
|
260
|
+
```javascript
|
|
261
|
+
const webhooks = await client.webhooks.list('bot_123456');
|
|
262
|
+
|
|
263
|
+
webhooks.forEach(webhook => {
|
|
264
|
+
console.log(`${webhook.url}: ${webhook.active ? 'Active' : 'Inactive'}`);
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Update Webhook
|
|
269
|
+
|
|
270
|
+
```javascript
|
|
271
|
+
const updated = await client.webhooks.update('bot_123456', 'webhook_789', {
|
|
272
|
+
url: 'https://new-domain.com/webhook',
|
|
273
|
+
events: ['message.received'],
|
|
116
274
|
active: true
|
|
117
275
|
});
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Delete Webhook
|
|
118
279
|
|
|
119
|
-
|
|
120
|
-
await client.webhooks.
|
|
280
|
+
```javascript
|
|
281
|
+
await client.webhooks.delete('bot_123456', 'webhook_789');
|
|
282
|
+
```
|
|
121
283
|
|
|
122
|
-
|
|
123
|
-
const logs = await client.webhooks.logs('bot_id', 'webhook_id', 20);
|
|
284
|
+
### Test Webhook
|
|
124
285
|
|
|
125
|
-
|
|
126
|
-
await client.webhooks.
|
|
286
|
+
```javascript
|
|
287
|
+
const result = await client.webhooks.test('bot_123456', 'webhook_789');
|
|
288
|
+
console.log('Test result:', result);
|
|
127
289
|
```
|
|
128
290
|
|
|
129
|
-
###
|
|
291
|
+
### View Webhook Logs
|
|
130
292
|
|
|
131
293
|
```javascript
|
|
132
|
-
|
|
294
|
+
const logs = await client.webhooks.logs('bot_123456', 'webhook_789', 20);
|
|
295
|
+
|
|
296
|
+
logs.forEach(log => {
|
|
297
|
+
console.log(`[${log.timestamp}] ${log.event}: ${log.status}`);
|
|
298
|
+
});
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### Webhook Payload Example
|
|
302
|
+
|
|
303
|
+
```javascript
|
|
304
|
+
// Your webhook endpoint will receive:
|
|
305
|
+
{
|
|
306
|
+
"event": "message.received",
|
|
307
|
+
"timestamp": "2024-01-15T10:30:00Z",
|
|
308
|
+
"botId": "bot_123456",
|
|
309
|
+
"data": {
|
|
310
|
+
"messageId": "msg_789",
|
|
311
|
+
"userId": "user_123",
|
|
312
|
+
"message": "Hello bot!",
|
|
313
|
+
"channel": "discord",
|
|
314
|
+
"metadata": {
|
|
315
|
+
"username": "john_doe",
|
|
316
|
+
"channelId": "channel_456"
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### Verify Webhook Signature
|
|
323
|
+
|
|
324
|
+
```javascript
|
|
325
|
+
const crypto = require('crypto');
|
|
326
|
+
|
|
327
|
+
function verifyWebhookSignature(payload, signature, secret) {
|
|
328
|
+
const hmac = crypto.createHmac('sha256', secret);
|
|
329
|
+
const digest = hmac.update(JSON.stringify(payload)).digest('hex');
|
|
330
|
+
return signature === digest;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// In your webhook handler:
|
|
334
|
+
app.post('/webhook', (req, res) => {
|
|
335
|
+
const signature = req.headers['x-natureco-signature'];
|
|
336
|
+
const isValid = verifyWebhookSignature(req.body, signature, 'your_webhook_secret');
|
|
337
|
+
|
|
338
|
+
if (!isValid) {
|
|
339
|
+
return res.status(401).send('Invalid signature');
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Process webhook...
|
|
343
|
+
res.status(200).send('OK');
|
|
344
|
+
});
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
## 🎨 Web Widget
|
|
348
|
+
|
|
349
|
+
Embed a chat widget on your website.
|
|
350
|
+
|
|
351
|
+
### Basic Embed
|
|
352
|
+
|
|
353
|
+
```javascript
|
|
354
|
+
const embedCode = client.widget.getEmbedCode('bot_123456');
|
|
355
|
+
console.log(embedCode);
|
|
356
|
+
// Output: <script src="https://api.natureco.me/widget.js" data-bot-id="bot_123456" data-theme="light" data-position="bottom-right"></script>
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
### Custom Theme and Position
|
|
360
|
+
|
|
361
|
+
```javascript
|
|
362
|
+
const embedCode = client.widget.getEmbedCode({
|
|
363
|
+
botId: 'bot_123456',
|
|
364
|
+
theme: 'dark', // 'light' or 'dark'
|
|
365
|
+
position: 'bottom-left' // 'bottom-right', 'bottom-left', 'top-right', 'top-left'
|
|
366
|
+
});
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
### HTML Integration
|
|
370
|
+
|
|
371
|
+
```html
|
|
372
|
+
<!DOCTYPE html>
|
|
373
|
+
<html>
|
|
374
|
+
<head>
|
|
375
|
+
<title>My Website</title>
|
|
376
|
+
</head>
|
|
377
|
+
<body>
|
|
378
|
+
<h1>Welcome to my website</h1>
|
|
379
|
+
|
|
380
|
+
<!-- Add this before closing </body> tag -->
|
|
381
|
+
<script
|
|
382
|
+
src="https://api.natureco.me/widget.js"
|
|
383
|
+
data-bot-id="bot_123456"
|
|
384
|
+
data-theme="dark"
|
|
385
|
+
data-position="bottom-right"
|
|
386
|
+
data-welcome-message="Hi! How can I help you today?"
|
|
387
|
+
data-primary-color="#22c55e">
|
|
388
|
+
</script>
|
|
389
|
+
</body>
|
|
390
|
+
</html>
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
### Update Widget Settings
|
|
394
|
+
|
|
395
|
+
```javascript
|
|
396
|
+
await client.widget.updateSettings({
|
|
397
|
+
botId: 'bot_123456',
|
|
398
|
+
theme: 'dark',
|
|
399
|
+
position: 'bottom-right',
|
|
400
|
+
welcomeMessage: 'Hello! How can I assist you?',
|
|
401
|
+
primaryColor: '#22c55e'
|
|
402
|
+
});
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### Get Widget Settings
|
|
406
|
+
|
|
407
|
+
```javascript
|
|
408
|
+
const settings = await client.widget.getSettings('bot_123456');
|
|
409
|
+
console.log('Widget settings:', settings);
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
### Widget Customization Options
|
|
413
|
+
|
|
414
|
+
```html
|
|
415
|
+
<script
|
|
416
|
+
src="https://api.natureco.me/widget.js"
|
|
417
|
+
data-bot-id="bot_123456"
|
|
418
|
+
data-theme="dark"
|
|
419
|
+
data-position="bottom-right"
|
|
420
|
+
data-welcome-message="Hi there! 👋"
|
|
421
|
+
data-primary-color="#22c55e"
|
|
422
|
+
data-button-size="60"
|
|
423
|
+
data-window-width="400"
|
|
424
|
+
data-window-height="600"
|
|
425
|
+
data-z-index="9999">
|
|
426
|
+
</script>
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
## 📊 Analytics
|
|
430
|
+
|
|
431
|
+
### Get Bot Analytics
|
|
432
|
+
|
|
433
|
+
```javascript
|
|
434
|
+
// Get weekly analytics
|
|
133
435
|
const analytics = await client.analytics.get({
|
|
134
|
-
botId: '
|
|
436
|
+
botId: 'bot_123456',
|
|
135
437
|
period: 'week' // 'day', 'week', or 'month'
|
|
136
438
|
});
|
|
137
439
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
440
|
+
console.log('Total messages:', analytics.totalMessages);
|
|
441
|
+
console.log('Active users:', analytics.activeUsers);
|
|
442
|
+
console.log('Average response time:', analytics.avgResponseTime);
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
### Message Analytics
|
|
446
|
+
|
|
447
|
+
```javascript
|
|
448
|
+
const messageStats = await client.analytics.messages('bot_123456', {
|
|
449
|
+
startDate: '2024-01-01',
|
|
450
|
+
endDate: '2024-01-31'
|
|
142
451
|
});
|
|
143
452
|
|
|
144
|
-
|
|
145
|
-
|
|
453
|
+
console.log('Messages sent:', messageStats.sent);
|
|
454
|
+
console.log('Messages received:', messageStats.received);
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### Channel Analytics
|
|
458
|
+
|
|
459
|
+
```javascript
|
|
460
|
+
const channelStats = await client.analytics.channels('bot_123456');
|
|
461
|
+
|
|
462
|
+
channelStats.forEach(channel => {
|
|
463
|
+
console.log(`${channel.platform}: ${channel.messageCount} messages`);
|
|
464
|
+
});
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
### User Analytics
|
|
146
468
|
|
|
147
|
-
|
|
148
|
-
const
|
|
469
|
+
```javascript
|
|
470
|
+
const users = await client.analytics.users('bot_123456', { limit: 100 });
|
|
471
|
+
|
|
472
|
+
users.forEach(user => {
|
|
473
|
+
console.log(`${user.userId}: ${user.messageCount} messages, last seen ${user.lastSeen}`);
|
|
474
|
+
});
|
|
149
475
|
```
|
|
150
476
|
|
|
151
|
-
|
|
477
|
+
## 👤 User Management
|
|
478
|
+
|
|
479
|
+
### Get Current User
|
|
152
480
|
|
|
153
481
|
```javascript
|
|
154
|
-
// Get current user
|
|
155
482
|
const user = await client.user.me();
|
|
156
483
|
|
|
157
|
-
|
|
484
|
+
console.log('User ID:', user.id);
|
|
485
|
+
console.log('Display name:', user.displayName);
|
|
486
|
+
console.log('Email:', user.email);
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
### Update User Profile
|
|
490
|
+
|
|
491
|
+
```javascript
|
|
158
492
|
await client.user.update({
|
|
159
|
-
displayName: '
|
|
160
|
-
bio: '
|
|
161
|
-
avatar: 'https://example.com/avatar.
|
|
493
|
+
displayName: 'John Doe',
|
|
494
|
+
bio: 'AI enthusiast and developer',
|
|
495
|
+
avatar: 'https://example.com/avatar.jpg'
|
|
162
496
|
});
|
|
163
497
|
```
|
|
164
498
|
|
|
165
|
-
|
|
499
|
+
## 🔐 API Key Management
|
|
500
|
+
|
|
501
|
+
### List API Keys
|
|
166
502
|
|
|
167
503
|
```javascript
|
|
168
|
-
// List API keys
|
|
169
504
|
const keys = await client.apiKeys.list();
|
|
170
505
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
name: 'Production Key',
|
|
174
|
-
scopes: ['bots:read', 'bots:write']
|
|
506
|
+
keys.forEach(key => {
|
|
507
|
+
console.log(`${key.name}: ${key.scopes.join(', ')}`);
|
|
175
508
|
});
|
|
176
|
-
|
|
177
|
-
// Revoke API key
|
|
178
|
-
await client.apiKeys.revoke('key_id');
|
|
179
509
|
```
|
|
180
510
|
|
|
181
|
-
###
|
|
511
|
+
### Create New API Key
|
|
182
512
|
|
|
183
513
|
```javascript
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
// Get embed code (with options)
|
|
188
|
-
const embedCode = client.widget.getEmbedCode({
|
|
189
|
-
botId: 'bot_id',
|
|
190
|
-
theme: 'light', // 'light' or 'dark'
|
|
191
|
-
position: 'bottom-right' // 'bottom-right', 'bottom-left', etc.
|
|
514
|
+
const newKey = await client.apiKeys.create({
|
|
515
|
+
name: 'Production Key',
|
|
516
|
+
scopes: ['bots:read', 'bots:write', 'messages:send']
|
|
192
517
|
});
|
|
193
518
|
|
|
194
|
-
|
|
195
|
-
|
|
519
|
+
console.log('New API key:', newKey.key);
|
|
520
|
+
// IMPORTANT: Save this key securely, it won't be shown again!
|
|
521
|
+
```
|
|
196
522
|
|
|
197
|
-
|
|
198
|
-
await client.widget.updateSettings({
|
|
199
|
-
botId: 'bot_id',
|
|
200
|
-
theme: 'dark',
|
|
201
|
-
position: 'bottom-right',
|
|
202
|
-
welcomeMessage: 'Hello! How can I help you?',
|
|
203
|
-
primaryColor: '#10B981'
|
|
204
|
-
});
|
|
523
|
+
### Revoke API Key
|
|
205
524
|
|
|
206
|
-
|
|
207
|
-
|
|
525
|
+
```javascript
|
|
526
|
+
await client.apiKeys.revoke('key_123456');
|
|
527
|
+
console.log('API key revoked');
|
|
208
528
|
```
|
|
209
529
|
|
|
210
|
-
##
|
|
530
|
+
## 🛠️ Advanced Usage
|
|
531
|
+
|
|
532
|
+
### Error Handling
|
|
211
533
|
|
|
212
534
|
```javascript
|
|
213
535
|
const { NatureCoClient, NatureCoError } = require('natureco-sdk');
|
|
214
536
|
|
|
215
|
-
const client = new NatureCoClient('nco_your_api_key');
|
|
216
|
-
|
|
217
537
|
try {
|
|
218
538
|
const bot = await client.bots.create({
|
|
219
|
-
name: '
|
|
220
|
-
systemPrompt: 'You are helpful'
|
|
539
|
+
name: 'My Bot',
|
|
540
|
+
systemPrompt: 'You are helpful.'
|
|
221
541
|
});
|
|
222
542
|
} catch (error) {
|
|
223
543
|
if (error instanceof NatureCoError) {
|
|
224
544
|
console.error('API Error:', error.message);
|
|
225
|
-
console.error('Status
|
|
545
|
+
console.error('Status code:', error.statusCode);
|
|
226
546
|
console.error('Response:', error.response);
|
|
227
547
|
} else {
|
|
228
548
|
console.error('Unexpected error:', error);
|
|
@@ -230,69 +550,202 @@ try {
|
|
|
230
550
|
}
|
|
231
551
|
```
|
|
232
552
|
|
|
233
|
-
|
|
553
|
+
### Custom Timeout
|
|
234
554
|
|
|
235
555
|
```javascript
|
|
236
|
-
const client = new NatureCoClient('
|
|
237
|
-
|
|
238
|
-
timeout: 30000 // Request timeout in milliseconds (default: 30000)
|
|
556
|
+
const client = new NatureCoClient('nc_your_api_key', {
|
|
557
|
+
timeout: 60000 // 60 seconds
|
|
239
558
|
});
|
|
240
559
|
```
|
|
241
560
|
|
|
242
|
-
|
|
561
|
+
### Custom Base URL (Self-hosted)
|
|
243
562
|
|
|
244
|
-
|
|
563
|
+
```javascript
|
|
564
|
+
const client = new NatureCoClient('nc_your_api_key', {
|
|
565
|
+
baseURL: 'https://your-custom-domain.com/api/v1'
|
|
566
|
+
});
|
|
567
|
+
```
|
|
245
568
|
|
|
246
|
-
|
|
247
|
-
import { NatureCoClient, NatureCoError } from 'natureco-sdk';
|
|
569
|
+
### Retry Logic
|
|
248
570
|
|
|
249
|
-
|
|
571
|
+
```javascript
|
|
572
|
+
async function retryRequest(fn, maxRetries = 3) {
|
|
573
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
574
|
+
try {
|
|
575
|
+
return await fn();
|
|
576
|
+
} catch (error) {
|
|
577
|
+
if (i === maxRetries - 1) throw error;
|
|
578
|
+
await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1)));
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
250
582
|
|
|
251
|
-
//
|
|
252
|
-
const bot = await
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
})
|
|
583
|
+
// Usage
|
|
584
|
+
const bot = await retryRequest(() =>
|
|
585
|
+
client.bots.create({
|
|
586
|
+
name: 'My Bot',
|
|
587
|
+
systemPrompt: 'You are helpful.'
|
|
588
|
+
})
|
|
589
|
+
);
|
|
257
590
|
```
|
|
258
591
|
|
|
259
|
-
## Browser Usage
|
|
260
|
-
|
|
261
|
-
You can also use the SDK in the browser via CDN:
|
|
592
|
+
## 🌐 Browser Usage
|
|
262
593
|
|
|
263
594
|
```html
|
|
264
|
-
|
|
265
|
-
<
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
595
|
+
<!DOCTYPE html>
|
|
596
|
+
<html>
|
|
597
|
+
<head>
|
|
598
|
+
<title>NatureCo SDK Browser Example</title>
|
|
599
|
+
</head>
|
|
600
|
+
<body>
|
|
601
|
+
<h1>Chat with Bot</h1>
|
|
602
|
+
<input type="text" id="message" placeholder="Type a message...">
|
|
603
|
+
<button onclick="sendMessage()">Send</button>
|
|
604
|
+
<div id="response"></div>
|
|
605
|
+
|
|
606
|
+
<script src="https://unpkg.com/natureco-sdk@latest/index.js"></script>
|
|
607
|
+
<script>
|
|
608
|
+
const client = new NatureCoClient('nc_your_api_key');
|
|
609
|
+
|
|
610
|
+
async function sendMessage() {
|
|
611
|
+
const message = document.getElementById('message').value;
|
|
612
|
+
|
|
613
|
+
try {
|
|
614
|
+
const response = await client.messages.send({
|
|
615
|
+
botId: 'bot_123456',
|
|
616
|
+
message: message,
|
|
617
|
+
channel: 'web',
|
|
618
|
+
userId: 'user_' + Date.now()
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
document.getElementById('response').innerText = response.reply;
|
|
622
|
+
} catch (error) {
|
|
623
|
+
console.error('Error:', error);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
</script>
|
|
627
|
+
</body>
|
|
628
|
+
</html>
|
|
272
629
|
```
|
|
273
630
|
|
|
274
|
-
##
|
|
275
|
-
|
|
276
|
-
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.
|
|
631
|
+
## 📚 Complete Example
|
|
277
632
|
|
|
278
633
|
```javascript
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
634
|
+
const { NatureCoClient } = require('natureco-sdk');
|
|
635
|
+
|
|
636
|
+
async function main() {
|
|
637
|
+
// Initialize client
|
|
638
|
+
const client = new NatureCoClient('nc_your_api_key');
|
|
639
|
+
|
|
640
|
+
try {
|
|
641
|
+
// Create a bot
|
|
642
|
+
const bot = await client.bots.create({
|
|
643
|
+
name: 'Customer Support Bot',
|
|
644
|
+
systemPrompt: 'You are a helpful customer support assistant.',
|
|
645
|
+
model: 'gpt-4'
|
|
646
|
+
});
|
|
647
|
+
console.log('✅ Bot created:', bot.botId);
|
|
648
|
+
|
|
649
|
+
// Connect to Discord
|
|
650
|
+
await client.channels.connect({
|
|
651
|
+
botId: bot.botId,
|
|
652
|
+
platform: 'discord',
|
|
653
|
+
token: process.env.DISCORD_TOKEN
|
|
654
|
+
});
|
|
655
|
+
console.log('✅ Discord connected');
|
|
656
|
+
|
|
657
|
+
// Create webhook
|
|
658
|
+
const webhook = await client.webhooks.create({
|
|
659
|
+
botId: bot.botId,
|
|
660
|
+
url: 'https://your-domain.com/webhook',
|
|
661
|
+
events: ['message.received', 'message.sent'],
|
|
662
|
+
secret: 'your_secret'
|
|
663
|
+
});
|
|
664
|
+
console.log('✅ Webhook created:', webhook.id);
|
|
665
|
+
|
|
666
|
+
// Get embed code for website
|
|
667
|
+
const embedCode = client.widget.getEmbedCode({
|
|
668
|
+
botId: bot.botId,
|
|
669
|
+
theme: 'dark',
|
|
670
|
+
position: 'bottom-right'
|
|
671
|
+
});
|
|
672
|
+
console.log('✅ Widget embed code:', embedCode);
|
|
673
|
+
|
|
674
|
+
// Send a test message
|
|
675
|
+
const response = await client.messages.send({
|
|
676
|
+
botId: bot.botId,
|
|
677
|
+
message: 'Hello! This is a test.',
|
|
678
|
+
channel: 'web',
|
|
679
|
+
userId: 'test_user'
|
|
680
|
+
});
|
|
681
|
+
console.log('✅ Bot response:', response.reply);
|
|
682
|
+
|
|
683
|
+
// Get analytics
|
|
684
|
+
const analytics = await client.analytics.get({
|
|
685
|
+
botId: bot.botId,
|
|
686
|
+
period: 'week'
|
|
687
|
+
});
|
|
688
|
+
console.log('✅ Analytics:', analytics);
|
|
689
|
+
|
|
690
|
+
} catch (error) {
|
|
691
|
+
console.error('❌ Error:', error.message);
|
|
285
692
|
}
|
|
286
693
|
}
|
|
694
|
+
|
|
695
|
+
main();
|
|
287
696
|
```
|
|
288
697
|
|
|
289
|
-
##
|
|
698
|
+
## 🔗 Useful Links
|
|
699
|
+
|
|
700
|
+
- [Official Documentation](https://natureco.me/docs)
|
|
701
|
+
- [API Reference](https://natureco.me/docs/api)
|
|
702
|
+
- [Developers Portal](https://natureco.me/developers)
|
|
703
|
+
- [GitHub Repository](https://github.com/natureco/natureco-sdk)
|
|
704
|
+
- [Support](https://natureco.me/support)
|
|
705
|
+
|
|
706
|
+
## 📄 License
|
|
707
|
+
|
|
708
|
+
MIT License - see [LICENSE](LICENSE) file for details
|
|
709
|
+
|
|
710
|
+
## 🤝 Contributing
|
|
711
|
+
|
|
712
|
+
Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
713
|
+
|
|
714
|
+
## 💬 Support
|
|
290
715
|
|
|
291
|
-
- Documentation: https://natureco.me/docs
|
|
292
|
-
- SDK Documentation: https://natureco.me/docs/sdk
|
|
293
716
|
- Email: support@natureco.me
|
|
294
|
-
-
|
|
717
|
+
- Discord: [Join our community](https://discord.gg/natureco)
|
|
718
|
+
- Twitter: [@natureco](https://twitter.com/natureco)
|
|
719
|
+
|
|
720
|
+
## 📝 Changelog
|
|
721
|
+
|
|
722
|
+
### v1.0.5 (Latest)
|
|
723
|
+
- Updated README with comprehensive examples
|
|
724
|
+
- Added webhook signature verification
|
|
725
|
+
- Improved error handling documentation
|
|
726
|
+
- Added browser usage examples
|
|
727
|
+
|
|
728
|
+
### v1.0.4
|
|
729
|
+
- Added widget customization options
|
|
730
|
+
- Improved API key validation
|
|
731
|
+
- Bug fixes and performance improvements
|
|
732
|
+
|
|
733
|
+
### v1.0.3
|
|
734
|
+
- Added analytics module
|
|
735
|
+
- Added user management
|
|
736
|
+
- Added API key management
|
|
737
|
+
|
|
738
|
+
### v1.0.2
|
|
739
|
+
- Added webhook support
|
|
740
|
+
- Improved error handling
|
|
741
|
+
- Added timeout configuration
|
|
742
|
+
|
|
743
|
+
### v1.0.1
|
|
744
|
+
- Initial release
|
|
745
|
+
- Bot management
|
|
746
|
+
- Channel integration
|
|
747
|
+
- Messaging support
|
|
295
748
|
|
|
296
|
-
|
|
749
|
+
---
|
|
297
750
|
|
|
298
|
-
|
|
751
|
+
Made with ❤️ by [NatureCo](https://natureco.me)
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* NatureCo JavaScript SDK
|
|
3
|
-
* @version 1.0.
|
|
3
|
+
* @version 1.0.5
|
|
4
4
|
* @description Official JavaScript SDK for NatureCo API
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -316,7 +316,7 @@ class WidgetModule {
|
|
|
316
316
|
throw new Error('botId is required');
|
|
317
317
|
}
|
|
318
318
|
|
|
319
|
-
return `<script src="https://
|
|
319
|
+
return `<script src="https://api.natureco.me/widget.js" data-bot-id="${botId}" data-theme="${finalTheme}" data-position="${finalPosition}"></script>`;
|
|
320
320
|
}
|
|
321
321
|
|
|
322
322
|
async updateSettings({ botId, theme, position, welcomeMessage, primaryColor }) {
|