help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
nlp-text-analysis-bot
/
static
RSK World
nlp-text-analysis-bot
NLP Text Analysis Bot - Python + NLP + Flask + Machine Learning + Text Analysis + AI
static
  • script.js4 KB
  • style.css6.7 KB
script.js
static/script.js
Raw Download
Find: Go to:
/*
Project: Customer Service Bot
Author: RSK World
Website: https://rskworld.in
Contact: info@rskworld.com, support@rskworld.com
Phone: +91 93305 39277
Year: 2026
*/

document.addEventListener('DOMContentLoaded', () => {
    const chatBox = document.getElementById('chat-box');
    const userInput = document.getElementById('user-input');
    const sendBtn = document.getElementById('send-btn');

    // Auto scroll to bottom
    const scrollToBottom = () => {
        chatBox.scrollTop = chatBox.scrollHeight;
    };

    const appendMessage = (text, sender, isHtml = false) => {
        const msgDiv = document.createElement('div');
        msgDiv.classList.add('message', sender);
        if (isHtml) {
            msgDiv.innerHTML = text;
        } else {
            msgDiv.textContent = text;
        }
        chatBox.appendChild(msgDiv);
        scrollToBottom();
    };

    const handleOptionClick = (optionText) => {
        if (optionText.includes("raise a ticket")) {
            startTicketProcess();
        } else {
            // Treat as normal message
            sendMessage(optionText);
        }
    };

    const startTicketProcess = () => {
        // Simple flow for ticket
        const issue = prompt("Please describe your issue:");
        if (!issue) return;

        const contact = prompt("Please provide your contact email:");
        if (!contact) return;

        // Call API
        fetch('/api/ticket', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ issue, contact })
        })
            .then(res => res.json())
            .then(data => {
                appendMessage(`Ticket created successfully! Ticket ID: ${data.ticket_id}. We will contact you at ${contact}.`, 'bot');
            })
            .catch(err => {
                appendMessage("Error creating ticket. Please try again.", 'bot');
            });
    };

    const sendMessage = (text) => {
        if (!text) return;

        appendMessage(text, 'user');
        userInput.value = '';

        // Show typing indicator (simulated)
        const loadingDiv = document.createElement('div');
        loadingDiv.classList.add('message', 'bot');
        loadingDiv.textContent = '...';
        loadingDiv.id = 'loading';
        chatBox.appendChild(loadingDiv);
        scrollToBottom();

        fetch('/api/chat', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ message: text })
        })
            .then(res => res.json())
            .then(data => {
                document.getElementById('loading').remove();

                if (data.type === 'option') {
                    let html = `${data.text}<br><div style="margin-top:10px;">`;
                    data.options.forEach(opt => {
                        const safeOpt = opt.replace(/'/g, "\\'");
                        html += `<button class="option-btn" onclick="document.dispatchEvent(new CustomEvent('option-clicked', {detail: '${safeOpt}'}))">${opt}</button>`;
                    });
                    html += `</div>`;
                    appendMessage(html, 'bot', true);
                } else {
                    appendMessage(data.text, 'bot');
                }
            })
            .catch(err => {
                document.getElementById('loading').remove();
                appendMessage("Sorry, something went wrong.", 'bot');
            });
    };

    // Event listener for custom option clicks from injected HTML
    document.addEventListener('option-clicked', (e) => {
        handleOptionClick(e.detail);
    });

    sendBtn.addEventListener('click', () => {
        sendMessage(userInput.value.trim());
    });

    userInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter') {
            sendMessage(userInput.value.trim());
        }
    });
});
120 lines•4 KB
javascript

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer