English | 简体中文
- Install Node.js and npm
- Install Wrangler CLI:
npm install -g wrangler - Cloudflare account (free tier works)
- Matrix account
- OpenAI API key or alternative
wrangler login# Production namespace
wrangler kv:namespace create "KV"
# Preview namespace
wrangler kv:namespace create "KV" --previewThis will output something like:
{ binding = "KV", id = "your_namespace_id" }
Save these IDs for the next step.
wrangler r2 bucket create matrix-bot-storageEdit wrangler.toml and update:
[[kv_namespaces]]
binding = "KV"
id = "your_production_kv_id" # Replace with your KV ID
preview_id = "your_preview_kv_id" # Replace with your preview KV ID
[[r2_buckets]]
binding = "R2"
bucket_name = "matrix-bot-storage"# Matrix credentials
wrangler secret put MATRIX_USER_ID
# Enter: @yourbotuser:matrix.org
wrangler secret put MATRIX_PASSWORD
# Enter: your_secure_password
# OpenAI credentials
wrangler secret put OPENAI_API_KEY
# Enter: sk-your-openai-api-key
# Optional: Custom API endpoint
wrangler secret put OPENAI_BASE_URL
# Enter: https://api.openai.com/v1
# Admin users (comma-separated)
wrangler secret put BOT_ADMIN_USERS
# Enter: @admin1:matrix.org,@admin2:matrix.org
# Default model
wrangler secret put DEFAULT_MODEL
# Enter: gpt-4npm installwrangler deployAfter deployment, you'll see:
Published matrix-chatgpt-bot (0.01 sec)
https://matrix-chatgpt-bot.yourname.workers.dev
curl -X POST https://matrix-chatgpt-bot.yourname.workers.dev/loginExpected response:
{
"status": "logged_in",
"user_id": "@yourbotuser:matrix.org",
"device_id": "ABCDEFGHIJ"
}curl https://matrix-chatgpt-bot.yourname.workers.dev/startExpected response:
{
"status": "started"
}curl https://matrix-chatgpt-bot.yourname.workers.dev/statusExpected response:
{
"isRunning": true,
"syncToken": "s12345_67890_..."
}- Invite the bot to a Matrix room
- Send a message mentioning the bot:
@yourbotuser:matrix.org hello!
- The bot should respond with a greeting.
In your Matrix room, send:
!addprovider azure https://your-resource.openai.azure.com/openai/deployments/your-deployment YOUR_AZURE_API_KEY gpt-4 gpt-35-turbo
!provider set azure
!addprovider openrouter https://openrouter.ai/api/v1 YOUR_OPENROUTER_KEY gpt-4 claude-3-opus anthropic/claude-3-5-sonnet
!provider set openrouter
If you have a local model server (vLLM, LocalAI, Ollama):
!addprovider local http://your-server:8000/v1 none llama-2-7b mistral-7b
!provider set local
wrangler tail# List all keys
wrangler kv:key list --binding KV
# Get a specific key
wrangler kv:key get "sync:token" --binding KVwrangler r2 object list matrix-bot-storageAfter making changes:
wrangler deployThe bot will continue running with the new code.
Error: In order to use Durable Objects with a free plan, you must create a namespace using a new_sqlite_classes migration.
Solution: Ensure your wrangler.toml uses new_sqlite_classes:
[[migrations]]
tag = "v1"
new_sqlite_classes = ["MatrixSync"] # Required for Cloudflare free planIf you already deployed with new_classes, you may need to:
- Delete the worker:
wrangler delete - Update
wrangler.tomlto usenew_sqlite_classes - Deploy again:
wrangler deploy
-
Check Matrix credentials:
- Verify username format:
@user:server.org - Password is correct
- Account is not locked
- Verify username format:
-
Check Matrix homeserver:
- Verify
MATRIX_HOMESERVERinwrangler.toml - Try accessing the homeserver URL in a browser
- Verify
- Check sync status:
curl https://your-worker.workers.dev/status- If not running, restart:
curl https://your-worker.workers.dev/start- Check logs:
wrangler tail- Verify API key is correct
- Check API balance/quota
- Test the API directly:
curl https://api.openai.com/v1/models \
-H "Authorization: Bearer YOUR_API_KEY"If you see Durable Object errors:
- Check
wrangler.tomlmigrations section - Re-deploy:
wrangler deploy- Verify namespace/bucket IDs in
wrangler.toml - Check bindings are correct
- Ensure account has permissions
With Cloudflare's free tier:
- Workers: 100,000 requests/day (free)
- KV: 100,000 reads/day, 1,000 writes/day (free)
- R2: 10GB storage, 1M operations/month (free)
- Durable Objects: 1M requests/month (free)
For typical usage (5-10 rooms, moderate activity):
- Cost: $0/month (free tier sufficient)
For heavy usage (50+ rooms, high activity):
- Workers: ~$5/month
- KV: ~$0.50/month
- R2: ~$0.15/month
- Durable Objects: ~$5/month
- Total: ~$10-15/month
- Never commit secrets: Always use
wrangler secret put - Use admin whitelist: Set
BOT_ADMIN_USERSto restrict admin commands - Enable user whitelist: Use
!whitelistcommand to restrict bot access - Rotate API keys: Change keys periodically
- Monitor logs: Check for suspicious activity
The bot automatically backs up configuration to R2 at:
config/backup-{timestamp}.json
# Backup KV data
wrangler kv:key list --binding KV > kv-backup.json
# Backup R2 data
wrangler r2 object list matrix-bot-storage > r2-backup.json# Restore KV key
wrangler kv:key put "provider:openai" --binding KV < provider.json
# Re-login to Matrix
curl -X POST https://your-worker.workers.dev/loginEdit wrangler.toml:
[triggers]
crons = ["*/5 * * * *"] # Every 5 minutes instead of 2In Matrix room:
!setconfig maxContextMessages 50
In Matrix room:
!setprompt You are a helpful AI assistant specializing in programming and technology.
- Secrets configured
- KV namespace created and configured
- R2 bucket created and configured
- Bot logged in to Matrix
- Sync loop started
- Admin users configured
- Default provider configured
- Bot tested in a room
- Monitoring enabled
- Backup strategy in place
- Documentation reviewed
For issues and questions:
- Check logs:
wrangler tail - Review documentation: README.md
- Open an issue on GitHub