bots-draft's People
bots-draft's Issues
File package.json
{
"name": "botbuilder-sample-imagecaption",
"version": "1.0.0",
"description": "Bot Builder Sample - Image Caption",
"scripts": {
"start": "node app.js"
},
"license": "MIT",
"dependencies": {
"botbuilder": "^3.7.0",
"dotenv-extended": "^1.0.4",
"needle": "^1.4.3",
"request": "^2.79.0",
"restify": "^4.3.0",
"url": "^0.11.0",
"valid-url": "^1.0.9"
}
}
File qnadialog.js
'use strict';
const request = require('request');
module.exports = (session, args, next) => {
session.sendTyping();
const question = session.message.text;
const bodyText = JSON.stringify({question: question});
const uri = "https://westus.api.cognitive.microsoft.com/qnamaker/v1.0/knowledgebases/" + process.env.KBID +"/generateAnswer";
console.log(uri);
request.post(uri, { body: bodyText }, (err, code, body) => {
if(err) {
console.log(err);
session.endConversation('Vish, algo deu errado por aqui.');
} else {
const response = JSON.parse(body);
console.log(response);
if(response.score > 50) {
session.endConversation(response.answer);
} else if (response.score > 0) {
session.send("Não tenho certeza se estou correto, mas...");
session.endConversation(response.answer);
} else {
session.endConversation("Eu ainda não sei responder sobre: " + session.message.text);
}
}
}).setHeader('Ocp-Apim-Subscription-Key', process.env.SUBSCRIPTION_KEY);
};
File app.js
/*-----------------------------------------------------------------------------
An image caption bot for the Microsoft Bot Framework.
-----------------------------------------------------------------------------*/
// This loads the environment variables from the .env file
require('dotenv-extended').load();
var builder = require('botbuilder'),
needle = require('needle'),
restify = require('restify'),
url = require('url'),
validUrl = require('valid-url'),
captionService = require('./caption-service');
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());
// Gets the caption by checking the type of the image (stream vs URL) and calling the appropriate caption service method.
var bot = new builder.UniversalBot(connector, function (session) {
if (hasImageAttachment(session)) {
var stream = getImageStreamFromMessage(session.message);
captionService
.getCaptionFromStream(stream)
.then(function (caption) { handleSuccessResponse(session, caption); })
.catch(function (error) { handleErrorResponse(session, error); });
} else {
var imageUrl = parseAnchorTag(session.message.text) || (validUrl.isUri(session.message.text) ? session.message.text : null);
if (imageUrl) {
captionService
.getCaptionFromUrl(imageUrl)
.then(function (caption) { handleSuccessResponse(session, caption); })
.catch(function (error) { handleErrorResponse(session, error); });
} else {
session.send('Você enviou uma foto? Eu gosto de ver fotos! Tente me enviar uma imagem ou um URL de imagem');
}
}
});
//=========================================================
// Bots Events
//=========================================================
//Sends greeting message when the bot is first added to a conversation
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
var reply = new builder.Message()
.address(message.address)
.attachments([{
contentType: "image/jpeg",
contentUrl: "http://www.idhabbo.com.br/media/uploads/up-a23d0841dc.png"
}])
.text('Olá, que bom que você veio conversar! Você sabia que eu consigo entender imagens? Envie um arquivo de imagem ou indique a URL de uma imagem.');
bot.send(reply);
}
});
}
});
//=========================================================
// Utilities
//=========================================================
function hasImageAttachment(session) {
return session.message.attachments.length > 0 &&
session.message.attachments[0].contentType.indexOf('image') !== -1;
}
function getImageStreamFromMessage(message) {
var headers = {};
var attachment = message.attachments[0];
if (checkRequiresToken(message)) {
// The Skype attachment URLs are secured by JwtToken,
// you should set the JwtToken of your bot as the authorization header for the GET request your bot initiates to fetch the image.
// https://github.com/Microsoft/BotBuilder/issues/662
connector.getAccessToken(function (error, token) {
var tok = token;
headers['Authorization'] = 'Bearer ' + token;
headers['Content-Type'] = 'application/octet-stream';
return needle.get(attachment.contentUrl, { headers: headers });
});
}
headers['Content-Type'] = attachment.contentType;
return needle.get(attachment.contentUrl, { headers: headers });
}
function checkRequiresToken(message) {
return message.source === 'skype' || message.source === 'msteams';
}
/**
* Gets the href value in an anchor element.
* Skype transforms raw urls to html. Here we extract the href value from the url
* @param {string} input Anchor Tag
* @return {string} Url matched or null
*/
function parseAnchorTag(input) {
var match = input.match('^<a href=\"([^\"]*)\">[^<]*</a>$');
if (match && match[1]) {
return match[1];
}
return null;
}
//=========================================================
// Response Handling
//=========================================================
function handleSuccessResponse(session, caption) {
if (caption) {
session.send('Eu acho que é... ' + caption);
}
else {
session.send('Não consegui encontrar uma legenda :(');
}
}
function handleErrorResponse(session, error) {
session.send('Oops! Algo deu errado.');
console.error(error);
}
arquivo de gestao
{
"name": "qn-a-maker-presentation",
"dependencies": {
"botbuilder": "^3.8.4",
"dotenv": "^2.0.0",
"request": "^2.81.0",
"restify": "^4.3.0"
},
"devDependencies": {
"@types/node": "^6.0.78",
"@types/restify": "^2.0.35"
}
}
File caption-service.js
// The exported functions in this module makes a call to Microsoft Cognitive Service Computer Vision API and return caption
// description if found. Note: you can do more advanced functionalities like checking
// the confidence score of the caption. For more info checkout the API documentation:
// https://www.microsoft.com/cognitive-services/en-us/Computer-Vision-API/documentation/AnalyzeImage
var request = require('request').defaults({ encoding: null });
var VISION_URL = 'https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze/?visualFeatures=Description&form=BCSIMG&subscription-key=' + process.env.MICROSOFT_VISION_API_KEY;
/**
* Gets the caption of the image from an image stream
* @param {stream} stream The stream to an image.
* @return {Promise} Promise with caption string if succeeded, error otherwise
*/
exports.getCaptionFromStream = function (stream) {
return new Promise(
function (resolve, reject) {
var requestData = {
url: VISION_URL,
encoding: 'binary',
headers: { 'content-type': 'application/octet-stream' }
};
stream.pipe(request.post(requestData, function (error, response, body) {
if (error) {
reject(error);
}
else if (response.statusCode !== 200) {
reject(body);
}
else {
resolve(extractCaption(JSON.parse(body)));
}
}));
}
);
};
/**
* Gets the caption of the image from an image URL
* @param {string} url The URL to an image.
* @return {Promise} Promise with caption string if succeeded, error otherwise
*/
exports.getCaptionFromUrl = function (url) {
return new Promise(
function (resolve, reject) {
var requestData = {
url: VISION_URL,
json: { 'url': url }
};
request.post(requestData, function (error, response, body) {
if (error) {
reject(error);
}
else if (response.statusCode !== 200) {
reject(body);
}
else {
resolve(extractCaption(body));
}
});
}
);
};
/**
* Extracts the caption description from the response of the Vision API
* @param {Object} body Response of the Vision API
* @return {string} Description if caption found, null otherwise.
*/
function extractCaption(body) {
if (body && body.description && body.description.captions && body.description.captions.length) {
return body.description.captions[0].text;
}
return null;
}
File bot.js
const builder = require('botbuilder');
const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// set up default dialog to use QnA Maker
const bot = new builder.UniversalBot(connector, require('./qnadialog.js'));
//=========================================================
// Bots Events
//=========================================================
//Sends greeting message when the bot is first added to a conversation
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
var reply = new builder.Message()
.address(message.address)
.text("Olá, eu sou o FAQ Bot e irei te ajudar!")
.attachments([{
contentType: "image/jpeg",
contentUrl: "http://www.theoldrobots.com/images62/Bender-18.JPG"
}]);
bot.send(reply);
}
});
}
});
module.exports = bot;
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.