https://discord.gg/botpress logo
How to resume a conversation with useWebchat
# 🤝help
e
When I render the useWebchat hook on my page passing it the userId and the userToken, I get a 400 response from https://webchat.botpress.cloud//conversations and I get {message: "must have required property 'x-user-key'"} const { client, messages, isTyping, user, clientState, newConversation } = useWebchat({ clientId: "", user: { id: botpressUser.userId, token: botpressUser.userToken, }, })
l
You might have the Chat integration configured for manual authentication, and the useWebchat hook is somehow attempting to use the Chat API in a way that requires this header. or There might be a misunderstanding of how useWebchat authenticates versus how the Chat API authenticates.
e
The documentation you sent me was helpful. But if I create an encryption key in the chat integration, can I use it with the useWebchat hook?
l
useWebchat shouldn't need to handle x-user key at all. Trying to give some more info to the devs, can you share as much details as possible that would help the case out?
e
I have a page built with React that uses a BotPress bot. I manually built the webchat and use the useWebchat hook, which is initialized with each render. The problem is that I use users on my page, and I want each user to have their own chat and, every time they log in, they can pick up the conversation where they left off. To do this, I tried saving the user ID and user token generated when starting a conversation with the bot and then sending it in the useWebchat. const { client, messages, isTyping, user, clientState, newConversation } = useWebchat({ clientId: "", user: { id: botpressUser.userId, token: botpressUser.userToken, }, }) But it seems to make a request to https://webchat.botpress.cloud//conversations and receive a 400 response with the payload that says {message: "must have required property 'x-user-key'"}
l
Will get back when I get more insight!
e
Thank you! I'll be waiting.
l
Waiting for a dev to get back! No exact ETA but will respond when I can~ (it is approaching the EOD so it is possible the answer only comes in tomorrow)
He did say your code seems to be good
this is the payload for user : { userId: string; userToken: string; }; notice the 'user' prefix could that be it ?
do let me know when you can @early-state-34882
e
Yes, I can update it. I'll make sure to use userId and userToken in the user object instead of id and token.
I'll keep you posted if it works for me.
l
Try it out and let me know, ill update the devs right away
e
Now I tried it like this const { client, messages, isTyping, user, clientState, newConversation } = useWebchat({ clientId: "", user: { userId: botpressUser.userId, userToken: botpressUser.userToken, }, }) and get response 401 { "id": "err_20251022205246x369ECFB1", "code": 401, "type": "Unauthorized", "message": "Invalid user key" }
What is the user key should I send?
w
you have to sign the user id into a JWT token (if you're signing it yourself)
using the same secret key you set in the chat-api integration (if it's the one you're using)
e
Good morning! I tried what you told me, but I still get the same response 401. { "id": "err_20251023123933x72A6F6BF", "code": 401, "type": "Unauthorized", "message": "Invalid user key" } const YOUR_USER_ID = userId const YOUR_ENCRYPTION_KEY =
${process.env.BOTPRESS_ENCRYPTION_KEY}
const xUserKey = jwt.sign({ id: YOUR_USER_ID }, YOUR_ENCRYPTION_KEY, { algorithm: 'HS256' }) const { client, messages, isTyping, user, clientState, newConversation } = useWebchat({ clientId: "", user: { userId: botpressUser.userId, userToken: botpressUser.xUserKey, }, })
w
can you check if it's being signed correctly?
l
@early-state-34882 Can you try including conversation ID aswell?
e
Where do I put it?
w
you might be missing a few claims
on the payload
namely exp and iat
l
const { user, client, clientState, messages, isTyping, participants, } = useWebchat({ clientId: some-client-id, conversationId: some-conversation-id, user: { useId: some-user-id, userToken: some-user-token } })
@early-state-34882
Let me know any updates when you can 🫡
e
Ok! I'll try it that way and update if it works
That works!!
I think it didn't work before because I was trying to enter a new conversationId, but when I passed it as a parameter it returned the correct conversation.
@loud-crowd-60573 @wide-umbrella-5226 Thank you very much for your help 🙌
l
Amazing news!!!
7 Views