简体   繁体   English

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

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

How can i make the chatbot send an attached file from any folder to the user?如何让聊天机器人从任何文件夹向用户发送附件?
I have the code below but he doesn't work, he show anything.我有下面的代码,但他不工作,他显示任何东西。

Can you help me please.你能帮我吗。

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
        }  
    }

You just need to load the file as base64 in to your code:您只需将文件作为 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
    }  
}

I have find how to do it, I have used the package axios to have the data and after transform it in base64.我已经找到了方法,我使用了 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
            };
        }

    }

thanks all of you for your response谢谢大家的回复

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

相关问题 如何在使用 Bot 框架的聊天机器人中使用 nodeJS 获取 excel 文件的路径? - How can I get the path of an excel file with nodeJS in a chatbot using Bot framework? 如何让机器人在只有特定用户才能看到的 Discord 频道中发送消息? - How can I make a bot send a message in a Discord channel that only a certain user can see? 如何在不使用任何框架的情况下访问以Node.js保存为另一个文件的形式提交的数据 - How can I access a data submitted in a form which is saved as another file with nodejs without using any framework 如何在NodeJS中构建Messenger Messenger? - How can i structure my Messenger Chatbot in NodeJS? 如何确保我的twitch机器人只能使用NODEJS和TMI API接收某些用户的命令? - How can I make sure that my twitch bot can only take commands from certain users with NODEJS and the TMI API? 如何让我的 Discord 机器人给用户一个角色? - How can I make my Discord bot give a user a role? 如何在.BOT文件中更改聊天机器人的LUIS应用程序? - How to change LUIS application for a chatbot in the .BOT file? Bot Framework-在NodeJS中处理用户发送的图像 - Bot Framework - Handling user sent images in NodeJS 在发送机器人框架 v4 Nodejs 之前拦截机器人消息 - Intercept bot messages before send in bot framework v4 Nodejs 使Discord bot使用NodeJS发送带有消息的图片 - Make Discord bot send picture with message with NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM