Bot Microsoft Framework - 将附件和存储发送到本地服务器

时间:2017-10-31 18:25:09

标签: ruby-on-rails node.js botframework

我发送了一个附件,它显示我:

enter image description here

回复就像 -

{"body"=>{"type"=>"message", "timestamp"=>"2017-10-31T17:45:37.088Z", "attachments"=>[{"name"=>"bot.png", "contentType"=>"image/png", "contentUrl"=>"http://localhost:45323/v3/attachments/a62ddeiklh5i/views/original"}],....

我是Node.js以及Microsoft Bot Framework中的新人。

我想将图像存储在本地服务器上。我怎样才能做到这一点?此外,请提供一些链接,以便我检查并尝试。

1 个答案:

答案 0 :(得分:1)

查看core-ReceiveAttachment示例。在那里,您将找到使用提供的contentUrl从邮件中下载附件的示例代码。

之后由您决定如何在本地服务器上存储

function (session) {

    var msg = session.message;
    if (msg.attachments.length) {

        // Message with attachment, proceed to download it.
        // Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
        var attachment = msg.attachments[0];
        var fileDownload = checkRequiresToken(msg)
            ? requestWithToken(attachment.contentUrl)
            : request(attachment.contentUrl);

        fileDownload.then(
            function (response) {

                // Send reply with attachment type & size
                var reply = new builder.Message(session)
                    .text('Attachment of %s type and size of %s bytes received.', attachment.contentType, response.length);
                session.send(reply);

            }).catch(function (err) {
                console.log('Error downloading attachment:', { statusCode: err.statusCode, message: err.response.statusMessage });
            });
    }
}

// Helper methods

// Request file with Authentication Header
var requestWithToken = function (url) {
    return obtainToken().then(function (token) {
        return request({
            url: url,
            headers: {
                'Authorization': 'Bearer ' + token,
                'Content-Type': 'application/octet-stream'
            }
        });
    });
};

// Promise for obtaining JWT Token (requested once)
var obtainToken = Promise.promisify(connector.getAccessToken.bind(connector));

var checkRequiresToken = function (message) {
    return message.source === 'skype' || message.source === 'msteams';
};