Bot使用Microsoft Bot Framework(C#)发送动态创建的可下载CSV

时间:2017-07-25 11:24:00

标签: c# botframework dynamically-generated slack

目标:使用来自Sql Server查询的信息(不想保存文件)动态创建CSV文件,并在Slack上的bot响应中将其发送给用户。用户单击链接以将文件下载到他们的计算机。

我在MemoryStream中创建了文件信息,但是在机器人响应中发送它时遇到了问题。这是我的C#伪代码和受this启发的代码:

public async Task useSelectedUPC(IDialogContext context, IAwaitable<object> item)
    {
        var upc = await item;

        // use upc to query sql server and obtain information
        // use that information to create a MemoryStream result in the format of a csv file

        reply.Attachments = new List<Attachment>()
                {
                    new Attachment()
                    {
                        ContentType = "text/csv",
                        Content = $"data:text/csv; base 64, {Convert.ToBase64String(result.ToArray())}",
                        Name = "sample.csv"
                    }
                };
        reply.Text = "Results: ";
        await context.PostAsync(reply);
    }

我也试过

Content = result,

Content = result.ToArray(),

Content = "hello",

Content = Convert.ToBase64String(result.toArray()),

我甚至尝试将ContentType和名称更改为“text / plain”。

但我在Bot框架通道模拟器中看到的只是 [文件类型'text / csv'] 并且没有链接。在Slack中,除附件的文本外没有任何内容出现。

Microsoft Bot Framework Attachment Class对可接受的内容含糊不清。 哪些附件内容可以接受,如何将MemoryStream结果转换为可接受的格式?

1 个答案:

答案 0 :(得分:0)

要使用特定于Slack的消息功能,您需要配置包含附件的JSON对象,并将其放在Activity对象的channelData属性中。

来自文章Implement channel-specific functionality > Create a full-fidelity Slack message

  

要创建完全保真的Slack消息,请将Activity对象的channelData属性设置为指定Slack消息,Slack附件和/或Slack按钮的JSON对象。

此代码段显示了自定义Slack消息的channelData属性示例。

"channelData": {
   "text": "Now back in stock! :tada:",
   "attachments": [
        {
            "title": "The Further Adventures of Slackbot",
            "author_name": "Stanford S. Strickland",
            "author_icon": "https://api.slack.com/img/api/homepage_custom_integrations-2x.png",
            "image_url": "http://i.imgur.com/OJkaVOI.jpg?1"
        },
        {
            "fields": [
                {
                    "title": "Volume",
                    "value": "1",
                    "short": true
                },
                {
                    "title": "Issue",
                    "value": "3",
                    "short": true
                }
            ]
        },
        {
            "title": "Synopsis",
            "text": "After @episod pushed exciting changes to a devious new branch back in Issue 1, Slackbot notifies @don about an unexpected deploy..."
        },
        {
            "fallback": "Would you recommend it to customers?",
            "title": "Would you recommend it to customers?",
            "callback_id": "comic_1234_xyz",
            "color": "#3AA3E3",
            "attachment_type": "default",
            "actions": [
                {
                    "name": "recommend",
                    "text": "Recommend",
                    "type": "button",
                    "value": "recommend"
                },
                {
                    "name": "no",
                    "text": "No",
                    "type": "button",
                    "value": "bad"
                }
            ]
        }
    ]
}

有关Slack附件JSON结构的更多细节,请参阅: https://api.slack.com/docs/message-attachments