如何让聊天机器人(机器人框架)从任何文件夹向用户(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
}
}
我找到了方法,我使用包 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
};
}
}
感谢大家的回复
如何让聊天机器人从任何文件夹向用户发送附件?
我有下面的代码,但他不工作,他显示任何东西。
你能帮帮我吗
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
}
}
我找到了方法,我使用包 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
};
}
}
感谢大家的回复