Email & Calendar Integration
Relevant source files
The following files were used as context for generating this wiki page:
- packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-messages-import.service.ts
- packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-events-import.service.ts
- packages/twenty-front/src/pages/settings/accounts/SettingsAccounts.tsx
- packages/twenty-docs/docs.json
- packages/twenty-apps/community/fireflies/README.md
- packages/twenty-apps/community/meeting-transcript/README.md
The Email & Calendar Integration feature allows users to connect their external accounts (such as Google or Microsoft) to Twenty. This enables the synchronization of emails and calendar events directly into the CRM, associating them with relevant contacts and companies. The goal is to provide a unified view of all customer interactions and activities within a single platform, enhancing productivity and context for sales and support teams.
This system is composed of a frontend interface for account management and dedicated backend services for handling the import and processing of data. The documentation for users can be found under the "Collaboration" and "Settings" sections of the User Guide.
Sources: packages/twenty-docs/docs.json:101-105, 131-133
Architecture Overview
The integration is split between the twenty-front and twenty-server packages. The frontend provides the UI for users to connect and manage their accounts, while the backend contains services that periodically fetch and import data from the connected accounts.
| Component | Location | Description |
|---|---|---|
| Account Settings UI | twenty-front | A React component in the settings page for connecting, viewing, and disconnecting external email/calendar accounts. |
| Email Import Service | twenty-server | A NestJS service responsible for fetching email messages and storing them as Message entities in the CRM. |
| Calendar Import Service | twenty-server | A NestJS service that fetches calendar events and stores them as CalendarEvent entities, linking them to attendees. |
Sources: packages/twenty-front/src/pages/settings/accounts/SettingsAccounts.tsx, packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-messages-import.service.ts, packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-events-import.service.ts
Frontend: Account Management
The user-facing part of the integration is handled in the SettingsAccounts.tsx component. This page allows users to manage their connected accounts for email and calendar synchronization.
Sources: packages/twenty-front/src/pages/settings/accounts/SettingsAccounts.tsx
Key Components & Logic
SettingsAccounts: The main React component that renders the list of connected accounts and provides options to add new ones.useConnectedAccountsQuery: A GraphQL query hook used to fetch the list of currently connected accounts for the user.useDisconnectAccountMutation: A GraphQL mutation hook to remove a connected account.- Account Providers: The UI supports connecting different types of accounts, such as Google (
'google') and Microsoft ('microsoft365'). Buttons are rendered for each provider, initiating the OAuth flow. - State Management: The component uses Recoil for state management, such as
isConnectAccountModalOpenStateto control the visibility of the connection modal.
The following diagram illustrates the sequence of connecting a new account from the user interface.
This diagram shows the OAuth flow for connecting a new external account.
Sources: packages/twenty-front/src/pages/settings/accounts/SettingsAccounts.tsx:34-150
Backend: Data Import Services
The backend is responsible for the heavy lifting of fetching data from the connected accounts. This is handled by two main services: MessagingMessagesImportService for emails and CalendarEventsImportService for calendar events. These services are designed to be run by background workers (e.g., using BullMQ).
Email Import Service
The MessagingMessagesImportService handles the synchronization of emails from a connected account into Twenty.
Core Logic
The service's primary method is importMessages. Its workflow is as follows:
- Initialization: Receives a
connectedAccountIdand ahistoryIdto fetch new messages since the last sync. - Fetch Messages: It uses a provider-specific service (e.g.,
google-gmail) to fetch a list of message IDs. - Batch Processing: The message IDs are processed in batches to avoid overwhelming the external API and the local database.
- Fetch Message Details: For each batch, it fetches the full details of each message.
- Data Transformation: The raw message data is transformed into the
Messageentity format used by Twenty. This includes parsingfrom,to,cc,bcc,subject, andbody. - Database Insertion: The transformed messages are bulk-inserted into the database using
messageRepository.insert. - Update Sync State: After a successful import, the
historyIdfor the connected account is updated to ensure the next sync starts from the correct point.
The diagram below visualizes the email import process.
This flowchart illustrates the steps taken by the MessagingMessagesImportService to sync emails.
Calendar Event Import Service
The CalendarEventsImportService is responsible for syncing calendar events. Its structure is very similar to the email import service but is tailored for event data.
Core Logic
The importEvents method orchestrates the import:
- Initialization: Takes a
connectedAccountIdand asyncTokento fetch changes. - Fetch Events: Uses a provider-specific calendar service (e.g.,
google-calendar) to fetch new or updated events. - Data Transformation: Each event is transformed into the
CalendarEvententity format. This includes:- Mapping attendees (participants) to existing
Personentities in the CRM or creating them if they don't exist. - Handling event status (e.g.,
confirmed,cancelled). - Storing event details like
summary,description,start, andendtimes.
- Mapping attendees (participants) to existing
- Upsert Events: The service performs an "upsert" operation (update if exists, insert if new) for each event based on the provider's unique event ID. This handles both new events and updates to existing ones.
- Update Sync State: The
syncTokenis updated for the connected account to mark the new synchronization point.
The diagram below visualizes the calendar event import process.
This flowchart illustrates the steps taken by the CalendarEventsImportService to sync calendar events.
Extensibility and Related Integrations
The core email and calendar integration provides a foundation that can be extended by other applications. Community apps like Fireflies and Meeting Transcript demonstrate this by building on top of the meeting data that calendar integration provides.
- Fireflies Integration: This app connects to Fireflies.ai via webhooks. When a meeting ends, it fetches an AI-generated summary, action items, and sentiment analysis, and creates a
Meetingrecord in Twenty. It intelligently links notes to contacts identified from the meeting participants. - Meeting Transcript Integration: This app uses an AI provider (like OpenAI or Groq) to process raw meeting transcripts sent via a webhook. It extracts summaries, action items with assignees and due dates, and creates structured
NoteandTaskrecords in the CRM, automatically linking them to the relevant people.
These examples show how the base calendar data can be enriched with AI-powered insights from third-party services, creating a more powerful and intelligent CRM experience.
Sources: packages/twenty-apps/community/fireflies/README.md, packages/twenty-apps/community/meeting-transcript/README.md
Summary
The Email & Calendar Integration is a cornerstone feature of Twenty, bridging the gap between external communication tools and the CRM. It provides a seamless flow of information by automatically syncing emails and events, ensuring that all customer-related activities are captured and organized. The frontend offers a simple interface for account management, while the robust backend services handle data import efficiently. Furthermore, the system's design allows for powerful extensions, as seen with community apps that add AI-driven insights to meeting data.