Skip to main content
Python

Calendar Agent GitHub Repository

Explore the complete source code for the Calendar Agent project. This repository contains all the necessary files and scripts to set up and run the Calendar Agent using CrewAI and Composio.
1

Install the required packages

pip install composio-crewai langchain-openai
Create a .env file and add your OpenAI API Key.
2

Import base packages

Next, we’ll import the essential libraries for our project.
import os
from datetime import datetime

from composio_crewai import App, ComposioToolSet
from crewai import Agent, Task, Crew
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI        
3

Initialise Language Model and Define tools

We’ll initialize our language model and set up the necessary tools for our agents.
load_dotenv()

# Initialize the language model
llm = ChatOpenAI(model="gpt-4o")

# Define tools for the agents
# We are using Google calendar tool from composio to connect to our calendar account.
composio_toolset = ComposioToolSet()
tools = composio_toolset.get_tools(apps=[App.GOOGLECALENDAR])

# Retreive the current date and time
date = datetime.today().strftime("%Y-%m-%d")
timezone = datetime.now().astimezone().tzinfo        
4

Setup Todo

Define the todo list that we want to convert into calendar events.
# Setup Todo
todo = """
    1PM - 3PM -> Code,
    5PM - 7PM -> Meeting,
    9AM - 12AM -> Learn something,
    8PM - 10PM -> Game
"""        
5

Create and Execute Agent

Finally, we’ll define and execute the agent responsible for creating Google Calendar events based on the todo list.
# Create and Execute Agent.
def run_crew():
calendar_agent = Agent(
    role="Google Calendar Agent",
    goal="""You take action on Google Calendar using Google Calendar APIs""",
    backstory="""You are an AI agent responsible for taking actions on Google Calendar on users' behalf. 
    You need to take action on Calendar using Google Calendar APIs. Use correct tools to run APIs from the given tool-set.""",
    verbose=True,
    tools=tools,
    llm=llm,
)
task = Task(
    description=f"Book slots according to {todo}. Label them with the work provided to be done in that time period. Schedule it for today. Today's date is {date} (it's in YYYY-MM-DD format) and make the timezone be {timezone}.",
    agent=calendar_agent,
    expected_output="if free slot is found",
)
crew = Crew(agents=[calendar_agent], tasks=[task])
result = crew.kickoff()
print(result) 

Complete Code

# Import base packages
import os
from datetime import datetime

from composio_crewai import App, ComposioToolSet
from crewai import Agent, Task
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

# Load environment variables
load_dotenv()

# Initialize the language model
llm = ChatOpenAI(model="gpt-4o")

# Define tools for the agents
# We are using Google calendar tool from composio to connect to our calendar account.
composio_toolset = ComposioToolSet()
tools = composio_toolset.get_tools(apps=[App.GOOGLECALENDAR])

# Retreive the current date and time
date = datetime.today().strftime("%Y-%m-%d")
timezone = datetime.now().astimezone().tzinfo

# Setup Todo
todo = """
    1PM - 3PM -> Code,
    5PM - 7PM -> Meeting,
    9AM - 12AM -> Learn something,
    8PM - 10PM -> Game
"""

# Create and Execute Agent.
def run_crew():
    calendar_agent = Agent(
        role="Google Calendar Agent",
        goal="""You take action on Google Calendar using Google Calendar APIs""",
        backstory="""You are an AI agent responsible for taking actions on Google Calendar on users' behalf.
        You need to take action on Calendar using Google Calendar APIs. Use correct tools to run APIs from the given tool-set.""",
        verbose=True,
        tools=tools,
        llm=llm,
    )
    task = Task(
        description=f"Book slots according to {todo}. Label them with the work provided to be done in that time period. Schedule it for today. Today's date is {date} (it's in YYYY-MM-DD format) and make the timezone be {timezone}.",
        agent=calendar_agent,
        expected_output="if free slot is found",
    )
    crew = Crew(agents=[calendar_agent], tasks=[task])
    result = crew.kickoff()
    print(result)
    return result

run_crew()