Search code examples
asp.net-mvcbotframeworkazure-cognitive-servicesdirect-line-botframework

Enable Directline in MS BOT Framework


1) I created a REST API for my client application and Registered in API Management (that used to get user specific data)

2) I created a BOT WEB APP in Azure portal using LUIS and it is working fine (I added sample Intents/Utterance)

3) Consumed the REST API in LUIS application as one of the Intent condition Tested the above in BOT Emulator and Azure portal as well. (Hard-coded the user info in the calling function)

Now I want to configure the BOT in my MVC client application and pass the user specific info based on the logged in user info. I read the below article to enable the directline API. Still looking for the better resources.

Connect BOT using Directline API

DirectLine Auth


Solution

  • Now I want to configure the BOT in my MVC client application and pass the user specific info based on the logged in user info.

    I’m using the following code sample to embed bot in my website and pass user specific information to bot, which works for me, you can refer to it.

    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Test</title>
        <link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
        <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
    </head>
    <body>
        <div> 
            <div id="mybot"/>
        </div>
    </body>
    </html>
    <script>
        BotChat.App({
            directLine: { secret: '{your_directline_secret}' },
            //you can dynamically retrieve the logged in user info in your mvc View
            //and pass thoes info to your bot
            user: { id: '1234', firstname: '{your_fname}', lastname: '{your_lname}'},
            bot: { id: '{your_botid}' },
            resize: 'detect'
        }, document.getElementById("mybot"))
    </script>
    

    Test result:

    enter image description here

    Using the following code snippet to extract user information from Activity object in bot app.

    if (activity.From.Properties["firstname"] != null)
    {
        var fname = activity.From.Properties["firstname"].ToString();
    }