Meed095
Meed095

Reputation: 63

Get URL of web page from Bot Framework

Hy.

My chatbot is hosted on a website http://www.abs.com

I wish I could in a "hello world" get the url: to have a “Hello world www.abc.com " I tried to use the 'HttpContext.Current.Request.Url.AbsoluteUri' and 'activity.ServiceUrl' methods but it didn't work.

In a second step i tried a new method by putting the code in my default.html

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

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

and in my BasicBotDialog.cs:

  if (activity.From.Properties["referrer"] != null)
  {
      var urlref= message.From.Properties["referrer"].ToString();
      await context.PostAsync("hy the page of the chatbot is" + urlref);
  }

but it doesn't work. Anyone have an idea.

PS: I use C# as a programming language and SDK 3.0.

Upvotes: 1

Views: 1483

Answers (2)

mdrichardson
mdrichardson

Reputation: 7241

Unfortunately, ServiceUrl is only going to get the URL of the Channel. If you want the actual URL of the site the user is on, I'd suggest something like this sample:

const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
    if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
    // The channelData submitted here is very similar to HTTP cookies and vulnerable to forgery attack.
    // Make sure you use signature to protect it and verify the signature on the bot side.

    // To minimize unexpected behaviors, we recommend to treat the "action" object as if it is immutable.
    // We use simple-update-in package to update "action" with partial deep cloning.
    action = window.simpleUpdateIn(
        action,
        ['payload', 'activity', 'channelData', 'url'],
        () => window.location.href // This is the part that gets the user's URL
    );
    }

A couple of notes:

  • This code and sample is for v4 of Web Chat. Your code there uses v3. I recommend using v4; it's very easy to migrate if you don't have a lot of customization.
  • The sample is a little hard to understand, here's a summary:

const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
    if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {

This part allows you to edit outgoing activities


action = window.simpleUpdateIn(
        action,
        ['payload', 'activity', 'channelData', 'url'],
        () => window.location.href // This is the part that gets the user's URL
    );

This part makes the actual edit


['payload', 'activity', 'channelData', 'url'],
        () => window.location.href

This says you want to set Activity.ChannelData.Url to window.location.href, which gives the user's current page URL.


And, just to show it working:

enter image description here

enter image description here

Note: The send only failed because it hit a breakpoint that I took too long to unpause

enter image description here

Upvotes: 1

mmking
mmking

Reputation: 1575

I'm understanding your question as you have a bot hosted on some website and would like the bot to respond with the url of the page they're on.

You can get that from ITurnContext.Activity.ServiceUrl if you're handling it from the ActivityHandler or from DialogContext.Context.Activity.ServiceUrl if handling from Dialog.

Upvotes: 0

Related Questions