This project integrates AO processes with backend APIs using a message-based approach that focuses on detecting messages rather than accessing state directly.
- AO Handlers: Lua scripts that run in the AO environment and send messages when API requests are received.
- Message Bridge: A Node.js script that uses aoconnect's results function to check for new messages.
- Backend APIs: Express.js servers that handle email and document requests.
# Install dependencies for the Message Bridge
cd ao-message-bridge
npm install
Edit the .env
file in the ao-message-bridge
directory to set the AO process ID and API endpoints:
# AO Process ID
AO_PROCESS_ID="your-ao-process-id"
# API Endpoints
EMAIL_API_URL="http://localhost:3001/api/email/send"
DOCUMENT_API_URL="http://localhost:3002/api/documents"
# Check interval in seconds
CHECK_INTERVAL=10
# Arweave wallet path
ARWEAVE_WALLET_PATH="path/to/your/arweave-wallet.json"
cd backend
npm start
This will start the email API server on port 3001 and the document API server on port 3002.
cd ao-message-bridge
npm start
This will start the Message Bridge, which will check for new messages every 10 seconds (or whatever interval you set in the .env
file).
In the AO console, load the AO handlers:
.load ao-message-bridge/ao-api-handlers.lua
In the AO console, load and run the test script:
.load ao-message-bridge/test-script.lua
This will send an email request, create a document request, and list documents, which will be processed by the Message Bridge.
To send an email from an AO process, send a message with the following format:
Send({
Target = "your-ao-process-id",
Action = "SendEmail",
Data = Utils.toJson({
to = "recipient@example.com",
subject = "Email Subject",
body = "Email Body",
from = "sender@example.com"
})
})
To create a document from an AO process, send a message with the following format:
Send({
Target = "your-ao-process-id",
Action = "CreateDocument",
Data = Utils.toJson({
fileName = "document.pdf",
fileSize = 1024,
contentType = "application/pdf",
content = "Document content",
uploadedBy = "user@example.com",
department = "Department"
})
})
To list documents from an AO process, send a message with the following format:
Send({
Target = "your-ao-process-id",
Action = "ListDocuments",
Data = Utils.toJson({})
})
- AO Process: Sends API requests to itself using the AO handlers.
- AO Handlers: Process the requests, store them in the process state, and send messages to be picked up by the Message Bridge.
- Message Bridge: Checks for new messages and processes API requests.
- Backend APIs: Handle email and document requests.
The Message Bridge uses aoconnect's results function to check for new messages, which is a more reliable approach than trying to access the state directly.
- The AO process receives an API request (e.g., SendEmail).
- The AO handler processes the request, stores it in the process state, and sends a message with the Action "APIRequest" and Status "pending".
- The Message Bridge checks for new messages and processes any "APIRequest" messages with Status "pending" that it finds.
- When a request is processed, the Message Bridge sends a message back to the AO process to update the request status.
- The AO process updates the status of the request and sends a message with the Action "APIRequest" and Status "completed".
- The AO process also notifies the requester.
If you encounter any issues, check the logs of the Message Bridge and the backend APIs for error messages.
Common issues:
- AO Process ID: Make sure the AO process ID in the
.env
file is correct. - Arweave Wallet: Make sure the Arweave wallet path in the
.env
file is correct. - API Endpoints: Make sure the API endpoints in the
.env
file are correct. - AO Handlers: Make sure the AO handlers are loaded in the AO process.
This project is licensed under the MIT License.