Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
constant msgerForm = get(".msger-inputarea");
constant msgerInput = get(".msger-input");
constant msgerChat = get(".msger-chat");

constant BOT_MSGS = [

constant BOT_NAME = "BOT";
constant PERSON_NAME = "John";

msgerForm.addEventListener("submit", event => {

  constant msgText = msgerInput.value;
  if (!msgText) return;

  appendMessage(PERSON_NAME, PERSON_IMG, "right", msgText);
  msgerInput.value = "";


  msgerChat.insertAdjacentHTML("beforeend", msgHTML);
  msgerChat.scrollTop += 500;

function botResponse() {
  constant r = random(0, BOT_MSGS.length - 1);
  constant msgText = BOT_MSGS[r];
  constant delay = msgText.split(" ").length * 100;

  setTimeout(() => {
    appendMessage(BOT_NAME, BOT_IMG, "left", msgText);
  }, delay);

// Utils
function get(selector, root = document) {
  return root.querySelector(selector);

function formatDate(date) {
  constant h = "0" + date.getHours();
  constant m = "0" + date.getMinutes();

  return `${h.slice(-2)}:${m.slice(-2)}`;

function random(min, max) {
  return Math.floor(Math.random() * (max - min) + min);

What I have tried:

I am trying to message chat box but I have failed.
Richard Deeming 4-Mar-24 4:17am    
"I have failed" is just as useless as "it doesn't work".

You need to explain precisely what the problem is, how you have tried to debug or fix it, and where you are stuck.

Just dumping 53 lines of unexplained code and saying "I have failed" is not a question anyone can answer.

1 solution

You haven't shown anything in your code that relates to chatbot functionality. Let's assume that you know that you need both a client and a server in this scenario, then you need to think about how you want to handle messaging. The first thing that you need to consider that each user in the system must have a unique identifier of one form or another. The reason that this is important is because you are going to want to send messages to someone who is uniquely identifiable. Let's imagine that our site has 5 users identifed somewhat originally as A, B, C, D, and E, and these users are all online at the same time.

Now user A wants to send a message to user B. From the client, he sends something to indicate that he wants to send this chunk of text to user B. The server, on receiving this, forwards this to user B's connection. B receives the message and replies back to A; the same client/server process happens in reverse.

A knows B, D, and E, but doesn't know C. A wants to send a message to all the people they know. Theoretically, the same process as above could happen, just sending the server message on to multiple people. Alternatively, these could all be inside a group and A sends the message to the group. Internally, when the server receives the request, it will forward the messages on to individual connections but, for convenience sake, it will do this hidden under the covers as a message to a group.

What you see here is that the important part of the process is the interaction between the client and the server, and the connections it keeps open to know who is online at any point. Theoretically, you could manage all of this yourself so, when a user logs in, they register a connection at the server which is kept open for the duration of their client session. This is easy enough to do conceptually but making this a robust approach takes a lot of hard work.

If I were doing this entirely in JavaScript, I would use[^] and use that to manage the end to end communications.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900