Skip to content

Instantly share code, notes, and snippets.

@renatodossantosleal
Created September 22, 2016 01:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatodossantosleal/fcc12c97e696c9fd991f71ce08aaee55 to your computer and use it in GitHub Desktop.
Save renatodossantosleal/fcc12c97e696c9fd991f71ce08aaee55 to your computer and use it in GitHub Desktop.
Integração Watson + Facebook
var express = require('express');
var request = require('request');
var bodyParser = require('body-parser');
var watson = require('watson-developer-cloud');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
var conversation_id = "";
var w_conversation = watson.conversation({
url: 'https://gateway.watsonplatform.net/conversation/api',
username: process.env.CONVERSATION_USERNAME || 'username',
password: process.env.CONVERSATION_PASSWORD || 'password',
version: 'v1',
version_date: '2016-07-11'
});
var workspace = process.env.WORKSPACE_ID || 'workspaceId';
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === 'tokenDeVerificacaoFacebook') {
res.send(req.query['hub.challenge']);
}
res.send('Erro de validação no token.');
});
app.post('/webhook/', function (req, res) {
var text = null;
messaging_events = req.body.entry[0].messaging;
for (i = 0; i < messaging_events.length; i++) {
event = req.body.entry[0].messaging[i];
sender = event.sender.id;
if (event.message && event.message.text) {
text = event.message.text;
}else if (event.postback && !text) {
text = event.postback.payload;
}else{
break;
}
var params = {
input: text,
context: {"conversation_id": conversation_id}
}
var payload = {
workspace_id: workspace
};
if (params) {
if (params.input) {
params.input = params.input.replace("\n","");
payload.input = { "text": params.input };
}
if (params.context) {
payload.context = params.context;
}
}
callWatson(payload, sender);
}
res.sendStatus(200);
});
function callWatson(payload, sender) {
w_conversation.message(payload, function (err, convResults) {
if (err) {
return responseToRequest.send("Erro.");
}
if(convResults.context != null)
conversation_id = convResults.context.conversation_id;
if(convResults != null && convResults.output != null){
var i = 0;
while(i < convResults.output.text.length){
sendMessage(sender, convResults.output.text[i++]);
}
}
});
}
function sendMessage(sender, text_) {
text_ = text_.substring(0, 319);
messageData = { text: text_ };
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: token },
method: 'POST',
json: {
recipient: { id: sender },
message: messageData,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
};
var token = "tokenFB";
var host = (process.env.VCAP_APP_HOST || 'localhost');
var port = (process.env.VCAP_APP_PORT || 3000);
app.listen(port, host);
@brunotech
Copy link

EU coloco todos o código no app.js?

@brunotech
Copy link

Eu encontrei este tutorial para watson diálogo, eu gostaria de saber como faço para usá-lo em watson conversa, eu quero implementar apenas a parte da estrutura do messenger, para o meu watson conversation possa enivar imagens, botões
https://github.com/nheidloff/facebook-watson-bot/blob/master/app.js

function sendButtonMessage(recipient, text, buttons) {
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:recipient},
message: {
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":text,
"buttons": buttons
}
}
}
}
},
function(error, response, body) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
}

function sendGenericTemplateMessageWithTweets(recipient, author, imageUrl, title, url) {
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:recipient},
message: {
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements": [
{
"title":author,
"image_url":imageUrl,
"subtitle":title,
"buttons":[
{
"type":"web_url",
"url":url,
"title":"View Tweet"
}
]
}
]
}
}
}
}
},
function(error, response, body) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
}

No meu bot estou seguindo este tutorial

http://www.ibm.com/developerworks/br/cloud/library/criando-chat-bots-ibm-watson-pt2/index.html

https://github.com/nheidloff/facebook-watson-bot/blob/master/app.js

Eu quero colocar a função 1 app.js no meu bot, e como posso indicá-lo na caixa de diálogo do meu bot? Quero enviar fotos, vídeos, botões

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment