Repository navigation
Expand file tree
/
Copy pathcalendly_adapter.py
More file actions
703 lines (640 loc) · 23.4 KB
/
Copy pathcalendly_adapter.py
File metadata and controls
703 lines (640 loc) · 23.4 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
"""
Calendly Adapter for UAW
Адаптер для Calendly через REST API
"""
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Union
from enum import Enum
import aiohttp
import json
from .base import (
BaseAdapter, AdapterConfig, AdapterResult,
AdapterStatus, AdapterCapability, AdapterError, AuthMethod
)
class CalendlyConfig(AdapterConfig):
"""Configuration for Calendly adapter"""
def __init__(
self,
api_key: Optional[str] = None,
webhook_secret: Optional[str] = None
):
super().__init__(
name="calendly",
display_name="Calendly",
description="Calendly adapter for scheduling and availability",
base_url="https://api.calendly.com",
api_version="v2",
timeout=30,
max_retries=3,
rate_limit=100, # Calendly API rate limit
auth_method=AuthMethod.API_KEY,
api_key=api_key,
icon_url="https://calendly.com/assets/calendly-icon-128x128.png",
documentation_url="https://developer.calendly.com/docs"
)
self.webhook_secret = webhook_secret
self._capabilities = [
AdapterCapability(
name="get_user",
description="Get authenticated user details",
method="GET",
endpoint="/users/me",
returns="User details"
),
AdapterCapability(
name="list_users",
description="List all users in organization",
method="GET",
endpoint="/users",
parameters=["email", "organization", "limit", "cursor"],
returns="List of users"
),
AdapterCapability(
name="get_user_availability",
description="Get user availability schedule",
method="GET",
endpoint="/users/{user_id}/availability_schedule",
parameters=["user_id"],
returns="Availability schedule"
),
AdapterCapability(
name="list_event_types",
description="List event types for a user",
method="GET",
endpoint="/users/{user_id}/event_types",
parameters=["user_id", "limit", "cursor"],
returns="List of event types"
),
AdapterCapability(
name="get_event_type",
description="Get event type details",
method="GET",
endpoint="/event_types/{event_type_id}",
parameters=["event_type_id"],
returns="Event type details"
),
AdapterCapability(
name="create_event_type",
description="Create a new event type",
method="POST",
endpoint="/event_types",
parameters=["name", "type", "duration_minutes", "user_id", "color", "description"],
returns="Created event type"
),
AdapterCapability(
name="update_event_type",
description="Update an event type",
method="PATCH",
endpoint="/event_types/{event_type_id}",
parameters=["event_type_id", "name", "type", "duration_minutes", "color", "description", "active"],
returns="Updated event type"
),
AdapterCapability(
name="delete_event_type",
description="Delete an event type",
method="DELETE",
endpoint="/event_types/{event_type_id}",
parameters=["event_type_id"],
returns="Success status"
),
AdapterCapability(
name="list_scheduled_events",
description="List scheduled events",
method="GET",
endpoint="/scheduled_events",
parameters=["organization", "user", "event_type", "status", "from", "to", "limit", "cursor"],
returns="List of scheduled events"
),
AdapterCapability(
name="get_scheduled_event",
description="Get scheduled event details",
method="GET",
endpoint="/scheduled_events/{event_id}",
parameters=["event_id"],
returns="Event details"
),
AdapterCapability(
name="cancel_scheduled_event",
description="Cancel a scheduled event",
method="POST",
endpoint="/scheduled_events/{event_id}/cancellation",
parameters=["event_id", "reason"],
returns="Cancellation result"
),
AdapterCapability(
name="create_invitee",
description="Create an invitee for an event",
method="POST",
endpoint="/scheduled_events/{event_id}/invitees",
parameters=["event_id", "email", "name", "first_name", "last_name"],
returns="Created invitee"
),
AdapterCapability(
name="list_invitees",
description="List invitees for an event",
method="GET",
endpoint="/scheduled_events/{event_id}/invitees",
parameters=["event_id", "limit", "cursor"],
returns="List of invitees"
),
AdapterCapability(
name="get_invitee",
description="Get invitee details",
method="GET",
endpoint="/scheduled_events/{event_id}/invitees/{invitee_id}",
parameters=["event_id", "invitee_id"],
returns="Invitee details"
),
AdapterCapability(
name="create_one_off_event",
description="Create a one-off event",
method="POST",
endpoint="/one_off_events",
parameters=["event_type", "start_time", "invitees", "email_invitees", "custom_questions"],
returns="Created one-off event"
),
AdapterCapability(
name="list_event_type_available_times",
description="List available times for an event type",
method="GET",
endpoint="/event_types/{event_type_id}/available_times",
parameters=["event_type_id", "start_time", "end_time", "timezone", "interval_minutes"],
returns="List of available times"
),
AdapterCapability(
name="list_organizations",
description="List organizations",
method="GET",
endpoint="/organizations",
parameters=["limit", "cursor"],
returns="List of organizations"
),
AdapterCapability(
name="get_organization",
description="Get organization details",
method="GET",
endpoint="/organizations/{organization_id}",
parameters=["organization_id"],
returns="Organization details"
),
AdapterCapability(
name="list_webhooks",
description="List webhooks",
method="GET",
endpoint="/webhook_subscriptions",
parameters=["organization", "user", "limit", "cursor"],
returns="List of webhooks"
),
AdapterCapability(
name="create_webhook",
description="Create a webhook",
method="POST",
endpoint="/webhook_subscriptions",
parameters=["url", "events", "organization", "user", "secret"],
returns="Created webhook"
),
AdapterCapability(
name="delete_webhook",
description="Delete a webhook",
method="DELETE",
endpoint="/webhook_subscriptions/{webhook_id}",
parameters=["webhook_id"],
returns="Success status"
),
]
self.capabilities = self._capabilities
class CalendlyAdapter(BaseAdapter):
"""
Calendly adapter using REST API.
Provides access to Calendly scheduling functionality.
"""
def __init__(self, config: Optional[CalendlyConfig] = None):
if config is None:
config = CalendlyConfig()
super().__init__(config)
self.capabilities = config.capabilities if hasattr(config, 'capabilities') else []
self._user_id: Optional[str] = None
async def initialize(self) -> bool:
"""Initialize the adapter"""
# Check if we have API key
if not self.config.api_key:
self.status = AdapterStatus.UNAVAILABLE
self.last_error = "No API key provided"
return False
# Try to get user info to verify connection
result = await self.get_user()
if result.success and result.data:
user_data = result.data
if isinstance(user_data, dict):
self._user_id = user_data.get("uri") or user_data.get("id")
self.status = AdapterStatus.CONFIGURED
return True
self.status = AdapterStatus.UNAVAILABLE
self.last_error = "Failed to verify API key"
return False
async def check_status(self) -> AdapterStatus:
"""Check current status"""
if self.status == AdapterStatus.CONFIGURED:
# Try to make a test request
result = await self.get_user()
if result.success:
return AdapterStatus.AVAILABLE
return self.status
async def execute(
self,
action: str,
parameters: Optional[Dict[str, Any]] = None
) -> AdapterResult:
"""Execute an action"""
if parameters is None:
parameters = {}
# Map action to method
action_mappings = {
"get_user": self.get_user,
"list_users": self.list_users,
"get_user_availability": self.get_user_availability,
"list_event_types": self.list_event_types,
"get_event_type": self.get_event_type,
"create_event_type": self.create_event_type,
"update_event_type": self.update_event_type,
"delete_event_type": self.delete_event_type,
"list_scheduled_events": self.list_scheduled_events,
"get_scheduled_event": self.get_scheduled_event,
"cancel_scheduled_event": self.cancel_scheduled_event,
"create_invitee": self.create_invitee,
"list_invitees": self.list_invitees,
"get_invitee": self.get_invitee,
"create_one_off_event": self.create_one_off_event,
"list_event_type_available_times": self.list_event_type_available_times,
"list_organizations": self.list_organizations,
"get_organization": self.get_organization,
"list_webhooks": self.list_webhooks,
"create_webhook": self.create_webhook,
"delete_webhook": self.delete_webhook,
}
method = action_mappings.get(action)
if not method:
return AdapterResult(
success=False,
error=f"Unknown action: {action}"
)
try:
return await method(**parameters)
except Exception as e:
return AdapterResult(
success=False,
error=str(e)
)
# User operations
async def get_user(self) -> AdapterResult:
"""Get authenticated user details"""
return await self._make_request(
method="GET",
endpoint="/users/me"
)
async def list_users(
self,
email: Optional[str] = None,
organization: Optional[str] = None,
limit: int = 100
) -> AdapterResult:
"""List all users in organization"""
params = {"count": limit}
if email:
params["email"] = email
if organization:
params["organization"] = organization
return await self._make_request(
method="GET",
endpoint="/users",
params=params
)
async def get_user_availability(self, user_id: Optional[str] = None) -> AdapterResult:
"""Get user availability schedule"""
uri = user_id or self._user_id or ""
return await self._make_request(
method="GET",
endpoint=f"/users/{uri}/availability_schedule"
)
# Event type operations
async def list_event_types(
self,
user_id: Optional[str] = None,
limit: int = 100
) -> AdapterResult:
"""List event types for a user"""
uri = user_id or self._user_id or ""
return await self._make_request(
method="GET",
endpoint=f"/users/{uri}/event_types",
params={"count": limit}
)
async def get_event_type(self, event_type_id: str) -> AdapterResult:
"""Get event type details"""
return await self._make_request(
method="GET",
endpoint=f"/event_types/{event_type_id}"
)
async def create_event_type(
self,
name: str,
type: str = "standard",
duration_minutes: int = 30,
user_id: Optional[str] = None,
color: str = "#4285f4",
description: Optional[str] = None
) -> AdapterResult:
"""Create a new event type"""
data = {
"name": name,
"type": type,
"duration_minutes": duration_minutes,
"color": color
}
if description:
data["description"] = description
if user_id:
data["user"] = user_id
elif self._user_id:
data["user"] = self._user_id
return await self._make_request(
method="POST",
endpoint="/event_types",
json_data=data
)
async def update_event_type(
self,
event_type_id: str,
name: Optional[str] = None,
type: Optional[str] = None,
duration_minutes: Optional[int] = None,
color: Optional[str] = None,
description: Optional[str] = None,
active: Optional[bool] = None
) -> AdapterResult:
"""Update an event type"""
data = {}
if name:
data["name"] = name
if type:
data["type"] = type
if duration_minutes:
data["duration_minutes"] = duration_minutes
if color:
data["color"] = color
if description:
data["description"] = description
if active is not None:
data["active"] = active
if not data:
return AdapterResult(
success=False,
error="No fields to update"
)
return await self._make_request(
method="PATCH",
endpoint=f"/event_types/{event_type_id}",
json_data=data
)
async def delete_event_type(self, event_type_id: str) -> AdapterResult:
"""Delete an event type"""
return await self._make_request(
method="DELETE",
endpoint=f"/event_types/{event_type_id}"
)
# Scheduled event operations
async def list_scheduled_events(
self,
organization: Optional[str] = None,
user: Optional[str] = None,
event_type: Optional[str] = None,
status: Optional[str] = None,
from_time: Optional[str] = None,
to: Optional[str] = None,
limit: int = 100
) -> AdapterResult:
"""List scheduled events"""
params = {"count": limit}
if organization:
params["organization"] = organization
if user:
params["user"] = user
elif self._user_id:
params["user"] = self._user_id
if event_type:
params["event_type"] = event_type
if status:
params["status"] = status
if from_time:
params["from"] = from_time
if to:
params["to"] = to
return await self._make_request(
method="GET",
endpoint="/scheduled_events",
params=params
)
async def get_scheduled_event(self, event_id: str) -> AdapterResult:
"""Get scheduled event details"""
return await self._make_request(
method="GET",
endpoint=f"/scheduled_events/{event_id}"
)
async def cancel_scheduled_event(
self,
event_id: str,
reason: Optional[str] = None
) -> AdapterResult:
"""Cancel a scheduled event"""
data = {}
if reason:
data["reason"] = reason
return await self._make_request(
method="POST",
endpoint=f"/scheduled_events/{event_id}/cancellation",
json_data=data
)
# Invitee operations
async def create_invitee(
self,
event_id: str,
email: str,
name: Optional[str] = None,
first_name: Optional[str] = None,
last_name: Optional[str] = None
) -> AdapterResult:
"""Create an invitee for an event"""
data = {"email": email}
if name:
data["name"] = name
if first_name:
data["first_name"] = first_name
if last_name:
data["last_name"] = last_name
return await self._make_request(
method="POST",
endpoint=f"/scheduled_events/{event_id}/invitees",
json_data=data
)
async def list_invitees(
self,
event_id: str,
limit: int = 100
) -> AdapterResult:
"""List invitees for an event"""
return await self._make_request(
method="GET",
endpoint=f"/scheduled_events/{event_id}/invitees",
params={"count": limit}
)
async def get_invitee(self, event_id: str, invitee_id: str) -> AdapterResult:
"""Get invitee details"""
return await self._make_request(
method="GET",
endpoint=f"/scheduled_events/{event_id}/invitees/{invitee_id}"
)
# One-off event operations
async def create_one_off_event(
self,
event_type: str,
start_time: str,
invitees: Optional[List[Dict[str, str]]] = None,
email_invitees: Optional[List[str]] = None
) -> AdapterResult:
"""Create a one-off event"""
data = {
"event_type": event_type,
"start_time": start_time
}
if invitees:
data["invitees"] = invitees
if email_invitees:
data["email_invitees"] = [{"email": email} for email in email_invitees]
return await self._make_request(
method="POST",
endpoint="/one_off_events",
json_data=data
)
async def list_event_type_available_times(
self,
event_type_id: str,
start_time: str,
end_time: str,
timezone: str = "UTC",
interval_minutes: int = 30
) -> AdapterResult:
"""List available times for an event type"""
params = {
"start_time": start_time,
"end_time": end_time,
"timezone": timezone,
"interval_minutes": interval_minutes
}
return await self._make_request(
method="GET",
endpoint=f"/event_types/{event_type_id}/available_times",
params=params
)
# Organization operations
async def list_organizations(self, limit: int = 100) -> AdapterResult:
"""List organizations"""
return await self._make_request(
method="GET",
endpoint="/organizations",
params={"count": limit}
)
async def get_organization(self, organization_id: str) -> AdapterResult:
"""Get organization details"""
return await self._make_request(
method="GET",
endpoint=f"/organizations/{organization_id}"
)
# Webhook operations
async def list_webhooks(
self,
organization: Optional[str] = None,
user: Optional[str] = None,
limit: int = 100
) -> AdapterResult:
"""List webhooks"""
params = {"count": limit}
if organization:
params["organization"] = organization
if user:
params["user"] = user
elif self._user_id:
params["user"] = self._user_id
return await self._make_request(
method="GET",
endpoint="/webhook_subscriptions",
params=params
)
async def create_webhook(
self,
url: str,
events: List[str],
organization: Optional[str] = None,
user: Optional[str] = None,
secret: Optional[str] = None
) -> AdapterResult:
"""Create a webhook"""
data = {
"url": url,
"events": events
}
if organization:
data["organization"] = organization
if user:
data["user"] = user
elif self._user_id:
data["user"] = self._user_id
if secret:
data["secret"] = secret
return await self._make_request(
method="POST",
endpoint="/webhook_subscriptions",
json_data=data
)
async def delete_webhook(self, webhook_id: str) -> AdapterResult:
"""Delete a webhook"""
return await self._make_request(
method="DELETE",
endpoint=f"/webhook_subscriptions/{webhook_id}"
)
# Utility methods
async def schedule_meeting(
self,
event_type_id: str,
start_time: str,
attendees: List[str],
end_time: Optional[str] = None
) -> AdapterResult:
"""Schedule a meeting using Calendly"""
# Create one-off event
result = await self.create_one_off_event(
event_type=event_type_id,
start_time=start_time,
email_invitees=attendees
)
if result.success:
return result
# Fallback: try to find available time and schedule
available_times = await self.list_event_type_available_times(
event_type_id=event_type_id,
start_time=start_time,
end_time=end_time or start_time # Use start_time as fallback
)
if available_times.success and available_times.data:
times = available_times.data
if isinstance(times, dict) and times.get("collection"):
first_time = times["collection"][0] if times["collection"] else None
if first_time:
return await self.create_one_off_event(
event_type=event_type_id,
start_time=first_time.get("start_time"),
email_invitees=attendees
)
return AdapterResult(
success=False,
error="Could not find available time for meeting"
)