Closed jackzro closed 2 years ago
@jackzro While I don't know why this isn't working for you, I need to warn you that you are not using the API correctly here and you are putting your Twilio account at risk. If this is code you are running within your React client-side application then you are potentially exposing your Account SID and Auth Token because they are available in the front-end. A malicious user could steal your account credentials and abuse your account with them
If you are making requests to a Twilio API, then you should proxy them through your own server (much like I do in this blog post on how to send an SMS message in React).
However, I also note that you are trying to invite an identity to a chat channel and this can be done with the Twilio Chat SDK by calling channel.invite(identity)
. Because the Chat client is authenticated with a JWT, this is a secure way of accessing the Chat service.
Let me know if this helps at all.
hey @philnash i read your article about chat programming i learnt from your article. i just want you know it really help me and really appreciate your effort and your reply man, https://www.twilio.com/blog/chat-app-twilio-kendoreact i read this and want to try private chat(1:1 chat) but i have already use SDK channel.invite(identity) but this way dont work from me so i try use REST API. in your tutorial you mix your server on react project so i just try from your tutorial, i better make my new RESTAPI that contain twilio's API?? or any idea you suggest??? i still learn about security about web proramming :)
Hey @jackzro, when you say that using the SDK to invite a user to a channel didn't work for you, what do you mean? Can you describe what you did and the error you got? Using the SDK for this is the right way to go, so we should make sure you can continue this way. If you can share some code and the error message I can hopefully help.
hey @philnash, i change a little from your code and i got this error SessionError: User already invited, but i check via postman to check member of the channel it still empty, can you help me what wrong with my code ?
const setupChatClient = (client) => {
setClient(client);
client
.getChannelByUniqueName("dave")
.then((channel) => {
console.log("first", channel);
return channel.invite(username).catch((err) => {
console.log(err);
});
})
.catch((error) => {
console.log(error.body.code);
if (error.body.code === 50300) {
return client.createChannel({
uniqueName: "msw",
friendlyName: "msw-global",
isPrivate: true,
});
}
// else if (error.body.code === 50400) {
// console.log("LOL", error.body);
// channels.on("channelInvited", function (channel) {
// return channel;
// });
// } else if (error.body.code === 54007) {
// console.log("masuk invite");
// }
else {
handleError(error);
}
})
.then(async (channel) => {
console.log(channel);
const member = await channel.getMembers();
console.log(member);
// setChannel(channel);
// setIsLoading(false);
// channel.getMessages().then(messagesLoaded);
// channel.on("messageAdded", messageAdded);
})
// .then((data) => {
// console.log(data);
// })
.catch((err) => console.log(err));
};
you can check the code https://github.com/jackzro/twilioChat
Invited user needs to accept the invitation to be added in the channel.
So if you invited a user and he did not accept the invitation yet, he is not in the channel and the channel's member list will be empty.
If you want to add user unconditionally, then use channel.add(user);
JS docs are a bit lacking on this, but see explanation for the Android SDK Channel#join method for more details.
axios .post( "https://chat.twilio.com/v2/Services/ISxxx/Channels/CHcxxx/Invites", { Identity: "jack", }, { auth: { username: "AXXXX", password: "8XXX", }, // headers: { // "Content-Type": "application/x-www-form-urlencoded", // }, } ) .then(({ data }) => { console.log(data); }) .catch((err) => console.log(err));
i tried to hit api from my react project but what i got is {"code": 20001, "message": "Missing required parameter Identity in the post body", "more_info": "https://www.twilio.com/docs/errors/20001", "status": 400} i have already put Identity on my axios req but still got this error and i tried with postman it works. Please Someone can help me ?? what is the problem ??