Deployment Guide: Notion to Slack Sync
Overview
This guide covers deploying the Notion to Slack sync system. The system automatically syncs changes from Notion databases to the local SQLite database and triggers Slack home page refreshes.
Architecture
Notion Database Changes
Backend Sync Endpoint (/api/notion/sync/from-notion)
SQLite Database Updated
SlackHomeRefreshService Called
Node.js Slack Bot API (/api/slack/refresh-homes)
Slack Home Pages Refreshed for All UsersPrerequisites Check
Run the test script to verify everything is configured:
cd /home/ubuntu/yappa-knowledge-hub
./scripts/test-sync.shQuick Setup (2 minutes)
1. Update Environment Variables
Backend (.env in https://github.com/undead2146/KnowledgeHub/blob/main/backend/):
SLACK_BOT_URL=http://localhost:3001
NOTION_WEBHOOK_SECRET= # Optional, for webhook verificationFrontend (.env in /home/ubuntu/yappa-knowledge-hub/):
API_PORT=3001
SLACK_BOT_URL=http://localhost:30012. Start Services
Start Slack Bot:
cd /home/ubuntu/yappa-knowledge-hub
npm startThe bot will start on:
- Port 3000: Slack Socket Mode
- Port 3001: Internal API for home refresh
Start Backend (in a new terminal):
cd /home/ubuntu/yappa-knowledge-hub/backend
symfony server:startOr if you don't have Symfony CLI:
cd /home/ubuntu/yappa-knowledge-hub/backend
php -S localhost:8000 -t public/3. Set Up Auto-Sync
Option A: Cron Job (Recommended for Production)
crontab -eAdd this line to sync every 2 minutes:
*/2 * * * * /home/ubuntu/yappa-knowledge-hub/auto-sync.shSave and verify:
crontab -lOption B: Manual Testing
cd /home/ubuntu/yappa-knowledge-hub
./auto-sync.shOption C: Watch Mode (Development)
# Run sync every 30 seconds (for testing)
watch -n 30 ./auto-sync.shTesting
1. Test Backend Sync
curl -X POST http://localhost:8000/api/notion/sync/from-notion \
-H "Content-Type: application/json" \
-d '{"force": true, "limit": 100}'Expected response:
{
"message": "Sync completed",
"created": 0,
"updated": X,
"skipped": Y,
"failed": 0
}2. Test Slack Home Refresh
curl -X POST http://localhost:3001/api/slack/refresh-homesExpected response:
{
"success": true,
"refreshed": 0,
"failed": 0
}Note: refreshed: 0 is expected until user tracking is implemented.
3. End-to-End Test
- Open Slack and go to the Yappa Knowledge Hub app home tab
- Make a change in Notion (edit a knowledge item)
- Wait 2 minutes (or run
./auto-sync.shmanually) - Refresh the Slack app home tab
- Your changes should appear
4. Test Health Endpoints
# Check Slack bot
curl http://localhost:3001/health
# Check backend
curl http://localhost:8000/api/notion/verifyProduction Deployment with PM2
Install PM2
npm install -g pm2Start Bot with PM2
cd /home/ubuntu/yappa-knowledge-hub
pm2 start src/app.js --name yappa-bot
pm2 save
pm2 startupManage PM2 Process
# View logs
pm2 logs yappa-bot
# Restart
pm2 restart yappa-bot
# Stop
pm2 stop yappa-bot
# Status
pm2 statusAPI Endpoints
Backend (Port 8000)
POST /api/notion/sync/from-notion- Sync from Notion to local DBPOST /api/notion/sync/categories-from-notion- Sync categoriesPOST /api/notion/sync/bidirectional- Full bidirectional syncGET /api/notion/sync/status- Get sync statusGET /api/notion/verify- Verify Notion connectionPOST /api/webhooks/notion- Webhook endpoint for Notion (optional)POST /api/webhooks/notion/trigger-sync- Manual trigger endpoint
Frontend (Port 3001)
POST /api/slack/refresh-homes- Refresh all user home pagesPOST /api/slack/refresh-home- Refresh specific user home pageGET /health- Health check
Monitoring
Check Sync Logs
tail -f /tmp/notion-sync.logCheck Backend Logs
tail -f https://github.com/undead2146/KnowledgeHub/blob/main/backend/var/log/dev.logCheck Bot Logs
If running with npm start, logs appear in the console.
If using PM2:
pm2 logs yappa-botTroubleshooting
Services Not Running
# Check if bot is running
curl http://localhost:3001/health
# Check if backend is running
curl http://localhost:8000/api/notion/verify
# Check processes
ps aux | grep node
ps aux | grep phpSync Not Working
# Test sync manually
./auto-sync.sh
# Check logs
tail -f /tmp/notion-sync.log
# Test backend directly
curl -X POST http://localhost:8000/api/notion/sync/from-notion \
-H "Content-Type: application/json" \
-d '{"force": true}'Home Not Refreshing
# Test refresh
curl -X POST http://localhost:3001/api/slack/refresh-homes
# Check bot logs
tail -f logs/slack-bot.logCron Job Not Running
# Check if cron job is configured
crontab -l
# Check cron logs
grep CRON /var/log/syslog | tail -20
# Test script manually
/home/ubuntu/yappa-knowledge-hub/auto-sync.sh
# Check script permissions
ls -la /home/ubuntu/yappa-knowledge-hub/auto-sync.shKnown Limitations
User Tracking
The current implementation has a limitation in the home refresh service. The getActiveUsers() function in https://github.com/undead2146/KnowledgeHub/blob/main/src/services/homeRefresh.js returns an empty array by default.
Impact: When refreshAllHomePages() is called, it won't refresh any homes because there are no tracked users.
Solutions:
- Implement user tracking: Track users when they open the app home and store in Redis/database
- Use Slack API: Call
users.list()to get all workspace members (can be expensive) - Manual refresh: Users can manually refresh by opening the app home tab
Recommended approach: Implement user tracking by modifying https://github.com/undead2146/KnowledgeHub/blob/main/src/handlers/home.js to call trackUserHomeOpen(userId) when users open the home page.
Sync Frequency
Adjust crontab to change frequency:
*/1 * * * * # Every 1 minute (fast, more API calls)
*/2 * * * * # Every 2 minutes (recommended)
*/5 * * * * # Every 5 minutes (slower, fewer API calls)
*/10 * * * * # Every 10 minutes (slow)Optional: Notion Webhooks
For instant updates instead of polling:
- Create a webhook in Notion pointing to:
https://your-domain.com/api/webhooks/notion - Set
NOTION_WEBHOOK_SECRETin backend/.env - Configure your reverse proxy to forward webhook requests to port 8000
Security Notes
- Webhook Secret: Set
NOTION_WEBHOOK_SECRETto verify webhook authenticity - Internal API: The Slack API endpoints (port 3001) should not be exposed publicly
- Firewall: Ensure ports 3000, 3001, and 8000 are only accessible internally
Performance Considerations
Database Size
- SQLite is suitable for small to medium workloads
- For large datasets (>10,000 items), consider PostgreSQL or MySQL
Log Rotation
Set up log rotation for /tmp/notion-sync.log to prevent disk space issues.
Next Steps
- Implement user tracking in homeRefresh.js for proper home page refreshes
- Set up monitoring for sync failures and API errors
- Configure Notion webhooks for instant updates (optional)
- Set up log rotation for /tmp/notion-sync.log
- Add metrics to track sync performance and success rates