簡體   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