Bot Framework:Activity类简明指南
Bot Framework相关文档:https://docs.botframework.com/en-us/csharp/builder/sdkreference/attachments.html
Bot Framework提供的回复样式不仅仅是语言框。如果我们写下这样的代码:
Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
await connector.Conversations.ReplyToActivityAsync(reply);
(本代码来自官方文档)
我们注意到这里的Activity类(https://docs.botframework.com/en-us/csharp/builder/sdkreference/activities.html)的属性没有被妥善设置,所以使用了默认值。而在实际情况中,Activity类通常可以被设置各种各样的属性。
public class Activity : IActivity, IContactRelationUpdateActivity, IMessageActivity, ITypingActivity, IConversationUpdateActivity, IActionActivity
{
/// <summary>Initializes a new instance of the Activity class.</summary>
public Activity();
/// <summary>Initializes a new instance of the Activity class.</summary>
public Activity(string type = null, string id = null, DateTime? timestamp = null, string serviceUrl = null, string channelId = null, ChannelAccount from = null, ConversationAccount conversation = null, ChannelAccount recipient = null, string textFormat = null, string attachmentLayout = null, IList<ChannelAccount> membersAdded = null, IList<ChannelAccount> membersRemoved = null, string topicName = null, bool? historyDisclosed = null, string locale = null, string text = null, string summary = null, IList<Attachment> attachments = null, IList<Entity> entities = null, object channelData = null, string action = null, string replyToId = null);
(本代码来自类定义)
首先,Activity类有一个这样的属性:ActivityType

当Type(通常)被设置为Message时,能做的事情是最多的。下面我介绍4个属性,这4个属性都是Message能完成的。
1,Text&Locale/TextFormat属性:主要是文本和区域,文本格式这几个属性。
文本和区域:
| Description | Example | |
|---|---|---|
| Text | A text payload in markdown syntax which will be rendered as appropriate on each channel | Hello, how are you? |
| Locale | The locale of the sender (if known) | en |
文本格式:
| Description | Notes | |
|---|---|---|
| plain | The text should be treated as raw text and no formatting applied at all | |
| markdown | The text should be treated as markdown formatting and rendered on the channel as appropriate | default |
| xml | The text is simple XML markup (subset of HTML) | Skype Only |
具体的规范可以自行查阅文档。
2,Attachment属性:是一系列附件对象,其中Rich Card就在这里;除此之外还可以设置多个Rich Card是以转盘的形式显示还是列表的形式。
| Description | Example | |
|---|---|---|
| ContentType | The contentType of the ContentUrl property | image/png |
| ContentUrl | A link to content of type ContentType | http://somedomain.com/cat.jpg |
| Content | An embedded object of type contentType | If contentType = "application/vnd.microsoft.hero" then Content would be a JSON object for the HeroCard |
下表是AttachmentLayout属性:
| Description | Notes | |
|---|---|---|
| list | Multiple attachments should be shown as a list | default |
| carousel | multiple attachments should be shown as a carousel if possible, else fall back to a list |
3,Entities属性:Entities属性是一系列开放式schema.org对象,允许在通道和机器人之间交换常见的上下文元数据。当设置了这样的属性,我们就可以很好地串起上下文来分析。
其中,Mention实体就可以通过设置来感知自己有没有被提起(比如说有没有人通过@机器人 的方式使用它)。
| Description | |
|---|---|
| type | type of the entity ("mention") |
| mentioned | ChannelAccount of the person or user who was mentioned |
| text | the text in the Activity.Text property which represents the mention. (this can be empty or null) |
而Place实体可以通过channels发送地址、地理信息。
The Place Object
| Property | Description |
|---|---|
| Type | 'Place' |
| Address | string description or PostalAddress (future) |
| Geo | GeoCoordinates |
| HasMap | URL to a map or complex "Map" object (future) |
| Name | Name of the place |
GeoCoordinates Object
| Property | Description |
|---|---|
| Type | 'GeoCoordinates' |
| Name | Name of the place |
| Longitude | Longitude of the location WGS 84 |
| Latitude | Latitude of the location WGS 84 |
| Elevation | Elevation of the location WGS 84 |
4,ChannelData property:可以传递额外的属性,更详细地描述每个channel启用的message。
当Type不是Message时,将会有其他的对应措施。比如说,
Conversation Update代表它的状态被改变,比如把bot添加到会话中,或者更改会话对象。
Contact Relation Update代表bot被用户在联系人列表中添加/删除了。
Typing可以告诉双方一个“正在打字”的状态。
Ping是用来测试Bot的。
Delete是用来删除用户的个人身份信息的。
我们在初次接触Bot时,应该重点掌握Message这种类型。
现在我们来读一段代码:
……(创建了一个connector)
Activity replyToConversation = message.CreateReply("Should go to conversation, with a carousel");
replyToConversation.Recipient = message.From;
replyToConversation.Type = "message";
replyToConversation.AttachmentLayout = AttachmentLayouts.Carousel;
replyToConversation.Attachments = new List<Attachment>();
Dictionary<string, string> cardContentList = new Dictionary<string, string>();
cardContentList.Add("PigLatin", "https://<ImageUrl1>");
cardContentList.Add("Pork Shoulder", "https://<ImageUrl2>");
cardContentList.Add("Bacon", "https://<ImageUrl3>");
foreach(KeyValuePair<string, string> cardContent in cardContentList)
{
List<CardImage> cardImages = new List<CardImage>();
cardImages.Add(new CardImage(url:cardContent.Value ));
List<CardAction> cardButtons = new List<CardAction>();
CardAction plButton = new CardAction()
{
Value = $"https://en.wikipedia.org/wiki/{cardContent.Key}",
Type = "openUrl",
Title = "WikiPedia Page"
};
cardButtons.Add(plButton);
HeroCard plCard = new HeroCard()
{
Title = $"I'm a hero card about {cardContent.Key}",
Subtitle = $"{cardContent.Key} Wikipedia Page",
Images = cardImages,
Buttons = cardButtons
};
Attachment plAttachment = plCard.ToAttachment();
replyToConversation.Attachments.Add(plAttachment);
}
replyToConversation.AttachmentLayout = AttachmentLayoutTypes.Carousel;
var reply = await connector.Conversations.SendToConversationAsync(replyToConversation);
请关注第4行,设置了这个Activity类的收件人就是这个Activity的发起者(一对一对话中这一行可以被去掉)。
第5行中,设置了Type为Message。
第6行中,设置了显示模式为转盘。
第7行,表示这个activity要加入一些attachment(比如说别的文章会提到的Rich Card),初始化了一个List。
后面将三种东西打包作为三个整体使用。
后面的代码有兴趣的可以尝试,我在别的文章里会解释这些卡片的详细含义。
Bot Framework:Activity类简明指南的更多相关文章
- 手把手教你利用微软的Bot Framework,LUIS,QnA Maker做一个简单的对话机器人
最近由于要参加微软亚洲研究院的夏令营,需要利用微软的服务搭建一个对话Bot,以便对俱乐部的情况进行介绍,所以现学了几天,搭建了一个简单的对话Bot,期间参考了大量的资料,尤其是下面的这篇博客: htt ...
- 下一个时代,对话即平台 —— 开始使用Bot Framework和Cognitive Service来打造你的智能对话服务
在16年3月30号微软的全球开发者大会Build上发布了Bot Framework,微软认为下一个big thing是Conversation as a Platform,简称CaaP,中文应该叫做& ...
- 如何将 Microsoft Bot Framework 链接至微信公共号
说到 Microsoft Bot Framework 其实微软发布了已经有一段时间了,有很多朋友可能还不太了解,微软Bot的功能今天我给大家简单的介绍一下,Bot Framework的开发基础以及如何 ...
- Power BI Embedded 与 Bot Framework 结合的AI解决方案
最近最热门的话题莫过于AI了,之前我做过一片讲 BOTFRAMEWORK和微信 相结合的帖子 如何将 Microsoft Bot Framework 链接至微信公共号 我想今天基于这个题目扩展一下,P ...
- Bot Framework 搭建聊天机器人
这周我来跟大家分享的是在Microsoft Build 2016上发布的微软聊天机器人的框架. 现如今,各种人工智能充斥在我们的生活里.最典型的人工智能产品就是聊天机器人,它既可以陪我们聊天,也可以替 ...
- Microsoft Bot Framework with LUIS
今年微软的编程之美的主题是“对话即平台”,“人工智能”,要求参赛选手用到Bot Framework与Cognitive Services. 大多数人应该对这两个技术都不怎么熟悉吧,我就在这里写写自己所 ...
- How the Microsoft Bot Framework Changed Where My Friends and I Eat: Part 1
Bots are everywhere nowadays, and we interact with them all of the time. From interactions on our ph ...
- Power BI Embedded 与 Bot Framework 结合的AI报表系统
最近最热门的话题莫过于AI了,之前我做过一片讲 BOTFRAMEWORK和微信 相结合的帖子 如何将 Microsoft Bot Framework 链接至微信公共号 我想今天基于这个题目扩展一下,P ...
- Java8简明指南
Java8简明指南 转载自并发编程网 – ifeve.com本文链接地址: Java8简明指南 欢迎来到Java8简明指南.本教程将一步一步指导你通过所有新语言特性.由短而简单的代码示例,带你了解如何 ...
随机推荐
- MAF框架的使用限制
虽然MAF实现了插件式开发,动态热插拨,AppDomain隔离等诸多优点,但是正因为它复杂的功能机制也带来了很多其它方面的使用限制,下面列出官方给出的MAF框架的使用限制. 1) 在应用主程序显示的插 ...
- Swoole 协程 MySQL 客户端与异步回调 MySQL 客户端的对比
Swoole 协程 MySql 客户端与 异步回调 MySql 客户端的对比 为什么要对比这两种不同模式的客户端? 异步 MySQL 回调客户端是虽然在 Swoole 1.8.6 版本就已经发布了, ...
- Unity 5着色器系统代码介绍(下)
http://forum.china.unity3d.com/thread-25738-1-10.html 上一篇对着色器系统的工作原理做了介绍,现在我们将继续深入,将目光聚焦在标准着色器的光照函数. ...
- 3dmax切割平行线
1 选择物体(可编辑多边形),选择边 ,然后点击切片平面 2 然后会出现黄色线框 3 移动旋转黄色线框到合适位置,然后点切片 4 结果
- 30个php操作redis常用方法代码例子【转】
背景:redis这个新产品在sns时很火,而memcache早就存在, 但redis提供出来的功能,好多网站均把它当memcache使用,这是大才小用,这儿有30个方法来使用redis,值得了解. 这 ...
- openinstall渠道统计工具介绍
大家好,今天给大家介绍一下如何使用openinstall 来实现APP 渠道统计,做运营推广的朋友应该对渠道统计并不陌生,之前一般都是让技术的同事打渠道包方式进行渠道统计,而且只有安卓才能打渠道包.o ...
- 洛谷 P3381 【模板】最小费用最大流
题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行包含四个正整数\(N.M.S.T\) ...
- day5字典作业详解
1.day5题目 1.有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2&q ...
- JMeter(6) jenkins测试报告及邮件优化
jenkins邮件 使用jenkins执行完任务自动将测试结果发送到邮箱,效果如下: 生成html报告 build文件设置 jenkins设置 SummaryReport写入邮件正文 ...
- 事务&数据库连接池&DBUtils
事务的特性 原子性 指的是 事务中包含的逻辑,不可分割. 一致性 指的是 事务执行前后.数据完整性 隔离性 指的是 事务在执行期间不应该受到其他事务的影响 持久性 指的是 事务执行成功,那么数据应该持 ...