﻿var azure = require('azure-storage');
var fs = require("fs");
var Twitter = require('twitter');
var path = require('path');
var os = require('os');

module.exports = function (context, myBlob) {
    var containerName = "workitems";
    var destinationFileNameTarget = path.join(os.tmpdir(),"file.jpg");

    var client = new Twitter({
        consumer_key: process.env.TWITTER_CONSUMER_KEY,
        consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
        access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
        access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
    });
 
    // context.log("Node.js blob trigger function processed blob \n Name:", context.bindingData.name, "\n Blob Size:", myBlob.length, "Bytes");
    // context.log('Node.js blob trigger function processed blob', myBlob);
    // context.log('Node.js blob type of', typeof myBlob);

    var blobName = context.bindingData.name;
    var blobSvc = azure.createBlobService();
    
    var writeable = fs.createWriteStream(destinationFileNameTarget);
    blobSvc.createReadStream(containerName, blobName).pipe(writeable);
    writeable.on('finish', function(){  
        var twitterImage = require('fs').readFileSync(destinationFileNameTarget);

        // Make post request on media endpoint. Pass file data as media parameter
        client.post('media/upload', {media: twitterImage}, function(error, media, response) {
            if (!error) {
                // If successful, a media object will be returned.
                context.log('media uploaded');

                // Lets tweet it
                var status = {
                    status: 'I am a tweet',
                    media_ids: media.media_id_string // Pass the media id string
                }

                client.post('statuses/update', status, function(error, tweet, response) {
                    if (!error) {
                        var tweet_link = "https://twitter.com/MicrosoftLaunch/status/" + tweet[0].d_str;
                        // context.log('tweet sent', tweet);
                        context.log("tweet sent");
                        context.log('tweet link', tweet_link);
                        // context.log('tweet response', response);
                        blobSvc.deleteBlob(containerName, blobName, function(error, response){
                            if(!error){
                                // Blob has been deleted
                                context.log('deleted');
                                context.done();
                            } else {
                                context.log('error deleting');
                                context.done();
                            }
                        });
                    }
                });
            } else {
                context.log('error uploading', error);
                context.done();
            }
        });
    });
};
@@ -591,289 +591,8 @@
  %7D);
-%0A %0A    // context.log(%22Node.js blob trigger function processed blob %5Cn Name:%22, context.bindingData.name, %22%5Cn Blob Size:%22, myBlob.length, %22Bytes%22);%0A    // context.log('Node.js blob trigger function processed blob', myBlob);%0A    // context.log('Node.js blob type of', typeof myBlob);
 %0A%0A  
@@ -1116,32 +1116,79 @@
 ia, response) %7B%0A
+            context.log(%22Begin Media Upload%22);%0A
             if (
@@ -1297,22 +1297,25 @@
 og('
-media uploaded
+upload successful
 ');%0A
@@ -1629,37 +1629,148 @@
             
-if (!error) %7B
+context.log(%22Send Tweet%22);%0A                    if (!error) %7B%0A                        context.log(%22No Errors Sending Tweet%22);
 %0A           
@@ -1867,68 +1867,8 @@
 tr;%0A
-                        // context.log('tweet sent', tweet);
 %0A   
@@ -1901,17 +1901,17 @@
 xt.log(%22
-t
+T
 weet sen
@@ -1948,25 +1948,25 @@
 ontext.log('
-t
+T
 weet link', 
@@ -1982,75 +1982,8 @@
 k);%0A
-                        // context.log('tweet response', response);
 %0A   
@@ -2074,16 +2074,80 @@
 ponse)%7B%0A
+                            context.log(%22Begin deleting Blob%22);%0A
         
@@ -2280,17 +2280,17 @@
 xt.log('
-d
+D
 eleted')
@@ -2425,23 +2425,23 @@
 og('
-e
+E
 rror 
-d
+D
 eleting'
 );%0A 
@@ -2436,16 +2436,23 @@
 eleting'
+, error
 );%0A     
@@ -2648,15 +2648,15 @@
 og('
-e
+E
 rror 
-u
+U
 ploa

Add more logging