Bot框架:标记消息

时间:2017-07-20 06:57:13

标签: json tags bots botframework

因此,当机器人通过Bot Connecter发送消息时,它会将其作为JSON对象发送,其外观类似于:

{
  "type": "message",
  "timestamp": "2017-07-20T06:35:09.196Z",
  "serviceUrl": "http://127.0.0.1:53484",
  "channelId": "emulator",
  "from": {
  "id": "ijfi3f969c6fij756",
  "name": "Bot"
},
"conversation": {
"id": "g671h707amhch9326"
},
"recipient": {
"id": "default-user"
},
"membersAdded": [],
"membersRemoved": [],
"text": "Hey there!",
"attachments": [],
"entities": [],
"replyToId": "efincfmnc40la0g7c",
"id": "hi959g2a8ji1fn86",
"localTimestamp": "2017-07-20T08:35:09+02:00"
}

我们想添加一些额外的标签,以传输有关正在发送的消息的更多数据。例如,我们可能希望添加一个标记,显示我们希望用户使用电子邮件地址进行回复。也许是这样的事情:

  

{
    "输入":"消息",
   "期待":"电子邮件"
    " timestamp":" 2017-07-20T06:35:09.196Z",     " serviceUrl":" http://127.0.0.1:53484",     " channelId":"模拟器",     "来自":{
    " id":" ijfi3f969c6fij756",      "姓名":" Bot"   },

我们的想法是,我们希望使用自定义标记来运行其他代码。例如,我们可能想做的事情是:

if(reply.Expecting.Equals("email")
{
  //Do something awesome here.
}

但是,我们还没有找到有关如何自定义这些JSON消息的任何信息。可能吗?如果是这样,我们将如何进行呢?

2 个答案:

答案 0 :(得分:1)

您可以使用中间件来完成此任务。这是C#文档和NodeJS文档。

基本上,您可以从bot对象设置中间件:

bot.use({
    botbuilder: (sess, next) => {
        inbound(sess, next);
    }
    ,send: (evt, next) => {
        outbound(evt, next);
    }
});

...其中botbuilder捕获用户发送的内容,send捕获机器人发送的内容。

然后,您可以定义处理消息的函数,如果您认为合适,则可以更改它们:

function inbound(sess: builder.Session, next: Function): void {
     console.log(sess.message);
    next();
}

function outbound(evt: builder.IEvent, next: Function): void {
    console.log(evt);
    next();
}

我的建议是更改消息数据结构本身,而是使用中间件来执行自定义行为,因为更改这些消息可能会产生副作用。您可以轻松捕获中间件中所需的内容,然后将参数传递给next()函数,以允许后续对话框处理期望。

此外,如果您正在尝试捕获预期的用户输入,则正则表达式与意图对话框匹配或触发操作非常有效。

答案 1 :(得分:1)

假设您使用的是.net Framework。

下面是我能想到的一个选项。 向活动介绍自定义属性

CustomActivity

上实施Activity类
 public class CustomActivity : Activity
    {
        [JsonProperty(PropertyName = "cutomprop")]
        public string MyCustomProp { get; set; } = "Custom Prop";
    }

然后在回复回复时考虑创建CustomActivity类型的Activity并将所有原始Activity的道具复制到它(为此目的使用工具Automapper)

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

        ConnectorClient client = new ConnectorClient(new Uri(activity.ServiceUrl));

      var reply = new CustomActivity
        {

            ChannelData = activity.ChannelData,
            ChannelId = activity.ChannelId,
            ServiceUrl = activity.ServiceUrl,
            Name = activity.Name,
            Action = activity.Action,
            Type = activity.Type,
            Conversation = activity.Conversation,
            Code = activity.Code,
            From = activity.From,
            Value = activity.Value,
            Text = activity.Text,
            InputHint = activity.InputHint,
            Attachments = activity.Attachments,
            Entities = activity.Entities,
            ReplyToId = activity.ReplyToId,
            AttachmentLayout = activity.AttachmentLayout,
            HistoryDisclosed = activity.HistoryDisclosed,
            Id = activity.Id,
            LocalTimestamp = activity.LocalTimestamp,
            Locale = activity.Locale,
            MembersAdded = activity.MembersAdded,
            MembersRemoved = activity.MembersRemoved,
            Properties = activity.Properties,
            Recipient = activity.Recipient,
            RelatesTo = activity.RelatesTo,
            Speak = activity.Speak,
            SuggestedActions = activity.SuggestedActions,
            Summary = activity.Summary,
            TextFormat = activity.TextFormat,
            Timestamp = activity.Timestamp,
            TopicName = activity.TopicName

        };

        reply.Text = "I have colors in mind, but need your help to choose the best one.";
        reply.Type = ActivityTypes.Message;
        reply.TextFormat = TextFormatTypes.Plain;
       //set any other prop you want as per your needs
        await client.Conversations.SendToConversationAsync(reply);
        context.Wait(MessageReceivedAsync);
    }

以下是我得到的输出

{
  "cutomprop": "Custom Prop",
  "type": "message",
  "id": null,
  "timestamp": "2017-07-20T12:44:41.504Z",
  "localTimestamp": "2017-07-20T18:14:41+05:30",
  "serviceUrl": "http://127.0.0.1:56931",
  "channelId": "emulator",
  "from": {
    "id": "default-user",
    "name": "default-user"
  },
  "conversation": {
    "id": "h0j9hn8bicfm8e509"
  },
  "recipient": {
    "id": "mcfimjke5aebemhf",
    "name": "Bot"
  },
  "textFormat": "plain",

希望有所帮助...考虑为您的场景验证它并注意其他任何副作用。 **没有在所有频道上测试它,它适用于模拟器