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.