Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue fixed mini chatbot #370

Merged

Conversation

rutikakengal
Copy link
Contributor

@rutikakengal rutikakengal commented Oct 20, 2024

#360 Add mini chattbot issue fixed , This chatbot gives quike response to the users questions and runs smoothly , @GarimaSingh0109 Add gssoc-ext, Hactoberfest-accepted and level 3 to this PR ,

Screenrecording

Screen.Recording.2024-10-20.162809.mp4

Summary by CodeRabbit

  • New Features

    • Introduced a web-based chat interface for waste management chatbot.
    • Added a chatbot button linking to the new chat interface on the main page.
  • Enhancements

    • Improved user interface with new styles for the chatbot button and tooltip.
    • Adjusted the position and appearance of the scroll-to-top button.

These changes enhance user interaction and accessibility within the waste management application.

Copy link
Contributor

coderabbitai bot commented Oct 20, 2024

Walkthrough

The changes introduce a new web-based chat interface for a waste management chatbot through the addition of chatbot.html. This file includes necessary HTML structure, external CSS for styling, and JavaScript for managing chat interactions. The index.html file is modified to include a chatbot button that links to the new chat interface, along with adjustments to existing UI elements for improved layout and functionality.

Changes

File Change Summary
chatbot.html Introduced a web-based chat interface with HTML structure, linked external stylesheets, and JavaScript for chat interactions, including functions for sending messages and handling images.
index.html Added a chatbot button linking to chatbot.html, introduced new CSS classes for chatbot styling, and adjusted the position and color of the existing scroll-to-top button.

Poem

In the garden where ideas bloom,
A chatbot now dispels the gloom.
With messages sent and images shared,
A friendly chat, no need to be scared.
Click the button, give it a try,
Waste management's new friend is nigh! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Oct 20, 2024

Deploy Preview for manageyourwaste failed. Why did it fail? →

Name Link
🔨 Latest commit 3c50081
🔍 Latest deploy log https://app.netlify.com/sites/manageyourwaste/deploys/6714e4b6a62048000866976e

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (3)
index.html (2)

17-55: LGTM! Consider adding focus styles for keyboard accessibility.

The new CSS classes for the chatbot button are well-structured and provide a good user experience. The fixed positioning and z-index ensure the button is always visible, and the tooltip is a nice touch.

Consider adding focus styles to the .chatbot-button class to improve keyboard accessibility. For example:

.chatbot-button:focus {
    outline: 2px solid #fff;
    outline-offset: 2px;
}

493-503: LGTM! Consider adding aria-label for screen readers.

The new HTML structure for the chatbot button is well-organized and includes all necessary elements. The image and tooltip enhance the user experience, and the button is properly linked to the chatbot page.

To improve accessibility for screen readers, consider adding an aria-label to the button. For example:

<button class="chatbot-button group" aria-label="Open chatbot">

This will provide context for users relying on screen readers.

chatbot.html (1)

105-105: Remove unnecessary background color styling

The background color set for the body might have been used for development or testing purposes. If it's not intended for the final design, consider removing it to avoid unintended styling in production.

Apply this diff to remove the background color:

- background-color: #42c553; /* Optional: to visualize the centering */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 388d133 and 3c50081.

⛔ Files ignored due to path filters (2)
  • bot.jpg is excluded by !**/*.jpg
  • chatbot.gif is excluded by !**/*.gif
📒 Files selected for processing (2)
  • chatbot.html (1 hunks)
  • index.html (2 hunks)
🧰 Additional context used
🪛 Gitleaks
chatbot.html

124-124: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

🔇 Additional comments (3)
index.html (2)

56-64: LGTM! Good adjustments for visual consistency.

The changes to the scroll-to-top button's position and color are appropriate. Moving it up accommodates the new chatbot button, and matching the color creates visual consistency.


Line range hint 1-553: Overall, great implementation of the chatbot feature!

The changes in this file successfully introduce the mini chatbot feature as outlined in the PR objectives. The new chatbot button is well-integrated into the existing page, with appropriate styling and positioning. The modifications to the scroll-to-top button ensure visual consistency and prevent overlap with the new chatbot button.

A few minor suggestions for accessibility improvements have been made, which would further enhance the user experience for all users.

chatbot.html (1)

170-196: Check for unhandled exceptions during message generation

While the try-catch block handles errors during message generation, consider ensuring that all possible exceptions are properly caught and logged. Additionally, verify that the user receives appropriate feedback in all error scenarios.

Run the following script to check for any unhandled promise rejections or exceptions in the codebase related to sendMessage function:

This script will help identify any async functions that may not be properly handling exceptions.

Comment on lines +221 to +227
if (message.startsWith("```") && message.endsWith("```")) {
// Code block handling
const codeContent = message.substring(3, message.length - 3);
const escapedCode = codeContent
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
msgDiv.innerHTML = `<pre><code class="hljs">${escapedCode}</code></pre>`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Address potential XSS vulnerabilities due to insufficient input sanitization

When setting innerHTML with user-generated content, there's a risk of Cross-Site Scripting (XSS) attacks. The current code replaces < and > characters, but this may not be sufficient to prevent XSS in all cases. Consider using a robust sanitization library like DOMPurify to ensure all malicious scripts are neutralized.

To fix this issue, you can include DOMPurify and sanitize the content before setting innerHTML:

Add DOMPurify to your project:

<script src="https://cdn.jsdelivr.net/npm/dompurify@2.4.0/dist/purify.min.js"></script>

Update the code block handling in both AI and user messages:

- msgDiv.innerHTML = `<pre><code class="hljs">${escapedCode}</code></pre>`;
+ const safeCode = DOMPurify.sanitize(escapedCode);
+ msgDiv.innerHTML = `<pre><code class="hljs">${safeCode}</code></pre>`;

For regular messages:

- msgDiv.innerHTML = marked.parse(message);
+ const safeMessage = DOMPurify.sanitize(marked.parse(message));
+ msgDiv.innerHTML = safeMessage;

Also applies to: 260-266

Comment on lines +221 to +250
if (message.startsWith("```") && message.endsWith("```")) {
// Code block handling
const codeContent = message.substring(3, message.length - 3);
const escapedCode = codeContent
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
msgDiv.innerHTML = `<pre><code class="hljs">${escapedCode}</code></pre>`;
window.hljs.highlightBlock(msgDiv.querySelector("code"));

// Add Copy button
const copyButton = document.createElement("button");
copyButton.innerText = "Copy code";
copyButton.onclick = function () {
navigator.clipboard.writeText(codeContent).then(
() => {
// Change text to show confirmation instead of using alert
copyButton.innerText = "Copied!";
// Optional: revert the button text back to "Copy code" after 2 seconds
setTimeout(() => {
copyButton.innerText = "Copy code";
}, 2000);
},
(err) => {
console.error("Failed to copy text: ", err);
}
);
};

msgDiv.appendChild(copyButton);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor duplicated code for code block handling

The code for handling code blocks is duplicated in both the AI and user message rendering logic. To enhance maintainability and follow the DRY (Don't Repeat Yourself) principle, consider extracting this logic into a reusable function.

You can create a helper function to handle code blocks:

function renderCodeBlock(codeContent) {
  const escapedCode = codeContent.replace(/</g, "&lt;").replace(/>/g, "&gt;");
  return `<pre><code class="hljs">${escapedCode}</code></pre>`;
}

function addCopyButton(msgDiv, codeContent) {
  const copyButton = document.createElement("button");
  copyButton.innerText = "Copy code";
  copyButton.onclick = function () {
    navigator.clipboard.writeText(codeContent).then(
      () => {
        copyButton.innerText = "Copied!";
        setTimeout(() => {
          copyButton.innerText = "Copy code";
        }, 2000);
      },
      (err) => {
        console.error("Failed to copy text: ", err);
      }
    );
  };
  msgDiv.appendChild(copyButton);
}

Then, update your displayMessage function to use these helpers:

if (message.startsWith("```") && message.endsWith("```")) {
  const codeContent = message.substring(3, message.length - 3);
  msgDiv.innerHTML = renderCodeBlock(codeContent);
  window.hljs.highlightBlock(msgDiv.querySelector("code"));
  addCopyButton(msgDiv, codeContent);
} else {
  msgDiv.innerHTML = DOMPurify.sanitize(marked.parse(message));
}

This approach reduces code duplication and makes future maintenance easier.

Also applies to: 260-283

Comment on lines +190 to +193
displayMessage(
"This content is not safe for display based on current settings. or an internal error.",
"ai"
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve error message clarity for better user experience

The error message displayed to the user combines two different scenarios, which might be confusing. Consider providing a clearer and more user-friendly message when an error occurs during message generation.

Apply this diff to refine the error message:

- displayMessage(
-   "This content is not safe for display based on current settings. or an internal error.",
-   "ai"
- );
+ displayMessage(
+   "Sorry, an error occurred while processing your request. Please try again later.",
+   "ai"
+ );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
displayMessage(
"This content is not safe for display based on current settings. or an internal error.",
"ai"
);
displayMessage(
"Sorry, an error occurred while processing your request. Please try again later.",
"ai"
);

chatbot.html Show resolved Hide resolved
@GarimaSingh0109 GarimaSingh0109 merged commit 8d89c31 into GarimaSingh0109:main Oct 20, 2024
2 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants