Search code examples
javascriptbotframeworkchatbotweb-chat

Greeting message is not showing up until a message is triggered to the bot


I am trying to send a greeting message and ask the user's name. The bot is trying to ask some questions from the user and then help him with a redirect link. It works perfect in the emulator or Microsoft online test tool but the greeting message is not showing up once the bot is integrated inside an ASP.NET Application.

I have searched and found a couple of useful links but I am not still able to fix the issue.

I have tried two different methods, iframes and window.WebChat.renderWebChat but the issue happens in both cases the greeting message is not shown until I send a message to the bot.

As have been discussed in other posts, all the solutions are ending to this topic,so the chat bot integration must be implemented in javascript or Node; And, a custom event need to be sent (as a trigger) to the chatbot. However, while my bot has been deployed on a personal windows server, I do not know how to configure it and set on the directLine parameter.

As have been posted here the probable solution is as the following codes but I can't configure and set the required parameters to fetch the token.

Messaging endpoint: https://xxx.yyy.com/api/messages

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>Web Chat: Send welcome event</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script crossorigin="anonymous" src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
    <style>
      html,
      body {
        height: 100%;
      }

      body {
        margin: 0;
      }

      #webchat {
        height: 100%;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="webchat"></div>
    <script>
      (async function() {

        //I have no idea how this line of code must be set to achieve the token
        //------------------------------------------------------
        const res = await fetch('https://xxx.yyy.com/directline/token', { method: 'POST' });
        const { token } = await res.json();
        //------------------------------------------------------


        const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
          if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
            dispatch({
              type: 'WEB_CHAT/SEND_EVENT',
              payload: {
                name: 'webchat/join',
                value: { language: window.navigator.language }
              }
            });
          }

          return next(action);
        });

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

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

Finally, I have to emphasise that the Bot is working well on the online testing tool and the emulator but the error happens in integration in the asp.net application.

Screenshot: the photos on the left are from the website application, the right photo is from the test environment: enter image description here

Edit (1) : Title updated , screen shot added.


Solution

  • The code below fixed the issue of token generation and sending an event to trigger the chatbot inside a web application.

    $.ajax({
                url: 'https://directline.botframework.com/v3/directline/tokens/generate',
                method: "POST",
                headers: {
                    "Authorization": "Bearer SECRET GOES HERE"
                },
            }).then(function (response) {
                const { token } = JSON.parse(JSON.stringify(response));
                const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
                    if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
                        dispatch({
                            type: 'WEB_CHAT/SEND_EVENT',
                            payload: {
                                name: 'webchat/join',
                                value: { language: window.navigator.language }
                            }
                        });
                    }
    
                    return next(action);
                });
    
                window.WebChat.renderWebChat(
                    {
                        directLine: window.WebChat.createDirectLine({ token }),
                        store
                    },
                    document.getElementById('chatBotFrame')
                );
    
                document.querySelector('#chatBotFrame > *').focus();
            }).catch(function (err) {
                console.error(err);
            });