Go to file
2024-10-03 12:46:33 -04:00
models first commit 2024-10-03 12:22:56 -04:00
.gitignore first commit 2024-10-03 12:22:56 -04:00
package.json first commit 2024-10-03 12:22:56 -04:00
README.md fix readme 2024-10-03 12:46:33 -04:00
short-backend-api.js first commit 2024-10-03 12:22:56 -04:00
short.js first commit 2024-10-03 12:22:56 -04:00

URL Shortener Bot with Backend API

This repository contains two components: a Discord bot for shortening URLs and a backend API for handling the URL shortening and redirection. Both parts work together to provide a seamless experience for shortening URLs and sharing them directly from Discord.

Features

  • Discord Slash Command for URL shortening with domain selection.
  • Express.js Backend API to handle the actual shortening process and redirection.
  • MongoDB Integration for storing and retrieving shortened URLs.
  • API Key Validation for secure access to the backend.

Components

1. short.js - Discord URL Shortener Bot

This is a Discord bot built using discord.js that provides a command to shorten URLs directly from Discord. Users can choose from multiple domains, or the bot will use a default domain.

Setup Instructions for short.js

  1. Install dependencies:

    npm install discord.js unirest dotenv
    
  2. Environment Variables: Create a .env file in the project root and add:

    BOT_TOKEN=your_discord_bot_token
    DISCORD_CLIENT_ID=your_discord_client_id
    API_KEY=your_url_shortening_api_key
    
  3. Run the bot:

    node short.js
    

How It Works

  • Users enter a URL and optionally choose a domain using the /shortenurl slash command.
  • The bot validates the URL and sends a request to the backend API to generate the shortened link.
  • The response is sent back as a Discord embed message with the shortened URL.

2. short-backend-api.js - URL Shortener Backend

This is a Node.js backend API using Express.js and MongoDB for URL shortening. It receives requests from the Discord bot or any client and provides a short URL in response.

Setup Instructions for short-backend-api.js

  1. Install dependencies:

    npm install express mongoose shortid dotenv
    
  2. MongoDB Setup: Make sure MongoDB is running and accessible. By default, it connects to mongodb://127.0.0.1:27017/shorturl.

  3. Environment Variables: Create a .env file in the project root with:

    API_KEY=your_secure_api_key
    
  4. Run the API:

    node short-backend-api.js
    

Routes

  • POST /api/shorturl: Shortens a URL.
    • Request:
      • Body: { "longUrl": "https://example.com", "domain": "s.shells.lol" }
      • Header: x-api-key: your_api_key
    • Response: { "shortUrl": "https://s.shells.lol/abcd1234" }
  • GET /:shortId: Redirects to the original long URL.

How It Works

  • The API checks if the provided API key is valid.
  • The longUrl is checked against the database. If it has already been shortened, the existing short URL is returned.
  • If it's a new URL, it generates a unique shortId using shortid and stores it in the database.
  • The shortId can then be used to redirect users to the original URL when they visit https://yourdomain.com/:shortId.

Models

Url Model

This model represents the schema for storing URLs in MongoDB. It consists of the following fields:

  • longUrl: The original URL.
  • shortId: The shortened identifier for the URL.
const mongoose = require('mongoose');

const urlSchema = new mongoose.Schema({
  longUrl: { type: String, required: true },
  shortId: { type: String, required: true }
});

module.exports = mongoose.model('Url', urlSchema);

Full Setup

Prerequisites

  • Node.js v16.6.0 or higher
  • MongoDB running locally or remotely
  • A valid Discord bot token and a URL shortening API key

Steps

  1. Clone the repository:

    git clone https://git.ssh.surf/dlinux-community/url-shortener.git
    cd url-shortener
    
  2. Follow the setup instructions for both short.js and short-backend-api.js.

  3. Ensure the bot and backend API are both running:

    # In one terminal, run the backend API
    node short-backend-api.js
    
    # In another terminal, run the Discord bot
    node short.js