I’m trying to handle files in bot framework in teams.
I have a dialog with this function :
async finalStep(stepContext) {
if (stepContext.result && stepContext.result.length > 0) {
stepContext.values.files = await Promise.all(stepContext.result.map(async file => ({
filename: file.name,
fileContent: await this.getFileContentInBase64(file.contentUrl)
})));
} else {
stepContext.values.files = [];
}
const userProfile = await this.userState.get(stepContext.context, {});
const aadObjectId = userProfile.aadObjectId;
const finalData = {
userId: aadObjectId,
cet: stepContext.values.cet,
sector: stepContext.values.sector,
job: stepContext.values.job,
requestType: stepContext.values.requestType,
description: stepContext.values.description,
files: []
};
const apiUrl = ''; // power automate url
try {
const response = await axios.post(apiUrl, finalData, {
headers: {
'Content-Type': 'application/json'
}
});
const ticketId = response.data.id;
await stepContext.context.sendActivity(t('dt_ticket.ticket_created', { ticketId }));
const accessToken = await getAccessToken();
const siteId = '';
const driveId = '';
const folderId = '';
await this.uploadFilesToGraph(accessToken, siteId, driveId, folderId, stepContext.values.files, ticketId);
} catch (error) {
console.error('Error sending data to the API: ', error.response ? error.response.data : error.message);
}
return await stepContext.endDialog();
}
async getFileContentInBase64(contentUrl) {
const response = await axios.get(contentUrl, { responseType: 'arraybuffer' });
return Buffer.from(response.data, 'binary').toString('base64');
}
async uploadFilesToGraph(accessToken, siteId, driveId, folderId, files, ticketId) {
try {
const client = Client.init({
authProvider: (done) => {
done(null, accessToken);
}
});
for (const file of files) {
//const fileContent = Buffer.from(file.fileContent, 'base64');
// Create a small file with random content
const fileName = file.filename;
const fileContent = 'This is a random test content: ' + Math.random();
fs.writeFileSync(fileName, fileContent);
const fileBuffer = fs.readFileSync(fileName);
const fileUploadUrl = `/sites/${siteId}/drives/${driveId}/items/${folderId}:/${ticketId} - ${file.filename}:/content`;
const response = await client.api(fileUploadUrl).put(fileBuffer);
}
} catch (error) {
this.logDebug('Error uploading files to Graph', { error: error });
}
}
I modify this to try to juste upload a little text file. On emulator localhost, it’s working well, but when I’m using this code through teams bot, I have the following error :
Error: DialogContextError
And I don’t have more informations.
The part which catch the error on index.js :
// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
console.error(`n [onTurnError] unhandled error: ${ error }`);
// Log error to Application Insights
telemetryMiddleware.telemetryClient.trackTrace({
message: 'Error',
properties: { error: error.toString() },
severityLevel: 3 // Error
});
// Send a trace activity
await context.sendTraceActivity(
'OnTurnError Trace',
`${ error }`,
'https://www.botframework.com/schemas/error',
'TurnError'
);
// Send a message to the user
await context.sendActivity('The bot encountered an error or bug.');
await context.sendActivity('To continue to run this bot, please fix the bot source code.');
await context.sendActivity('Error : ' + error);
};
I can’t figure out where the issue is, if you have some thoughts, would be great.