嘿。
我的聊天机器人托管在网站 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。
并且,只是为了展示它的工作原理:
注意:发送失败只是因为它遇到了一个断点,而我花了太长时间才取消暂停
关于c# - 从 Bot Framework 获取网页的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59827582/