This folder contains the Node.js rewrite of the old ASP.NET Core backend.
chatbotfbNode is the runtime core of the platform:
- Receives Facebook webhook events and sends bot replies.
- Hosts auth and dashboard APIs used by the Vue frontend.
- Stores operational data in MongoDB.
- Calls Gemini for AI answer generation.
The frontend repo (chatbotfbweb) does not replace this runtime role. It only consumes backend APIs.
- Facebook webhook verification and message receive flow.
- Dashboard CRUD for knowledge base and authorized users.
- Google login for the dashboard with HttpOnly cookie session auth.
- Bot command support is split into individual files under
src/botCommands. - Current commands:
/ask,/about,/echo,/time,/weather,/uptime,/ping,/me,/fb,/link,/random,/pick,/mem,/top,/history,/help.
src/commands.tskeeps the sharedBotCommandContext,BotCommandHandler, and dispatcher.src/botCommands/*.tscontains one command per file.src/botCommands/shared.tscontains shared helper functions used by multiple commands.
Useful aliases:
/hfor help/timefor time/upfor uptime/sayfor echo/infofor about
/ask now runs in 2 phases to reduce token usage and improve context accuracy:
- Bot sends an immediate waiting message: AI is searching for documents.
- Phase 1 (planner): send Gemini the user question, 3 recent user ask messages, and only knowledge metadata (
id,topic,keywords). - If planner says data is enough, bot returns answer immediately.
- If planner requests more context, backend loads only requested knowledge documents from MongoDB and calls Gemini again for the final answer.
This keeps responses fast while still allowing deep answers when needed.
Picks one or more random items from a semicolon-separated list. Useful for group decisions.
Usage:
/pick -l item1; item2; item3→ picks 1 random item (default)/pick -n 2 -l ăn cơm; ăn bún; ăn chè; uống cà phê→ picks 2 random items without replacement
Parameters:
-l(required): List of items separated by semicolons-n(optional): Number of items to pick (default: 1)
Example: /pick -n 3 -l A; B; C; D; E returns 3 random items
Shows weather for today or forecast for N days later.
Usage:
/weather→ today at default location (Da Nang)/weather Hue→ today in Hue/weather 3 Hue→ forecast 3 days later in Hue/weather 5→ forecast 5 days later in default location
Parameters:
day(optional): day offset from today, default0, max5location(optional): city/location, default fromOpenWeather__DefaultLocation
Copy .env.example to .env and fill in the secrets. The loader also accepts a .env file placed in a parent folder, so you can keep the same environment layout you used for the .NET app.
Important environment keys:
Mongo__ConnectionStringMongo__DatabaseNameFacebook__PageAccessTokenFacebook__PageIdFacebook__GraphApiVersionFacebook__AppSecretGemini__ApiKeyOpenWeather__ApiKeyWebhook__VerifyTokenAuth__GoogleClientIdAuth__GoogleClientSecretAuth__OAuthRedirectAuth__FrontendBaseUrlAuth__SessionSecret
Run locally:
npm install
npm run devBuild for production:
npm run build
npm startThe server listens on PORT and defaults to 5000.
- The Node version keeps the original API shape so the Facebook webhook, admin dashboard, and bot commands can be swapped in without changing the external flow.
- The Facebook callback URL can point to either
/webhookor/api/webhook, but it must target the Node backend domain, not the frontend Vercel site. - Webhook signature verification is supported when
Facebook__AppSecretis set. - The app fails fast on weak or missing
Auth__SessionSecretvalues instead of silently booting with the old default. - The dashboard and auth cookies rely on the backend being served behind HTTPS in production.
- Message history persistence is intentionally scoped: only user
/askmessages and bot replies in/askflow are stored in MongoDB.
- Facebook users chat with the bot through backend webhook routes.
- Admin users manage knowledge and authorized users through the Vue frontend.
- Frontend calls backend
/api/*endpoints with cookie-based auth. - Backend uses MongoDB collections:
knowledge_basemessagesauthorized_users
npm run checknpm run build