commune-ai 0.1.4 → 0.1.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 +135 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -224,6 +224,141 @@ console.log(`Found ${searchResults.length} relevant emails`);
|
|
|
224
224
|
|
|
225
225
|
---
|
|
226
226
|
|
|
227
|
+
## Manage Inboxes Programmatically
|
|
228
|
+
|
|
229
|
+
Create and manage email inboxes for your agents programmatically.
|
|
230
|
+
|
|
231
|
+
### Create Multiple Inboxes
|
|
232
|
+
|
|
233
|
+
Set up different inboxes for different purposes:
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
import { CommuneClient } from "commune-ai";
|
|
237
|
+
|
|
238
|
+
const client = new CommuneClient({
|
|
239
|
+
apiKey: process.env.COMMUNE_API_KEY,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// Create inboxes for different departments
|
|
243
|
+
async function setupInboxes(domainId: string) {
|
|
244
|
+
const supportInbox = await client.inboxes.create(domainId, {
|
|
245
|
+
localPart: "support", // Creates support@yourdomain.com
|
|
246
|
+
agent: {
|
|
247
|
+
id: "support-agent",
|
|
248
|
+
name: "Support Bot",
|
|
249
|
+
metadata: { department: "customer-success" }
|
|
250
|
+
},
|
|
251
|
+
status: "active",
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
const salesInbox = await client.inboxes.create(domainId, {
|
|
255
|
+
localPart: "sales", // Creates sales@yourdomain.com
|
|
256
|
+
agent: {
|
|
257
|
+
id: "sales-agent",
|
|
258
|
+
name: "Sales Bot",
|
|
259
|
+
metadata: { department: "sales" }
|
|
260
|
+
},
|
|
261
|
+
status: "active",
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
return { supportInbox, salesInbox };
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Set Webhooks for Specific Inboxes
|
|
269
|
+
|
|
270
|
+
Configure webhooks per inbox to route emails to different handlers:
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
async function configureWebhooks(domainId: string, inboxes: any) {
|
|
274
|
+
// Support inbox webhook
|
|
275
|
+
await client.inboxes.setWebhook(domainId, inboxes.supportInbox.id, {
|
|
276
|
+
endpoint: "https://your-api.com/webhooks/support",
|
|
277
|
+
events: ["email.received"],
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
// Sales inbox webhook
|
|
281
|
+
await client.inboxes.setWebhook(domainId, inboxes.salesInbox.id, {
|
|
282
|
+
endpoint: "https://your-api.com/webhooks/sales",
|
|
283
|
+
events: ["email.received"],
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### List and Manage Inboxes
|
|
289
|
+
|
|
290
|
+
```ts
|
|
291
|
+
// List all inboxes for a domain
|
|
292
|
+
const inboxes = await client.inboxes.list(domainId);
|
|
293
|
+
console.log(`Found ${inboxes.length} inboxes`);
|
|
294
|
+
|
|
295
|
+
// Update an inbox
|
|
296
|
+
await client.inboxes.update(domainId, inboxId, {
|
|
297
|
+
localPart: "help", // Change from "support" to "help"
|
|
298
|
+
status: "active",
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
// Delete an inbox
|
|
302
|
+
await client.inboxes.remove(domainId, inboxId);
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
---
|
|
306
|
+
|
|
307
|
+
## Listen for Specific Inbox Events
|
|
308
|
+
|
|
309
|
+
Route incoming emails based on which inbox received them:
|
|
310
|
+
|
|
311
|
+
```ts
|
|
312
|
+
import { createWebhookHandler } from "commune-ai";
|
|
313
|
+
|
|
314
|
+
const handler = createWebhookHandler({
|
|
315
|
+
onEvent: async (message, context) => {
|
|
316
|
+
const { inboxId, inboxAddress } = context.payload;
|
|
317
|
+
|
|
318
|
+
console.log(`📧 Email received at: ${inboxAddress}`);
|
|
319
|
+
|
|
320
|
+
// Route to different handlers based on inbox
|
|
321
|
+
if (inboxAddress?.includes("support")) {
|
|
322
|
+
await handleSupportEmail(message, context);
|
|
323
|
+
} else if (inboxAddress?.includes("sales")) {
|
|
324
|
+
await handleSalesEmail(message, context);
|
|
325
|
+
} else {
|
|
326
|
+
// Default handler for other inboxes
|
|
327
|
+
await handleGeneralEmail(message, context);
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
// Example handlers
|
|
333
|
+
async function handleSupportEmail(message: any, context: any) {
|
|
334
|
+
const sender = message.participants.find(p => p.role === "sender")?.identity;
|
|
335
|
+
if (sender) {
|
|
336
|
+
await client.messages.send({
|
|
337
|
+
to: sender,
|
|
338
|
+
text: "Thanks for contacting support! We'll get back to you soon.",
|
|
339
|
+
conversation_id: message.conversation_id,
|
|
340
|
+
domainId: context.payload.domainId,
|
|
341
|
+
inboxId: context.payload.inboxId,
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async function handleSalesEmail(message: any, context: any) {
|
|
347
|
+
const sender = message.participants.find(p => p.role === "sender")?.identity;
|
|
348
|
+
if (sender) {
|
|
349
|
+
await client.messages.send({
|
|
350
|
+
to: sender,
|
|
351
|
+
text: "Thanks for your interest! A sales representative will contact you.",
|
|
352
|
+
conversation_id: message.conversation_id,
|
|
353
|
+
domainId: context.payload.domainId,
|
|
354
|
+
inboxId: context.payload.inboxId,
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
---
|
|
361
|
+
|
|
227
362
|
## Semantic Search (Coming Soon)
|
|
228
363
|
|
|
229
364
|
Search across all emails in your organization using natural language queries.
|