-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebhooks.ts
More file actions
70 lines (58 loc) · 2 KB
/
webhooks.ts
File metadata and controls
70 lines (58 loc) · 2 KB
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
/**
* Webhooks Example
*
* This example demonstrates:
* - Adding webhooks for robot events
* - Configuring webhook events and headers
* - Getting webhook notifications when runs complete
*
* Site: Indie Hackers (https://www.indiehackers.com)
*/
import 'dotenv/config';
import { Extract } from 'maxun-sdk';
async function main() {
const extractor = new Extract({
apiKey: process.env.MAXUN_API_KEY!,
baseUrl: process.env.MAXUN_BASE_URL!
});
try {
// Create a robot to extract Indie Hackers posts
const robot = await extractor
.create('Indie Hackers Posts Monitor')
.navigate('https://www.indiehackers.com/tags/artificial-intelligence')
.captureList({
selector: 'a.ember-view.portal-entry',
maxItems: 10
});
console.log(`Robot created: ${robot.id}`);
// Add webhook for notifications
await robot.addWebhook({
url: 'https://your-webhook-url.com/notifications',
events: ['run.completed', 'run.failed'],
headers: {
'Authorization': 'Bearer your-secret-token',
'X-Custom-Header': 'maxun-webhook'
}
});
console.log('\n✓ Webhook added');
console.log(' URL: https://your-webhook-url.com/notifications');
console.log(' Events: run.completed, run.failed');
const webhooks = robot.getWebhooks();
console.log(`\n✓ Total webhooks configured: ${webhooks?.length || 0}`);
// Run the robot - webhook will be triggered on completion
console.log('\nRunning robot...');
const result = await robot.run();
console.log(`\n✓ Run completed: ${result.runId}`);
console.log(` Status: ${result.status}`);
console.log(` Items extracted: ${result.data.listData?.length || 0}`);
console.log('\n→ Webhook notification has been sent to your endpoint');
} catch (error: any) {
console.error('Error:', error.message);
process.exit(1);
}
}
if (!process.env.MAXUN_API_KEY) {
console.error('Error: MAXUN_API_KEY environment variable is required');
process.exit(1);
}
main();