问题
I saved the token from createdirectline first time it was initialized to the server. But whenever I get the token from the server and used it, it will not use that token instead it will create a new one.
React.useEffect(() => {
const temp = async () => {
if (Object.entries(directline).length === 0) {
if(await props.chatBotData!.tokenFromDb){
setDirectline(createDirectLine({ token: props.chatBotData.tokenFromDb.toString() }));
}else {
setDirectline(createDirectLine({ token: DIRECTLINE_SECRET }));
}
}
};
temp();
}, [directline,props.chatBotData]);
const [directline, setDirectline]: any = useState({});
回答1:
You want to save the Direct Line secret server-side so it is inaccessible, not the token. The secret is used when making a call to /directline/tokens/generate or /directline/conversations in order to retrieve a token (and conversationId, if the latter endpoint). (Reference Connect a bot to Direct Line and Generate a Direct Line token.)
In short, this is the flow your site should follow:
- Web site with Web Chat loads.
- As Web Chat instantiates, an API call is made to your "token" server (i.e.
https://.../gettoken. - When the "token" server token API is called, it makes a call to
/directline/tokens/generatepassing thesecretwhich then returns thetoken. - The token is returned back to your calling web site which then passes it for use in
createDirectLine().
It's important to note that tokens are only good for 30 mins, at the time of this writing, before they expire. If you are expecting a conversation to last longer than 30 mins, then you will need to perform a similar process to exchange the token for a refreshed token beforehand.
Hope of help!
来源:https://stackoverflow.com/questions/63823755/botframework-create-directline-get-token-from-server