跳转到主要内容

热门内容

今日:


总体:


最近浏览:


Chinese, Simplified

category

Description

A simple web page with Web Chat connected to a bot via Direct Line App Service Extension protocol.

Test out the hosted sample

How to run

  • Fork this repository
  • Navigate to /Your-Local-WebChat/samples/01.getting-started/i.protocol-direct-line-app-service-extension in command line
  • Run npx serve
  • Browse to http://localhost:5000/

Things to try out

  • Type hello: you should be able to type to the bot and receive a response in plain text

Code

Jump to completed code to see the end-result index.html.

Getting started

Goals of this bot

This code features the Direct Line App Service Extension protocol (Direct Line ASE). Additional steps required to set up a bot to use Direct Line App Service Extension can be found in the DL ASE documentation.

The index.html page has the following main goals:

This sample starts with the full-bundle CDN sample as the base template.

Obtain a Direct Line ASE-specific token

We are updating the endpoint of our token server to retrieve a Direct Line ASE-specific token.

- const res = await fetch('https://webchat-mockbot.azurewebsites.net/directline/token', { method: 'POST' });
+ const res = await fetch('https://webchat-mockbot2.azurewebsites.net/api/token/directlinease', { method: 'POST' });

The token is a JSON Web Token and the iss and aud fields are both https://directlineextension.botframework.com/.

Render using the Direct Line ASE chat adapter

Create a Direct Line ASE chat adapter with the ASE-specific token and bot endpoint. Our MockBot is hosted on https://webchat-mockbot2.azurewebsites.net/, the bot endpoint is https://webchat-mockbot2.azurewebsites.net/.bot/v3/directline.

  …
  window.WebChat.renderWebChat(
    {
-     directLine: window.WebChat.createDirectLine({ token })
+     directLine: await window.WebChat.createDirectLineAppServiceExtension({
+       domain: 'https://webchat-mockbot2.azurewebsites.net/.bot/v3/directline',
+       token
+     })
    },
    document.getElementById('webchat')
  );
  …

Completed code

Here is the finished index.html:

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>Web Chat: Full-featured bundle with Direct Line Speech channel</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script
      crossorigin="anonymous"
      src="https://cdn.botframework.com/botframework-webchat/latest/webchat-minimal.js"
    ></script>
    <style>
      html,
      body {
        background-color: #f7f7f7;
        height: 100%;
      }

      body {
        margin: 0;
      }

      #webchat {
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
        height: 100%;
        margin: auto;
        max-width: 480px;
        min-width: 360px;
      }
    </style>
  </head>
  <body>
    <div id="webchat" role="main"></div>
    <script>
      (async function() {
        const res = await fetch('https://webchat-mockbot2.azurewebsites.net/api/token/directlinease', { method: 'POST' });
        const { token } = await res.json();

        window.WebChat.renderWebChat(
          {
            directLine: await window.WebChat.createDirectLineAppServiceExtension({
              domain: 'https://webchat-mockbot2.azurewebsites.net/.bot/v3/directline',
              token
            })
          },
          document.getElementById('webchat')
        );

        document.querySelector('#webchat > *').focus();
      })().catch(err => console.error(err));
    </script>
  </body>
</html>

Further reading

Other CDN bundles

Check out the hosted samples and source code for other CDN bundle options below.

Full list of Web Chat hosted samples

View the list of available Web Chat samples

本文地址
最后修改
星期四, 八月 8, 2024 - 20:02
Article