jazzedge
7/28/2017 - 11:28 AM

Bot - Blob storage

Bot - Blob storage

//01. Configure Blob storage

var builder = require('botbuilder');
var azure = require('azure-storage');
var request = require('request').defaults({ encoding: null });


var accessKey = 'XdU6cKUYRT ... 5Bf3yzgjY1JKIg==';
var storageAccount = 'roccoblob01';
var containerName = 'roccoblobcontainer1';
var blobSvc = azure.createBlobService(storageAccount, accessKey);

//02. Take a picture, with a max of 2 retries
        //https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/capability-SimpleTaskAutomation
        builder.Prompts.attachment(session, "Upload a picture for me to transform.", {
            retryPrompt: 'The value you entered is not a valid picture. Please try again:',
            maxRetries: 2
            //If the maximum number of retries are reached the next dialog is called with the args.resumed set in notCompleted. 
            //The message the bot will send when an no valid user input was received is customizable with the retryPrompt.
        });

//03. Response displayed in the next waterfall step

function (session, results, next) {
    var msg = session.message;
    if (msg.attachments && msg.attachments.length > 0) {
        // Echo back attachment
        var attachment = msg.attachments[0];
            session.send({
                text: "You sent:",
                attachments: [
                    {
                        contentType: attachment.contentType,
                        contentUrl: attachment.contentUrl,
                        name: attachment.name,
                        path: attachment.data   //Try attachment.content too
                        
                    }
                ]
            });
            console.log('Image attachment name: %s', attachment.name);
            console.log('Image content type: %s', attachment.contentType);
            console.log('Image content url: %s', attachment.contentUrl);
            console.log('Image content data: %s', attachment.path);
            var imageUrl = attachment.contentUrl + '/' + attachment.name;
            console.log('Image absolute URL: %s', imageUrl);
            //Grab image and encode to Base64
            var tempUrl = attachment.contentUrl; //'https://roccobot02gghwh4.blob.core.windows.net/images/chatbot.jpg';
            request.get(tempUrl, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                    data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');
                    console.log('Base64:' + data);
                    //Upload to BLOB        
                    blobSvc.createBlockBlobFromText(containerName, storageAccount, data, function(error, result, response){
                    if(!error){
                        // file uploaded
                        console.log('Image uploaded to Blob successfully');
                    } else {
                        console.log('Image NOT uploaded to Blob');
                    }
                    });
                } else {
                    console.log('Base64 response status code:' + response.statusCode);
                }
            });
            //List blobs in container
            blobSvc.listBlobsSegmented(containerName, null, function(err, result) {
            if (err) {
                console.log("Couldn't list blobs for container %s", containerName);
                console.error(err);
            } else {
                console.log('Successfully listed blobs for container %s', containerName);
                console.log(result.entries);
                console.log(result.continuationToken);
            }
        });
        } else {
            // Echo back users text
            session.send("You said: %s", session.message.text);
        }
        session.dialogData.height = results.response;
        //session.send('You answered %s', session.dialogData.height);
        next();
    }