Skip to content
Permalink
Browse files

fixed by eslint

  • Loading branch information...
webus committed Aug 8, 2017
1 parent 9450632 commit 13d0e09ddd208cec21dee3991b04b6d6437d0528
Showing with 153 additions and 134 deletions.
  1. +5 −0 .eslintignore
  2. +15 −0 .eslintrc
  3. +2 −2 index.js
  4. +14 −15 src/api-ai-webhook.js
  5. +117 −117 src/api-ai.js
@@ -0,0 +1,5 @@
/data
/doc
/coverage
_*.js
demo*.js
@@ -0,0 +1,15 @@
{
"extends": "airbnb",
"env": {
"node":true,
"mocha":true,
"es6": true,
"mongo":true
},
"rules": {
// disable requiring trailing commas
"comma-dangle": 0,
"max-len": ["error", {"code": 120, "ignoreComments": true, "ignoreUrls": true}],
"no-underscore-dangle": [2, { "allow": ["_id", "__get__", "__with__"] }]
}
}
@@ -1,4 +1,4 @@
"use strict";

require("dotenv").config();

require('dotenv').config();
module.exports = require('./src/botstack.js');
@@ -1,22 +1,21 @@
const db = require('./dynamodb.js');

module.exports = (req, res, next) => {
res.json({
speech: req.body.result.fulfillment.speech,
displayText: req.body.result.fulfillment.speech,
source: 'repost-bot'
});

res.json({
speech: req.body.result.fulfillment.speech,
displayText: req.body.result.fulfillment.speech,
source: 'repost-bot'
});
res.end();
console.log('===Received a database hook from API.ai');

res.end();
console.log("===Received a database hook from API.ai");
// add to db
if (req.body) {
db.logApiaiObject(req.body);
} else {
console.log('No body to put in DB');
}

//add to db
if(req.body) {
db.logApiaiObject(req.body);
} else {
console.log('No body to put in DB');
}

return next();
return next();
};
@@ -7,143 +7,143 @@ const sessionStore = require('./session.js')();
const log = require('./log.js');

const APIAI_ACCESS_TOKEN = process.env.APIAI_ACCESS_TOKEN;
const APIAI_LANG = "en";
const APIAI_LANG = 'en';
const apiAiService = apiai(APIAI_ACCESS_TOKEN);

async function backchatApiAiSync(response) {
if (process.env.BACKCHAT_APIAI_SYNC_URL) {
const reqData = {
url: process.env.BACKCHAT_APIAI_SYNC_URL,
resolveWithFullResponse: true,
method: 'POST',
json: response
};
try {
const result = await rp(reqData);
if (result.statusCode != 200) {
log.warn('Something wrong with BackChat endpoint', {
module: 'botstack:api-ai'
});
} else {
log.debug('Copy API.AI response to BackChat endpoint', {
module: 'botstack:api-ai'
});
}
} catch (e) {
log.error(e, {
module: 'botstack:api-ai'
});
throw e;
}
if (process.env.BACKCHAT_APIAI_SYNC_URL) {
const reqData = {
url: process.env.BACKCHAT_APIAI_SYNC_URL,
resolveWithFullResponse: true,
method: 'POST',
json: response
};
try {
const result = await rp(reqData);
if (result.statusCode != 200) {
log.warn('Something wrong with BackChat endpoint', {
module: 'botstack:api-ai'
});
} else {
log.debug('Copy API.AI response to BackChat endpoint', {
module: 'botstack:api-ai'
});
}
} catch (e) {
log.error(e, {
module: 'botstack:api-ai'
});
throw e;
}
}
}

function getApiAiResponse({ apiAiRequest, senderID, eventName, message, sessionID } = {
eventName: null, message: null
eventName: null, message: null
}) {
return new Promise((resolve, reject) => {
apiAiRequest.on('response', (response) => {
let logParams = {
module: "botstack:api-ai",
senderId: senderID,
sessionId: sessionID,
response
};

if (eventName) {
logParams.eventName = eventName;
}

if (message) {
logParams.message = message;
}

log.debug("API.AI responded", logParams);

backchatApiAiSync(response);

if (lodash.get(response, 'result')) {
log.debug("API.AI result", {
module: "botstack:api-ai",
senderId: senderID,
result: response.result
});

const responseText = lodash.get(response.result, 'fulfillment.speech');
const responseData = lodash.get(response.result, 'fulfillment.data');
const messages = lodash.get(response.result, 'fulfillment.messages');
const action = lodash.get(response.result, 'action');

if (lodash.get(responseData, 'facebook')) {
// FIXME: implement this type of messages
log.debug("Response as formatted message", {
module: "botstack:api-ai",
senderId: senderID
});
resolve(null);
} else if (!lodash.isEmpty(messages)) {
const returnData = {
messages,
response
};
resolve(returnData);
}
}
});

apiAiRequest.on('error', (error) => {
log.debug(error, {
module: "botstack:api-ai",
senderId: senderID
});
reject(error);
});
return new Promise((resolve, reject) => {
apiAiRequest.on('response', (response) => {
const logParams = {
module: 'botstack:api-ai',
senderId: senderID,
sessionId: sessionID,
response
};

apiAiRequest.end();
});
};
if (eventName) {
logParams.eventName = eventName;
}

async function processEvent(eventName, senderID) {
const sessionResult = await sessionStore.get(senderID);
const sessionID = sessionResult.sessionID;
if (message) {
logParams.message = message;
}

log.debug("Process event", {
module: "botstack:api-ai",
senderId: senderID,
eventName: eventName,
sessionId: sessionID
});
log.debug('API.AI responded', logParams);

const apiAiRequest = apiAiService.eventRequest({
name: eventName
},{
sessionId: sessionID
});
backchatApiAiSync(response);

const result = await getApiAiResponse({ apiAiRequest, senderID, eventName, sessionID });
return result;
}
if (lodash.get(response, 'result')) {
log.debug('API.AI result', {
module: 'botstack:api-ai',
senderId: senderID,
result: response.result
});

async function processTextMessage(message, senderID) {
const sessionResult = await sessionStore.get(senderID);
const sessionID = sessionResult.sessionID;
const responseText = lodash.get(response.result, 'fulfillment.speech');
const responseData = lodash.get(response.result, 'fulfillment.data');
const messages = lodash.get(response.result, 'fulfillment.messages');
const action = lodash.get(response.result, 'action');

log.debug("Process text message", {
module: "botstack:api-ai",
senderId: senderID,
message: message,
sessionId: sessionID
if (lodash.get(responseData, 'facebook')) {
// FIXME: implement this type of messages
log.debug('Response as formatted message', {
module: 'botstack:api-ai',
senderId: senderID
});
resolve(null);
} else if (!lodash.isEmpty(messages)) {
const returnData = {
messages,
response
};
resolve(returnData);
}
}
});

const apiAiRequest = apiAiService.textRequest(message, {
sessionId: sessionID
apiAiRequest.on('error', (error) => {
log.debug(error, {
module: 'botstack:api-ai',
senderId: senderID
});
reject(error);
});

const result = await getApiAiResponse({ apiAiRequest, senderID, message, sessionID });
return result;
apiAiRequest.end();
});
}

async function processEvent(eventName, senderID) {
const sessionResult = await sessionStore.get(senderID);
const sessionID = sessionResult.sessionID;

log.debug('Process event', {
module: 'botstack:api-ai',
senderId: senderID,
eventName,
sessionId: sessionID
});

const apiAiRequest = apiAiService.eventRequest({
name: eventName
}, {
sessionId: sessionID
});

const result = await getApiAiResponse({ apiAiRequest, senderID, eventName, sessionID });
return result;
}

async function processTextMessage(message, senderID) {
const sessionResult = await sessionStore.get(senderID);
const sessionID = sessionResult.sessionID;

log.debug('Process text message', {
module: 'botstack:api-ai',
senderId: senderID,
message,
sessionId: sessionID
});

const apiAiRequest = apiAiService.textRequest(message, {
sessionId: sessionID
});

const result = await getApiAiResponse({ apiAiRequest, senderID, message, sessionID });
return result;
}

module.exports = {
processTextMessage,
processEvent
processTextMessage,
processEvent
};

0 comments on commit 13d0e09

Please sign in to comment.
You can’t perform that action at this time.