c# - 从 Bot Framework 获取网页的 URL

标签 c# azure botframework azure-bot-service

嘿。

我的聊天机器人托管在网站 http://www.abs.com

我希望我可以在“hello world”中获取网址:有一个“Hello world www.abc.com” 我尝试使用“HttpContext.Current.Request.Url.AbsoluteUri”和“activity.ServiceUrl”方法,但没有成功。

在第二步中,我尝试了一种新方法,将代码放入我的 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>

在我的 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);
  }

但它不起作用。大家有什么想法吗?

PS:我使用 C# 作为编程语言和 SDK 3.0。

最佳答案

不幸的是,ServiceUrl 只能获取 channel 的 URL。如果您想要用户所在网站的实际 URL,我建议使用类似 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
    );
    }

一些注意事项:

  • 此代码和示例适用于 Web Chat v4。你的代码使用 v3.我推荐使用v4;如果您没有太多自定义功能,迁移非常容易。
  • 该示例有点难以理解,以下是摘要:
<小时/>
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
    if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {

此部分允许您编辑传出事件

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

这部分进行实际编辑

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

这表示您要将 Activity.ChannelData.Url 设置为 window.location.href,它提供用户当前页面的 URL。

<小时/>

并且,只是为了展示它的工作原理:

enter image description here

enter image description here

注意:发送失败只是因为它遇到了一个断点,而我花了太长时间才取消暂停

enter image description here

关于c# - 从 Bot Framework 获取网页的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59827582/

相关文章:

youtube - 如何在MS团队中显示媒体内容

c# - 以编程方式更改 AssemblyVersion 和 AssemblyFileVersion 属性

c# - 在 Unity 中使用 JSON 保存游戏

node.js - 机器人框架 : is it possible to combine LUIS intents and normal intents?

azure - 什么是 "Deployment ID"和 "Deployment unique name"

azure - Azure 门户和 App Insights Analytics 中显示的不同用户数据

node.js - 欢迎留言 Bot Framework v4 Nodejs

c# - 带有 angularjs 的 $http 请求正在返回我的 html 文档

c# - 以编程方式将相机置于 unity3d 中的对象上

windows - 启动Azure VM并自动登录Windows 10