我正在尝试使用 Microsoft 的机器人框架实现一个机器人。我按照文档进行操作,但似乎没有正确添加按钮。在 Skype 上,它显示一种缩略图,可打开 Skype 下载页面。在 Facebook Messenger 上它没有显示任何内容。
有什么建议吗? 谢谢!
最佳答案
是的,您可以实现添加一个自定义按钮,该按钮将适用于机器人框架的每个 channel 。
有两种方法可以实现: 1)使用Bot的卡片功能获取按钮。 (快捷方便) 2) 使用直线 channel 并添加您的自定义按钮。 (复杂)
让我们从方法 1 开始
您需要创建一张英雄卡并在对话中调用它。英雄卡包含来自 bot 的文本响应和缩略图图像 url。 (如果不需要,您可以删除缩略图)。以下是为您提供帮助的运行代码。
public async Task StartAsync(IDialogContext context)
{
context.Wait(ImgCardResponse);
}
private async Task ImgCardResponse(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var message = await argument;
//responseMsgOnly is used to pass bot reply message
//responseImage is used to pass thumbnail image
var attachment = BotButtonCard(responseMsgOnly, responseImage);
cardMsg.Attachments.Add(attachment);
await context.PostAsync(cardMsg);
}
private static Attachment BotButtonCard(string responseMsgOnly, string responseImage)
{
var heroCard = new HeroCard
{
Title = "Here we go with the response",
Subtitle = "Subtitle goes here",
Text = responseMsgOnly,
Images = new List<CardImage>
{
new CardImage(responseImage)
}
Buttons = new List<CardAction>
{
new CardAction(ActionTypes.OpenUrl, "Your Button Label", value: "https://www.google.com")
}
};
return heroCard.ToAttachment();
}
private async Task ResumeAfterAction(IDialogContext context, IAwaitable<object> result)
{
context.Done(new object());
}
让我们从方法 2 开始
Direct Line API 是一个简单的 REST API,用于直接连接到单个机器人。此 API 旨在供开发人员编写自己的客户端应用程序、Web 聊天控件或将与其机器人对话的移动应用程序。 Direct Line v3.0 Nuget 包简化了对底层 REST API 的访问。
你需要创建一个html页面并放入下面的代码
在头部
<link href="../botchat.css" rel="stylesheet"/>
<script src="../js/botchat.js"></script>
body 部位
<div id="bot"></div>
<script>
var FirstName;
var emailaddress;
var Environment;
try{
FirstName = _spPageContextInfo.userDisplayName;
emailaddress = "manoj.resources@resources.in";
Environment= _spPageContextInfo.webAbsoluteUrl ;
}
catch(ex)
{
spCOntext = 'You';
Environment = 'Local System';
}
BotChat.App({
directLine: { secret: 'Your direct line secret' },
user: { id: FirstName,Name: Environment},
name: spCOntext,
value: FirstName,
From: '',
bot: { id: 'Bot ID' },
resize: 'detect'
}, document.getElementById("bot"));
</script>
如果你需要任何帮助,请告诉我
关于c# - 向机器人框架应用程序添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38697731/