如何使chatbot(机器人框架)从任何文件夹向用户(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
        }  
    }

singhan 回答:如何使chatbot(机器人框架)从任何文件夹向用户(NodeJS)发送附件文件?

您只需要将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
    }  
}
,

我已经找到了解决方法,我已经使用软件包 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
            };
        }

    }

感谢大家的答复

本文链接:https://www.f2er.com/3104788.html

大家都在问