jazzedge
7/28/2017 - 11:48 AM

Bot - Vision API example

Bot - Vision API example

//https://github.com/alyssaong1/Bot-Framework-HOL/blob/master/FinalSolution/index.js
//https://departmentfortransport.files.wordpress.com/2013/01/woman-riding-a-bike.jpg

var rp = require('request-promise');

bot.dialog('/analyseImage', [
    function (session){
        // Ask the user which category they would like
        // Choices are separated by |
        builder.Prompts.text(session, "Enter an image url to get the caption for it: ");
    }, function (session, results, next){
        // The user chose a category
        if (results.response) {
           //Show user that we're processing their request by sending the typing indicator
            session.sendTyping();
            // Build the url we'll be calling to get top news
            var url = "https://westeurope.api.cognitive.microsoft.com/vision/v1.0/describe/";
            // Build options for the request
            var options = {
                        method: 'POST', // thie API call is a post request
                        uri: url,
                        headers: {
                            'Ocp-Apim-Subscription-Key': '10da26d86a0447 ... 03e11209d',
                            'Content-Type': "application/json"
                        },
                        body: {
                            url: results.response,
                            language: 'en'
                        },
                        json: true
                    }
            //Make the call
            rp(options).then(function (body){
                // The request is successful
                console.log(body["description"]["captions"]);
                session.send(body["description"]["captions"][0]["text"]);
            }).catch(function (err){
                // An error occurred and the request failed
                session.send("Argh, something went wrong. : ", err);
            }).finally(function () {
                // This is executed at the end, regardless of whether the request is successful or not
                session.endDialog();
            });
        } else {
            // The user choses to quit
            session.endDialog("Ok. Mission Aborted.");
        }
    }
]);