Calvin1103
Calvin1103

Reputation: 15

MS Bot get current page url

I have created a bot with MS Bot SDK. Then, I want to get the page URL where I'm hosting the bot. I just inject the script to a page to host the bot. But, does anyone who knows how to get the current page URL from C#?

I can see someone is trying to use Activity for getting the URL, but I can't find the right property from Activity.

Upvotes: 1

Views: 432

Answers (2)

Eric Dahlvang
Eric Dahlvang

Reputation: 8292

ChannelData was designed to enable sending custom information from client to bot, and back. Similar to Fei Han's answer, you can intercept outgoing messages and provide custom ChannelData for every activity sent.

<script>

    var dl = new BotChat.DirectLine({
        secret: 'yourdlsecret',           
        webSocket: false,
        pollingInterval: 1000,
    });

    var urlref = window.location.href;

    BotChat.App({
        botConnection: {
            ...dl,
            postActivity: activity => dl.postActivity({
                ...activity,
                channelData: { pageurl: urlref }
            })
        },
        user: { id: 'userid' },
        bot: { id: 'botid' },
        resize: 'detect'
    }, document.getElementById("bot"));
</script>

Then, in the bot:

enter image description here

Upvotes: 1

Fei Han
Fei Han

Reputation: 27805

I just inject the script to a page to host the bot. But, does anyone who knows how to get the current page URL from C#?

If you embed webchat in your web site and you want to get the URL of the web page where you embed the webchat, you can try the following approach to get the URL and pass it to your bot.

Pass the URL to bot:

<script>
    var urlref = window.location.href;

    BotChat.App({
        directLine: { secret: "{directline_secret}" },
        user: { id: 'You', pageurl: urlref},
        bot: { id: '{bot_id}' },
        resize: 'detect'
    }, document.getElementById("bot"));
</script>

Retrieve the URL in bot application:

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

Upvotes: 2

Related Questions