Search code examples
c#httpxmlhttprequestbotframework

Get URL Referer and Origin header from Microsoft Bot Framework


I am using a directline inside my website, I was wondering if there is anyway to tell the URL of the website from the Request header Referrer and Origin, I want to get the value inside a Dialog, I have tried using Activity.ServiceUrl but it is giving directline.botframework.com and the HttpContext.Current.Request.Url.AbsoluteUri is giving the Azure URL.

   public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);
        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;                      }

Solution

  • If you’d like to get the URL of the webpage where you embed webchat inside bot application Dialog, you can try to get the URL and pass it to your bot, like this:

    <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>
    

    Inside bot dialog:

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

    Test result:

    enter image description here