A Cloudflare Pages application that provides AI phone agent capabilities, including scheduling calls, managing calendars, and handling email notifications.
This application follows a simplified architecture using Cloudflare Pages Functions:
┌─────────┐ ┌───────────────────┐ ┌─────────────────┐
│ Resend │────▶│ Cloudflare Pages │────▶│ Bland.ai API │
└─────────┘ └───────────────────┘ └─────────────────┘
│ │ │
│ │ │
▼ ▼ ▼
Email Events Process Webhooks Schedule Calls
Manage Calendar Handle Phone Calls
Send Notifications
- AI Phone Agent: Schedule and manage automated phone calls using Bland.ai
- Calendar Integration: Create and manage calendar events for scheduled calls
- Email Notifications: Send confirmation, rescheduling, and cancellation emails
- Webhook Processing: Handle webhooks from email and phone call services
- Secure Storage: Store call and scheduling data securely
- Error Handling: Comprehensive error handling and reporting
/
├── functions/ # Pages Functions (serverless API endpoints)
│ ├── api/ # API endpoints
│ │ ├── webhook.js # Webhook handler
│ │ ├── schedule.js # Call scheduling handler
│ │ └── index.js # API info endpoint
│ └── static.js # Static file handler
├── public/ # Static assets
├── src/
│ ├── config/ # Configuration
│ ├── services/ # Core services
│ │ ├── agent-scheduling-service.ts # Call scheduling service
│ │ ├── bland-service.ts # Bland.ai integration
│ │ ├── calendar-service.ts # Calendar management
│ │ ├── email-service.ts # Email notifications
│ │ └── storage-service.ts # Data storage
│ ├── types/ # TypeScript type definitions
│ ├── utils/ # Utility functions
│ └── webhooks/ # Webhook processing logic
├── test/ # Tests
│ ├── integration/ # Integration tests
│ ├── services/ # Service tests
│ └── ... # Other tests
├── scripts/ # Development and deployment scripts
│ ├── dev-server.js # Custom development server
│ ├── minimal-server.js # Minimal development server
│ └── ... # Other scripts
├── _routes.json # Cloudflare Pages routing configuration
└── package.json # Project configuration
The application requires the following environment variables:
WEBHOOK_SIGNING_SECRET: The signing secret from ResendTARGET_WEBHOOK_URL: The URL of your target webhookTARGET_WEBHOOK_AUTH_TOKEN: The authentication token for your target webhookDEBUG_WEBHOOKS: Set to "true" to enable debug logging (optional)STORE_FAILED_PAYLOADS: Set to "true" to store failed webhook payloads (optional)RESEND_API_KEY: API key for Resend email serviceSENDER_EMAIL: Email address to use as senderSENDER_NAME: Name to use as senderDEFAULT_TIMEZONE: Default timezone for date/time formattingBLAND_AI_API_KEY: API key for Bland.ai serviceBLAND_AI_BASE_URL: Base URL for Bland.ai APIMAX_CALL_DURATION_MINUTES: Maximum call duration in minutesDEFAULT_RETRY_COUNT: Default number of retries for API calls
- Node.js 18 or later
- npm or yarn
- Clone the repository:
git clone https://github.com/yourusername/phone-agent.git
cd phone-agent- Install dependencies:
npm install- Create a
.envfile with your environment variables (see.env.examplefor reference).
There are three ways to run the application locally:
Start the development server using Wrangler:
npm run devThis will start a local server at http://localhost:8788.
If you encounter GLIBC compatibility issues with Wrangler, use our custom development server:
npm run dev:localThis will start a local Express server at http://localhost:8787 that mimics the Cloudflare Pages environment.
For environments with dependency issues or compatibility problems, use our minimal server that relies only on Node.js built-in modules:
npm run dev:minimalThis will start a lightweight HTTP server at http://localhost:8787 with mock API endpoints.
You can test the API endpoints using curl:
# Get API information
curl http://localhost:8787/api
# Get schedule information
curl http://localhost:8787/api/schedule?requestId=test123
# Test webhook endpoint
curl -X POST -H "Content-Type: application/json" \
-d '{"sender":"test@example.com","subject":"Test Webhook","body":"This is a test"}' \
http://localhost:8787/api/webhook
# Cancel a scheduled call
curl -X POST -H "Content-Type: application/json" \
-d '{"requestId":"test123","reason":"Testing cancellation"}' \
http://localhost:8787/api/schedule/cancelRun the tests:
npm testRun the tests with coverage:
npm run test:coverage-
Log in to the Cloudflare dashboard and create a new Pages project.
-
Connect your GitHub repository.
-
Configure the build settings:
- Build command:
npm run build - Build output directory:
/
- Build command:
-
Add your environment variables in the Cloudflare dashboard.
-
Deploy the application.
Alternatively, you can deploy using the Wrangler CLI:
npm run deploy- Log in to your Resend account.
- Navigate to the Webhooks section.
- Click "Add Webhook".
- Enter the URL of your Cloudflare Pages application (e.g.,
https://your-app.pages.dev/api/webhook). - Select the events you want to receive (e.g.,
email.sent,email.delivered, etc.). - Save the webhook configuration.
- Copy the signing secret for use in your environment variables.
- Log in to your Bland.ai account.
- Navigate to the Webhooks section.
- Configure a webhook endpoint pointing to your application (e.g.,
https://your-app.pages.dev/api/webhook). - Select the call events you want to receive.
- Save the webhook configuration.
If you encounter GLIBC compatibility issues with Wrangler, try using one of our alternative development servers:
# Option 1: Custom Express server
npm run dev:local
# Option 2: Minimal Node.js server
npm run dev:minimalIf you receive a "Cannot GET /api" error, make sure you're using the correct server and port. The API endpoints are available at:
- http://localhost:8787/api (for dev:local and dev:minimal)
- http://localhost:8788/api (for standard Wrangler dev)
MIT
