-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebhooks.py
More file actions
75 lines (58 loc) · 2.06 KB
/
webhooks.py
File metadata and controls
75 lines (58 loc) · 2.06 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
71
72
73
74
75
"""
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 asyncio
import os
import sys
from dotenv import load_dotenv
from maxun import Extract, Config
async def main():
extractor = Extract(Config(
api_key=os.environ["MAXUN_API_KEY"],
base_url=os.environ.get("MAXUN_BASE_URL", "https://app.maxun.dev/api/sdk/"),
))
# Create a robot to extract Indie Hackers posts
robot = await (
extractor
.create("Indie Hackers Posts Monitor")
.navigate("https://www.indiehackers.com/tags/artificial-intelligence")
.capture_list(
{
"selector": "a.ember-view.portal-entry",
"maxItems": 10,
}
)
)
print(f"Robot created: {robot.id}")
# Add webhook for notifications
await robot.add_webhook({
"url": "https://your-webhook-url.com/notifications",
"events": ["run.completed", "run.failed"],
"headers": {
"Authorization": "Bearer your-secret-token",
"X-Custom-Header": "maxun-webhook",
},
})
print("\n✓ Webhook added")
print(" URL: https://your-webhook-url.com/notifications")
print(" Events: run.completed, run.failed")
webhooks = robot.get_webhooks()
print(f"\n✓ Total webhooks configured: {len(webhooks or [])}")
# Run the robot — webhook will be triggered on completion
print("\nRunning robot...")
result = await robot.run()
list_data = result.get("data", {}).get("listData") or []
print(f"\n✓ Run completed: {result.get('runId')}")
print(f" Status: {result.get('status')}")
print(f" Items extracted: {len(list_data)}")
print("\n→ Webhook notification has been sent to your endpoint")
load_dotenv()
if not os.environ.get("MAXUN_API_KEY"):
print("Error: MAXUN_API_KEY environment variable is required", file=sys.stderr)
sys.exit(1)
asyncio.run(main())