ChatBot is an open-source, production-oriented chatbot reference implementation inspired by modern conversational UIs such as ChatGPT and Gemini. It demonstrates a modular architecture, session management, persistent storage, and third-party API integrations (Discord OAuth2, Google Gemini, GROQ). Use this repository to learn, prototype, or extend a real-world chatbot architecture.
For a live working example, visit: https://chat.admibot.xyz
- Modular, production-style code organization
- User sessions with persistent storage
- Discord OAuth2 login support
- Google Gemini API integration
- GROQ integration
- CDN hooks for custom user icons
- Deployment & tunnel guides (Cloudflare Tunnels)
- Production-ready patterns for experimentation and learning
- Prerequisites
- Project setup
- Environment variables (
.env.local
) - Configuration (
public/config.yml
) - MongoDB: step-by-step
- Google Gemini API key
- GROQ API key
- Discord OAuth2 setup
- Install, build & run
- Cloudflare Tunnel (Zero Trust) — quick guide
- Notes, security & best practices
- Support / More projects
- Recommended OS: Ubuntu / Debian (Windows can be used but requires additional build tools for Next.js).
- Node.js: v20 or v21 (use
node -v
to confirm). - A domain added to Cloudflare (required if you plan to expose your local instance via Cloudflare Tunnels).
- A MongoDB Atlas account (or any MongoDB instance reachable from your server).
- Accounts/keys for Google Gemini and GROQ (see sections below).
- A Discord Application for OAuth2 (see section below).
git clone https://github.com/HasenDev/ChatBot.git
cd ChatBot
Create .env.local
in the project root and add the variables listed in the next section. Keep this file private — do not commit it to source control.
Create .env.local
with the following content (replace placeholder values with your credentials):
# Discord Application client ID (replace with your own)
DISCORD_CLIENT_ID=00000000000000
# Discord application client Secret (replace with your own)
DISCORD_CLIENT_SECRET=your_discord_client_secret_here
# Secret for NextAuth authentication (replace with a strong random string)
NEXTAUTH_SECRET=replace_with_a_strong_random_secret
# URL where your Next.js app is hosted (e.g. https://chat.example.com)
NEXTAUTH_URL=https://ex.com
# MongoDB connection string (replace with your own credentials)
MONGODB_URI="mongodb+srv://username:password@cluster.mongodb.net/dbname?retryWrites=true&w=majority"
# Discord bot token (replace with your bot's token)
DISCORD_BOT_TOKEN="your_discord_bot_token_here"
# Google Gemini API key (replace with a valid key from Google Gemini Studio)
GEMINI_API_KEY=replace_with_google_api_key
# GROQ API key for querying your headless CMS (replace with a valid key from https://console.groq.com/playground)
GROQ_API_KEY=replace_with_groq_api_key
# --------------------------------------------------------
# CDN feature: used for uploading custom user icons in settings.
# These are placeholders. Implement your own CDN endpoint or modify lib/UploadCDN.ts.
# --------------------------------------------------------
# CDN upload URL (placeholder)
CDN_UPLOAD_URL=https://cdn.admibot.xyz
# Secret key for CDN uploads (placeholder)
CDN_SECRET_KEY=replace_with_your_own_secret_key
Important: Use a strong random NEXTAUTH_SECRET
(e.g., openssl rand -hex 32
) and never share your .env.local
publicly.
Edit public/config.yml
to set branding and static site values:
siteName: "ChatBot" # Replace with your ChatBot name
iconPath: "https://your.cdn/path/to/icon.webp"
Example:
siteName: "ChatBot by AdmiBot"
iconPath: "https://cdn.example.com/icons/chatbot-300x300.webp"
- Sign in to MongoDB Cloud (https://cloud.mongodb.com/) and create a project.
- Inside the project click Create Cluster → choose Free Tier (or another cluster type).
- Configure and create the cluster. Wait until the cluster shows Running.
- Click Connect for the cluster and follow the steps:
- Create a database user (username + password) and note them.
- Under Network Access add your IP (or allow access from anywhere while testing — not recommended for production).
- Choose Connect your application and copy the connection string (select the Node.js driver).
Each step screenshot is shown below.
- Paste the connection string into
MONGODB_URI
in.env.local
and replace<username>
,<password>
, and<dbname>
.
- Visit Google Gemini Studio (e.g., https://aistudio.google.com/) and sign in to your Google account.
- Create or select a Google Cloud project.
- Generate an API key (follow the UI: Get API Key → Create API Key).
- Restrict the API key (recommended): restrict by IP and API usage in Google Cloud Console.
- Put the key into
GEMINI_API_KEY
in.env.local
.
- Log in to GROQ Console (https://console.groq.com/playground).
- Go to API Keys → Create API Key, name it, complete any required CAPTCHAs, then create.
- Copy the generated key into
GROQ_API_KEY
in.env.local
. - Now you can use models like ChatGPT-OSS, DeepSeek and LLama
-
Open the Discord Developer Portal (https://discord.com/developers/applications) and log in.
-
Create a New Application, name it and save.
-
In OAuth2 settings:
- Copy Client ID → set
DISCORD_CLIENT_ID
in.env.local
. - Click Reset Secret (or Client Secret) → copy and set
DISCORD_CLIENT_SECRET
in.env.local
.
- Copy Client ID → set
-
Add Redirect URI for NextAuth:
https://yourappdomain.com/api/auth/callback/discord
- Set
NEXTAUTH_URL
to the public URL of your app and use a secureNEXTAUTH_SECRET
.
Ensure Node.js v20/21 is installed.
# Install dependencies
npm install
# Build the Next.js app
npm run build
# Start the server (default port used by this project shown below)
npm run start
By default the application serves on port 9700. Visit http://localhost:9700
to verify.
If you change the port in your runtime configuration, update any tunneling or reverse-proxy settings accordingly.
This guide shows how to expose a local instance through Cloudflare Zero Trust Tunnels (
cloudflared
). It assumes you have a domain added to your Cloudflare account.
- Sign in to your Cloudflare account at https://dash.cloudflare.com.
- In the left sidebar scroll and click Zero Trust.
- Once Zero Trust loads, click Network in the Zero Trust sidebar.
- Select Tunnels → Create a Tunnel (or New Tunnel).
- Choose Cloudflare (the UI option to create a new tunnel), then select your operating system and architecture when prompted.
- Cloudflare will display a generated install command for
cloudflared
. Run that command on your server to installcloudflared
. - After installation, Cloudflare will show a service install command that looks like:
sudo cloudflared service install eyJhIjoiNm.....
Execute the sudo cloudflared service install ...
command on your server. This registers the tunnel as a system service and starts it.
- Click Next in the Cloudflare UI. You will be prompted to create a hostname. Provide:
- Hostname: the subdomain you want, e.g.
chat.example.com
- Type:
HTTP
- URL:
http://<local-ip>:9700
Notes:
-
You can try
http://localhost:9700
first. If the hostname doesn’t route correctly, determine your machine’s local IP:- Linux/macOS: run
ip a
(orifconfig
) - Windows: run
ipconfig
- Linux/macOS: run
-
Use the local IP (e.g.,
http://192.168.1.100:9700
) when the tunnel needs to target a specific network interface. -
Requirements: your domain must be added to Cloudflare; the tunnel steps are available after
npm run start
and the app is listening on the chosen port.
When complete, Cloudflare will create DNS records and route traffic from your chosen hostname through the tunnel to your local app. Verify by visiting your hostname in a browser.
- Never commit
.env.local
to source control. Use secret managers for production. - For production use, the repository requires a license key from the Owner. This project is explicitly limited to personal, educational, or non-commercial use unless you obtain authorization.
- Use HTTPS endpoints for any public-facing hostnames. Cloudflare provides TLS termination for tunnels.
- Rotate API keys and credentials periodically. Keep least privilege on all keys.
- If you expose the app to the Internet, harden your app (rate limiting, input validation, secure CSP headers, etc.).
- Replace placeholder CDN upload endpoints with your own implementation and secure server-side validation in
lib/UploadCDN.ts
.
If you find this project useful, consider starring the repository. For more projects and tools from the AdmiBot team, see:
- AdmiBot: https://admibot.xyz — An all-in-one Discord Bot and toolkit.
Acknowledgements This project is authored and maintained by Hassen / the AdmiBot Team. Use responsibly and follow the license terms above.