0

I am using Chat Engine io if anyone is familiar with it. It is an API for chat rooms that you can implement into your application. I created an application that contains a support chat where the admin can chat with the email input user.

I have tried every possible way, read through documentation and still can not assign the admin to the already created user which is the admin.

createChat.js

const createChat = (email, callBack) => {
  axios
    .put(
      "https://api.chatengine.io/chats/",
      {
        "usernames": ["Tim", email],
        "is_direct_chat": true,
      },
      { headers: { "Private-Key": process.env.REACT_APP_CE_PRIVATE_KEY } }
    )
    .then((r) => callBack(r.data))
    .catch((error) => {
      console.error("there was an error", error);
    });
    
};

createUser.js

const createUser = (email, callBack) => {
  axios
    .put(
      "https://api.chatengine.io/users/",
      {
        "username": email,
        "secret": email,
        "email": email,
      },
      { headers: { "Private-Key": process.env.REACT_APP_CE_PRIVATE_KEY } }
    )
    .then((r) => callBack(r.data))
    .catch(error => {
        console.error('there was an error', error)
    })
};

handleSubmit function that is inside SupportWindow.js component

  const [email, setEmail] = useState("");

  const handleSubmit = () => {
    createUser(email,
      user => {
        createChat(email,
          chat => console.log('success', chat)
        )
      }
    )
  };

This is the console for the response returned...

{id: 111480, admin: {…}, people: Array(2), attachments: Array(0), last_message: {…}, …}
access_key: "*****************************"
admin: {username: 'e@e.com', first_name: '', last_name: '', avatar: null, custom_json: '{}', …}
attachments: []
created: "2022-04-14T23:36:49.934306Z"
custom_json: "{}"
id: 111480
is_authenticated: true
is_direct_chat: true
last_message: {created: '', attachments: Array(0), sender_username: '', text: '', custom_json: ''}
people: (2) [{…}, {…}]
title: null

The admin username SHOULD BE "Tim", as declared in the createChat.js function. Why is the admin username always the entered email address???

This is the instructed way of setting up your PUT call via axios as per docs.

--data-raw '{
    "usernames": ["adam_la_morre", "bob_baker", "wendy_walker"],
    "title": "Another Surprise Party!",
    "is_direct_chat": false
}'

RESULT:

{
  "id": 38702,
  "admin": {
    "username": "adam_la_morre",
    "first_name": "Adam",
    "last_name": "La Morre",
    "avatar": null,
    "custom_json": {
      "custom_json": 2001
    },
    "is_online": false
  },

The admin is the first index in the array. Just like mine.

TimMTech93
  • 9
  • 1
  • 4

2 Answers2

0

In your createUser you are setting username to email. You'll need to pass the name in if you want to set username to it.

  const handleSubmit = () => {
    createUser(email, userName,
      user => {
        createChat(email,
          chat => console.log('success', chat)
        )
      }
    )
  };

Then in your createUser:

const createUser = (email, userName, callBack) => {
  axios
    .put(
      "https://api.chatengine.io/users/",
      {
        "username": userName,
        "secret": email,
        "email": email,
      },
      { headers: { "Private-Key": process.env.REACT_APP_CE_PRIVATE_KEY } }
    )
    .then((r) => callBack(r.data))
    .catch(error => {
        console.error('there was an error', error)
    })
};

Also, you are setting secret to email.

Fred
  • 193
  • 8
  • The username is already created on the website. The chat room should have the user "Tim" which is already created on the site, as well as the person using the support chat on the actual application when entering their email. the createUser function is for the email input. The "admin" is already created via ChatEngine.io site. – TimMTech93 Apr 15 '22 at 04:37
  • Will the api allow you to send the object with just email (without username and secret)? – Fred Apr 15 '22 at 13:25
  • I think so. Reason for all three is so the user’s email input can be saved as all three, username, email and secret. – TimMTech93 Apr 15 '22 at 18:21
0

I found this solution. "usernames": ["adam_la_morre", "bob_baker",] the first index must start alphabet A, eg(aaa01). if the admin username is username less than the username will change the admin username (eg "usernames": ["bob_baker", "adam_la_morre",]. adam_la_morre becomes the admin username.