Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | import os |
| 2 | |
| 3 | from composio import Composio |
| 4 | |
| 5 | composio = Composio() |
| 6 | |
| 7 | # Connected account with Gmail access and its owner (raise KeyError when unset) |
| 8 | gmail_connected_account_id = os.environ["COMPOSIO_EXAMPLES_GMAIL_CONNECTED_ACCOUNT_ID"] |
| 9 | user_id = os.environ["COMPOSIO_EXAMPLES_USER_ID"] |
| 10 | |
| 11 | |
| 12 | # List all triggers |
| 13 | triggers = composio.triggers.list() |
| 14 | print(triggers) |
| 15 | |
| 16 | # List all active triggers |
| 17 | active_triggers = composio.triggers.list_active() |
| 18 | print(active_triggers) |
| 19 | |
| 20 | # List all triggers enums |
| 21 | trigger_enums = composio.triggers.list_enum() |
| 22 | print(trigger_enums) |
| 23 | |
| 24 | # Get a trigger by id |
| 25 | trigger = composio.triggers.get_type(slug="GMAIL_NEW_GMAIL_MESSAGE") |
| 26 | print(trigger) |
| 27 | |
| 28 | # Create a trigger instance pinned to a connected account |
| 29 | instance = composio.triggers.create( |
| 30 | slug="GMAIL_NEW_GMAIL_MESSAGE", |
| 31 | connected_account_id=gmail_connected_account_id, |
| 32 | trigger_config={}, |
| 33 | ) |
| 34 | print(instance) |
| 35 | |
| 36 | # Or use user ID and let the backend resolve the active connection |
| 37 | instance = composio.triggers.create( |
| 38 | slug="GMAIL_NEW_GMAIL_MESSAGE", |
| 39 | user_id=user_id, |
| 40 | trigger_config={}, |
| 41 | ) |
| 42 | print(instance) |
| 43 | |
| 44 | # Disable a trigger instance |
| 45 | disabled_instance = composio.triggers.disable(trigger_id=instance.trigger_id) |
| 46 | print(disabled_instance) |
| 47 | |
| 48 | # Enable a trigger instance |
| 49 | enabled_instance = composio.triggers.enable(trigger_id=instance.trigger_id) |
| 50 | print(enabled_instance) |
| 51 | |
| 52 | # Delete a trigger instance |
| 53 | deleted_instance = composio.triggers.delete(trigger_id=instance.trigger_id) |
| 54 | print(deleted_instance) |
| 55 | |
| 56 | |
| 57 | # Verify a webhook (example in Flask) |
| 58 | # @app.route('/webhook', methods=['POST']) |
| 59 | # def webhook(): |
| 60 | # try: |
| 61 | # result = composio.triggers.verify_webhook( |
| 62 | # id=request.headers.get('webhook-id', ''), |
| 63 | # payload=request.get_data(as_text=True), |
| 64 | # signature=request.headers.get('webhook-signature', ''), |
| 65 | # timestamp=request.headers.get('webhook-timestamp', ''), |
| 66 | # secret=os.environ['COMPOSIO_WEBHOOK_SECRET'], |
| 67 | # ) |
| 68 | # |
| 69 | # # Result contains: |
| 70 | # # - version: WebhookVersion (V1, V2, or V3) |
| 71 | # # - payload: Normalized TriggerEvent |
| 72 | # # - raw_payload: Original parsed payload |
| 73 | # print(f"Webhook version: {result['version']}") |
| 74 | # print(f"Trigger: {result['payload']['trigger_slug']}") |
| 75 | # return 'OK', 200 |
| 76 | # except WebhookSignatureVerificationError: |
| 77 | # return 'Unauthorized', 401 |
| 78 | |
| 79 | |
| 80 | # Subscribe to a trigger |
| 81 | subscription = composio.triggers.subscribe() |
| 82 | |
| 83 | |
| 84 | # Define a callback functions |
| 85 | @subscription.handle(toolkit="GITHUB") |
| 86 | def handle_github_event(data): |
| 87 | print(data) |
| 88 | |
| 89 | |
| 90 | @subscription.handle(toolkit="SLACK") |
| 91 | def handle_slack_event(data): |
| 92 | print(data) |
| 93 | |
| 94 | |
| 95 | print("Subscribed to triggers. Waiting for events...", flush=True) |
| 96 | subscription.wait_forever() |