Simplifying User Input with Message Suggestions

Simplifying User Input with Message Suggestions

Welcome back! In the previous lesson, you learned how to integrate API requests to create a dynamic chat interface. This allowed for real-time communication between the user and the server, enhancing the interactivity of our application. Today, we will build on that foundation by adding message suggestions to our chat interface. Message suggestions are predefined prompts that users can select to quickly send common queries. This feature not only improves user experience by making interactions more efficient but also guides users in formulating their questions.

Enhancing the Chat Interface with Suggestions

To implement message suggestions, we will add buttons to our chat.html file. These buttons will represent common queries that users might have, such as asking about services, business hours, or contact information. By clicking on these buttons, users can quickly send these queries without typing them out manually.

We will position these buttons within a div element with the class suggestions, which will be placed above the chat container and input elements. This placement ensures that the suggestion buttons are easily accessible to users as they interact with the chat interface.

Here's how you can add these buttons to your HTML:

HTML
<!DOCTYPE html>
<html>
<!-- Head section... -->
<body>
    <!-- Header section... -->
    <div class="suggestions">
        <button class="suggestion-btn" onclick="usePrompt('What services do you offer?')">Our Services</button>
        <button class="suggestion-btn" onclick="usePrompt('What are your business hours?')">Business Hours</button>
        <button class="suggestion-btn" onclick="usePrompt('What is your contact email?')">Contact Email</button>
    </div>
    <!-- Chat container and input elements... -->
    <!-- Script functions... -->
</body>
</html>

These buttons are styled with the class suggestion-btn and use the onclick attribute to call the usePrompt function with specific queries. The content of the buttons includes:

  • "Our Services" for the query "What services do you offer?"
  • "Business Hours" for the query "What are your business hours?"
  • "Contact Email" for the query "What is your contact email?"

This setup allows users to interact with the chat interface more efficiently by selecting from a list of these predefined questions.

Implementing the usePrompt Function

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal