Triggers
Send payloads to your system based on external events
Overview
Triggers act as a notification system for your AI applications, enabling your agents to respond dynamically to external events occurring within your integrations.
When these events take place, triggers capture relevant information and deliver structured payloads directly to your system, facilitating timely and context-aware responses.
For instance, imagine building a Slack bot designed to generate humorous responses to messages from your co-workers. To achieve this, your application needs to receive notifications whenever someone posts a new message in a specific Slack channel. Triggers fulfill this role by listening for these events and promptly notifying your system, allowing your bot to respond appropriately.
Composio supports two primary methods for delivering these payloads:
-
Webhooks: HTTP POST requests sent to a publicly accessible URL that you configure. Webhooks are ideal for scenarios where your application needs to handle events asynchronously and independently from the event source.
-
Websockets: Persistent, real-time connections that push event data directly to your application. Websockets are suitable for applications requiring immediate, continuous, and low-latency communication.
Managing triggers
Before proceeding, ensure you’ve created an integration and established a connection to your external account (e.g., Slack, GitHub).
Enable the Trigger
Enable the “New Message Received” trigger for your Slack app through the dashboard, CLI, or code.
Code
CLI
Dashboard
Some triggers expect certain configuration to set the correct events. You can inspect and add these properties while enabling the triggers.
Listeners
Once you have the triggers set up, you can specify listener functions using websockets through the SDK or webhooks.
Specifying Listeners through Websockets
We create a listener and then define a callback function that executes when a listener recieves a payload.
Specifying Listeners through Webhooks
Assuming you’ve already set up a trigger as discussed in previous steps, here’s how you can use webhooks instead to listen in on new events happening in an app.
Configure Webhook URL
To receive trigger events via webhooks, you need to configure a publicly accessible URL where Composio can send the event payloads. This URL should point to an endpoint in your application that can process incoming webhook requests.
Listening on the webhooks
To demonstrate, here’s a FastAPI server to handle incoming webhook requests.
Demo: Roast Slack Messages
Let’s build a fun bot that generates snarky greentext responses to Slack messages using gpt-4.5
.
Implement Response Generation
Create a function to generate snarky responses using gpt-4.5
. We’ll also set up a preprocessor to handle Slack-specific message parameters:
Configure the tools
Set up the tools for sending Slack messages. We attach our preprocessor to automatically handle message threading and formatting:
The preprocessor ensures that every message is automatically configured with the correct thread, channel, and formatting settings, reducing the chance of misconfigured responses.
Run your server locally and use ngrok to expose it:
Remember to update your webhook URL in the Composio dashboard with your ngrok URL.
Troubleshooting
If you encounter issues with triggers or webhook listeners, you can use the Composio dashboard to inspect detailed trigger logs. The dashboard allows you to review event payloads, identify errors, and manually resend events for testing purposes.
Access the trigger logs here