繁体   English   中英

如何让聊天机器人(机器人框架)将附件从任何文件夹发送给用户(NodeJS)?

[英]How can i make the chatbot (bot framework) send an attached file from any folder to the user (NodeJS)?

如何让聊天机器人从任何文件夹向用户发送附件?
我有下面的代码,但他不工作,他显示任何东西。

你能帮我吗。

const { TextPrompt, AttachmentPrompt } = require('botbuilder-dialogs');
constructor(luisRecognizer, bookingDialog) {
        super('MainDialog'); 

        this.addDialog(new TextPrompt('TextPrompt'))
            .addDialog(new AttachmentPrompt('AttachmentPrompt'))
            .addDialog(bookingDialog)
            .addDialog(new WaterfallDialog(MAIN_WATERFALL_DIALOG, [
                this.introStep.bind(this), 
                this.sendAttachmentStep.bind(this),
                this.finalStep.bind(this)
            ]));

    }
async sendAttachmentStep(stepContext) { 
        var base64Name = "Book1.xlsx";
        var base64Type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        var base64Url = "http://localhost:49811/v3/attachments/.../views/original";

        var att = await stepContext.prompt('AttachmentPrompt', {

                name: base64Name,
                contentType: base64Type,
                contentUrl: base64Url,

        });
        var nex = await stepContext.next();
        return {
            att, nex
        }  
    }

您只需将文件作为 base64 加载到您的代码中:

var fs = require('fs');

function base64_encode(file) {
    // read binary data
    var bitmap = fs.readFileSync(file);
    // convert binary data to base64 encoded string
    return new Buffer(bitmap).toString('base64');
}

async sendAttachmentStep(stepContext) {
    var base64Name = "Book1.xlsx";
    var base64Type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    var file = require(./<yourFile>);

    var base64File = base64_encode(file);

    var att = await stepContext.prompt('AttachmentPrompt', {

            name: base64Name,
            contentType: base64Type,
            contentUrl: `data:${ base64Type };base64,${ base64File }`,

    });
    var nex = await stepContext.next();
    return {
        att, nex
    }  
}

我已经找到了方法,我使用了 package axios来获取数据,然后在 base64 中转换它。

 async attachmentsStep(stepContext, next) {
        var activity = stepContext.context.activity;

        if (activity.attachments && activity.attachments.length > 0) {
            var attachment = activity.attachments[0];

            var base64Url = attachment.contentUrl;
            console.log(process.env.PATH);

            var axios = require('axios');

            var excel = await axios.get(base64Url, { responseType: 'arraybuffer' });
            var base64str = Buffer.from(excel.data).toString('base64');

            // base64str = 'data:' + base64Type + ';base64,' + base64str;

            this.base64str = base64str;

            var nex = await stepContext.next();


            return {
                base64str,
                nex
            };
        }

    }

谢谢大家的回复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM