Listen to New Emails in Gmail
In this guide, weβll:
- π Connect your Gmail account with Composio
- π Enable Triggers to listen to new emails in Gmail
- π§ Pass these triggers event payloads to an AI language model to identify bank transactions
- β Execute an action from Gmail tool to add important label to relevant emails
Tools represent a group of actions specific to an app. Actions are operations you can perform - like starring a repo on GitHub or creating an issue in Linear.
Install Libraries
pip install composio-core composio_openai
Connect Your Gmail Account
Weβll use default as the user id, also known as entity id.
You need to have an active Gmail Integration. Learn how to do this
herecomposio login
composio add gmail -e "default"
Donβt forget to set your COMPOSIO_API_KEY and OPENAI_API_KEY in your environment variables.
Enable Triggers
composio triggers enable gmail_new_gmail_message
Create an Agent
def agent_function(thread_id: str, message: str, sender_mail: str):
tools = toolset.get_tools(apps=[App.GMAIL])
response = openai_client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=[
{
"role": "system",
"content": "You are a helpful assistant that can parse the email content, identify bank transactions and add the 'important' label to the email. Otherwise, don't do anything.",
},
{
"role": "user",
"content": f"Thread ID: {thread_id}\nMessage: {message}\nSender: {sender_mail}",
},
],
)
result = toolset.handle_tool_calls(response)
print(result)
Create a Trigger Listener
listener = toolset.create_trigger_listener()
@listener.callback(filters={"trigger_name": Trigger.GMAIL_NEW_GMAIL_MESSAGE})
def callback_function(event):
payload = event.payload
thread_id = payload.get("threadId")
message = payload.get("messageText")
sender_mail = payload.get("sender")
agent_function(thread_id, message, sender_mail)
print("Starting listener")
listener.wait_forever()
Install Libraries
npm install composio-core openai
Connect Your Gmail Account
Weβll use default as the user id (entity id).
You need to have an active Gmail Integration. Learn how to do this
herecomposio login
composio add gmail -e "default"
Donβt forget to set your COMPOSIO_API_KEY and OPENAI_API_KEY in your environment variables.
Enable Triggers
composio triggers enable gmail_new_gmail_message
Create an Agent
const agentFunction = async (threadId, subject, senderMail) => {
const tools = await toolset.getTools({ apps: ["gmail"] });
const response = await openai_client.chat.completions.create({
model: "gpt-4o-mini",
messages: [
{
role: "system",
content: "You are a helpful assistant that can parse the email content, identify bank transactions and add the 'important' label to the email. Otherwise, don't do anything"
},
{
role: "user",
content: `Thread ID: ${threadId}, Subject: ${subject}, Sender: ${senderMail}`
}
],
tools: tools,
tool_choice: "auto",
});
const result = await toolset.handleToolCall(response);
console.log(result);
}
Create a Trigger Listener
toolset.triggers.subscribe(
(data) => {
const {
payload: {
threadId,
subject,
sender
}
} = data;
agentFunction(threadId, subject, sender);
},
{
triggerName: "gmail_new_gmail_message"
}
);
Next Steps
Now that youβve seen how to use triggers, you can explore the following resources:
Tools
Checkout our toolset of 250+ LLM ready tools to build powerful AI applications
Connections
Learn how to create and manage connections for your users
Compatible Agentic Frameworks
Integrate with popular agentic frameworks
Triggers
Subscribe to triggers to execute actions automatically
Other Concepts
Learn about workspace environments, using CLI & other concepts