Creating a cutting-edge chat user interface for ionic3 integration with dialog flow

I have successfully set up a chat interface that displays messages on the left side of the screen when sent or received from dialog flow. However, I would like to customize it so that my messages appear on the right side while dialog flow messages remain on the left side, each represented by different colored chat bubbles. I am unsure how to achieve this differentiation and would appreciate some guidance. Below is my HTML code.

<ion-content>
  <div style="font-size:10px; text-align:center;">7 minutes ago</div>
  <div class="msgbubble" *ngFor="let msg of messages">
    <div class="innermsg">
      {{ msg }}
    </div>
  </div>
</ion-content>

<ion-footer>
  <ion-toolbar>
    <ion-input placeholder="Type here" [(ngModel)]="question" type="text"></ion-input>
    <ion-buttons right>
      <button ion-button clear icon-only id="sendicon" (click)="ask(question)">
        <ion-icon name="send"></ion-icon>
      </button>
    </ion-buttons>
  </ion-toolbar>
</ion-footer>

This is ts code.

ask(question) {
    try {
      this.messages.push(question);
      var headers = new Headers();
      headers.append("Accept", 'application/json');
      headers.append('Content-Type', 'application/json' );
      headers.append('Authorization', 'Bearer ' + this.Access_token)
      let options = new RequestOptions({ headers: headers });

      let postParams = {
       "lang": "en",
      "query": question ,
      "sessionId": "12345",
      "timezone": "America/New_York"
      }

      try{
      this.http.post("https://api.dialogflow.com/v1/query?v=20150910", postParams, options)
        .subscribe(data => {
          let obj = JSON.parse(data['_body']);
          this.answer = obj.result.fulfillment.speech;
          //console.log(data['_body']);
          console.log(this.answer);
          this.messages.push(this.answer);

         }, error => {
          console.log(error);// Error getting the data
        });
    }
    catch(e){
      console.log(e);
    }
    }

  catch(e){
    console.log(e);
  }
  }

I envision the UI resembling a typical chat page or bot where my messages are displayed on the right and incoming messages on the left.

Answer №1

To improve your array "messages," consider adding an attribute to distinguish between a question and an answer. It would be beneficial if you included the interface of "message" in your question. In Angular, you can modify CSS classes based on conditions like this:

<div [className]="condition ? 'example-class' : 'other-class'"></div>

I hope this solution resolves your issue!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The beautiful synergy between Vuetify's v-input component and overflow animation

I am having trouble implementing an overflow animation on vuetify's v-text-field component. Despite my efforts, I can't seem to make it work as intended: <script setup> const text = 'very long long long long long text' </scri ...

Add the phrase "choose an option" to a dropdown menu and then reset the dropdown in Angular

I need to implement a dropdown that initially displays a blank option such as "select an option", and when the user chooses an item from the dropdown, this initial field should disappear. Also, I want to automatically clear the selected option after a cert ...

Tips for passing operation enums within Angular services

Currently delving into Angular development and finding myself in need of a way to send a get request with enums included in the API call. Any insights on how this can be accomplished would be greatly appreciated! ...

Form Field Highlighting

I am currently attempting to eliminate the blue "halo" outline that appears on form elements in Firefox on OS X. I have successfully removed the halo in Safari on OS X using CSS with the following code: input { outline: none; } However, this method doe ...

What is the reason behind Angular displaying the output of a successful HTTP post request as 'undefined'?

During the development of my MEAN stack application on a Windows 10 environment, I encountered an issue with one of the HTTP POST requests using Mongoose (v4.4.5) to connect to MongoDB. The request successfully wrote data to MongoDB, but the response recei ...

Guide to Aligning col-md-3 to the Right Side in HTML Bootstrap 4

Is there a way to align one div on the right side, but within the same row? I am trying to place the value 172.21 on the right side of the row. Check out the screenshot here: https://i.sstatic.net/eg3K5.png Here is the HTML: <div class="row" ...

Issue with Safari not displaying properly when using float:left and float:right (works fine in Firefox and Chrome)

My website has a left side column and a right side column, which display correctly in Firefox and Chrome. However, I am experiencing issues with Safari. Any insights on what could be causing this problem and why it only occurs in Safari? View the code on ...

Tips for horizontally aligning a list with sublist using CSS

Is there a way to align a horizontal list with a sublist in HTML and CSS? I want the menu items (Home, News, etc.) to be on one line, with submenu items below them. HTML <body> <div class="menu"> <ul class="menu_l ...

Failed to fetch job details from the server at http://localhost:3000/api/jobdetail as it returned a

I'm currently working with a MongoDB database that has two collections: users and jobdetail. My goal is to establish a connection with the jobdetail collection and retrieve its data using the following app.js code: const express = require('expre ...

What is preventing the scoped styles from loading on a Nuxt page?

In my Nuxt site, I have two separate Freshsales forms as components (formA and formB). I've customized the default form styles with scoped styles within those form components. However, when I use these forms on a page, the scoped styles don't get ...

What is the best method for inserting space between two divs?

I need to create more space between the last two cards and the image at the bottom, but this spacing should increase as I resize the browser window. Can anyone help me with this? https://i.stack.imgur.com/rq9t2.png https://i.stack.imgur.com/tOikx.png Th ...

Extract the content of an element and incorporate it into a script (AddThis)

HTML: <div id="file-section"> <div class="middle"> <p class="description"> Lorem ipsum... </p> </div> </div> jQuery: var share_on_addthis = { description: $('#file-section ...

Are you searching for ways to convert an object into an array?

I have a dynamically built object and I need to extract specific fields (the dynamic ones) from it and convert them into an array. In the following code snippet, my goal is to convert towers[X] into an array of objects. {id: "", description: "Teste", tow ...

Creating an uncomplicated selector bar for a basic javascript slideshow

I've spent the last couple of hours teaching myself some basic JavaScript, so please bear with me. Currently, I have a simple code in place for a straightforward slideshow on a website. Here is the JavaScript I'm using (where the 'slide&apo ...

What are the key differences between using role and class selectors in HTML 5 and CSS3 to create two sidebars with three

My decision to eliminate all ids from my CSS and transition to HTML 5 has led me to question the use of roles in markup. In the past, IDs were used to style sidebars individually within a single selector for both. Here is an example: <div id="sidebar" ...

Customizing the size of the selectInput widget in R/Shiny

I'm trying to adjust the size of the selectInput() widget in shiny using selectize.js. I've attempted to tweak various attributes listed on this page (https://github.com/selectize/selectize.js/blob/master/dist/css/selectize.css) but I can't ...

Elevate a division within its container

Is there a way to make a smaller div positioned at the top when it's placed next to a larger one in the same container? I'm looking for a solution that doesn't involve using position-relative and manually adjusting the placement. Take a loo ...

Is there a way to launch a different application using intents within Ionic development?

I attempted the following code but it seems to have no effect <ion-header-bar class="bar-dark"> <h1 class="title title-left">App Title</h1> <a class="button rate-me button-light" href="amzn://apps/here">Rate</a> </ ...

Angular: Keeping all FormControls in sync with model updates

I am dealing with a collection of FormControls that were created using FormBuilder: this.someForm = this.fb.group({ name: '', size: '', price: '', }); Is there an alternative method to update them based on ...

"the content does not occupy the entire space of the parent

My TableRow expands across the width of the screen, and within it is a ListView with a layout_width set to match_parent. However, the ListView does not expand properly. This issue arose when I added two buttons in the next TableRow. Even after setting the ...