Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2022
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.78 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>My Bot</title>
  5.   </head>
  6.   <body>
  7.     <div>
  8.       <div id="heading">
  9.         <!-- Change the h1 text to change the bot name -->
  10.         <div class="headingFirst">
  11.           <h1 class="chatboth1header">My Bot</h1>
  12.         </div>
  13.         <div class="headingSecond"></div>
  14.       </div>
  15.       <div id="webchat" role="main"></div>
  16.     </div>
  17.     <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
  18.     <script>
  19.       const styleSet = window.WebChat.createStyleSet({
  20.         // Add styleOptions to customize Web Chat canvas
  21.         bubbleBackground: "rgba(0, 0, 255, .1)",
  22.         bubbleFromUserBackground: "rgba(0, 255, 0, .1)",
  23.         rootHeight: "100%",
  24.         rootWidth: "100%",
  25.         backgroundColor: "white",
  26.         hideUploadButton: false,
  27.         userAvatarBackgroundColor: "white",
  28.         botAvatarBackgroundColor: "white",
  29.       });
  30.       // Add your BOT ID below
  31.       const BOT_ID = "my_bot_id";
  32.       const theURL =
  33.         "https://powerva.microsoft.com/api/botmanagement/v1/directline/directlinetoken?botId=" +
  34.         BOT_ID;
  35.  
  36.       const directLineTokenString = "directLineToken";
  37.  
  38.       const getDirectLineToken = async () => {
  39.         if (!sessionStorage) return undefined;
  40.  
  41.         const now = Date.now();
  42.  
  43.         let tokenItem = undefined;
  44.  
  45.         try {
  46.           tokenItem = JSON.parse(sessionStorage.getItem(directLineTokenString));
  47.         } catch (err) {
  48.           console.error("An error occurred: " + err);
  49.         }
  50.  
  51.         // If there is no token in storage or the token has expired
  52.         if (!tokenItem || (tokenItem && now > tokenItem.expiration)) {
  53.          try {
  54.            const res = await fetch(theURL);
  55.             const result = await res.json();
  56.             const tokenStorageItem = {
  57.               token: result.token,
  58.               expiration: now + result.expires_in * 1000,
  59.             };
  60.             sessionStorage.setItem(
  61.               directLineTokenString,
  62.               JSON.stringify(tokenStorageItem)
  63.             );
  64.             return result.token;
  65.           } catch (err) {
  66.             sessionStorage.removeItem(directLineTokenString);
  67.             console.error("An error occurred: " + err);
  68.             return undefined;
  69.           }
  70.         } else {
  71.           return tokenItem.token;
  72.         }
  73.       };
  74.  
  75.       (async function () {
  76.         const token = await getDirectLineToken();
  77.         window.WebChat.renderWebChat(
  78.           {
  79.             directLine: window.WebChat.createDirectLine({ token }),
  80.             styleSet
  81.           },
  82.           document.getElementById("webchat")
  83.         );
  84.       })().catch(async (err) => {
  85.         console.error("An error occurred: " + err);
  86.       });
  87.     </script>
  88.   </body>
  89. </html>
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement