Token Generation for Botframework Webchat

Amintas Lopes Neto

I've been migrating my Direct Line Bot from Webchat v3 to v4. The new version demands the use of tokens rather than the Direct Line secret in the calling page. Here is the code (index.html) used to start the bot:

<!DOCTYPE html>
<html lang="en-US">
<head>
    <title>Web Chat: Full-featured bundle</title>

    <script src="https://cdn.botframework.com/botframework-webchat/master/webchat.js"></script>

    <style>
         html, body {
            height: 100%
        }

         body {
            margin: 0
        }

         #webchat,
         #webchat > * {
             height: 100%;
             width: 100%;

        }


    </style>
</head>
<body>
       <div id="webchat" role="main"></div>

    <script>
    (async function () {
        const res = await fetch('https://bellamspt.azurewebsites.net/Forms/Webchat/directline/token', { method: 'POST' });

      const { token } = await res.json();

      window.WebChat.renderWebChat({
        directLine: window.WebChat.createDirectLine({ token })
      }, document.getElementById('webchat'));

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

Question: What code do I need to write to generate the token in other to be called by https://bellamspt.azurewebsites.net/Forms/Webchat/directline/token ?? Realize it's got to be something like

POST https://directline.botframework.com/v3/directline/tokens/generate
Authorization: Bearer SECRET

but I don't know if it's got to be a php, js or other type of file to work.

Thanks in advance

Hessel

I used php to solve this. You could give it a try.

<?php
$botSecret = '<your secret>';
$response = wp_remote_get( 'https://webchat.botframework.com/api/tokens', array( 'headers' => 'Authorization: BotConnector ' . $botSecret ) );
if( is_array($response) ) {
  $header = $response['headers'];
  $token = $response['body'];
}
?>
<script type="text/javascript">
        var webChatToken = <?php echo $token; ?>;
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MS BotFramework Temporary token issue in WebChat URL

Start dialogue in botframework webchat

BotBuilder-Samples are missing token server required by BotFramework-WebChat Samples?

BotFramework-WebChat - Adaptive Card

Auto completion with the BotFramework's WebChat

BotFramework-Webchat Middleware for renderMarkdown

Microsoft BotFramework-WebChat scrolling issues

Issues with suggestion list in botframework Webchat React

Sending Message from Botframework to Azure Test WebChat

Send messages with https://webchat.botframework.com/

How to fix token issue with webchat

WebpackError: TypeError: Object(...) is not a function gatsbyjs build error (botframework-webchat)

Azure BotFramework-WebChat javascript error initializing DirectLine

BotFramework-Webchat Middleware: set css class names

Botframework-WebChat - Why doesn't it work with Internet Explorer?

BotFramework WebChat - Sample #14 - Middleware for both Redux stores?

Webchat Value Exchanging Issue on Botframework V4

How to Start a Bot with the Microphone On on Botframework V4 and Webchat

Method to selectively display IM messages in webchat with BotFramework v4?

How to have new version of botframework webchat remember user ID

BotFramework Webchat v4 Sending message back through the javascript

botframework v4 customize title and minimizable webchat

Firebase token generation iOS

Laravel token generation

GET or POST for token generation

Botframework Create Directline get token from server

How to set the User avatar dynamically in BotFramework-WebChat based on logged in user using OAuthCard

How to create custom cards (like adaptive cards) in botframework webchat using React.js?

How to intercept message from Chatbot in a React component ? (Microsoft botframework-webchat)