Skip to content

Commit

Permalink
added a 'list contexts' lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
agnescameron committed Jan 31, 2020
1 parent 61bb5e2 commit 2337e35
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ app.use(
)

app.post('/', express.json(), (req, res) => {
console.log('heyyyy')
const agent = new WebhookClient({ request: req, response: res })

function getWishFood() {
Expand Down
4 changes: 2 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class App extends Component {
"name": "truth_bot_setting",
"sessionId": uuid.v4(),
"projectId": "g191120-truth-bot-hluhsq",
"steps": [0, 1]
"steps": [0, 1, 2, 3, 4, 5]
},
{
"name": "truth_bot_answering",
"sessionId": uuid.v4(),
"projectId": "c200103-challenge-truth-bot-wc",
"steps": [2, 3]
"steps": [6]
}
];
this.botQueue = [];
Expand Down
18 changes: 16 additions & 2 deletions src/helpers/textProcessing.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ async function deleteAllContexts(bot) {
})
}

async function listContexts(bot) {
const response = await fetch(".netlify/functions/listContexts", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
bot: bot })
})

console.log(response);
}

async function createContext(context, lifespan, bot) {
const response = await fetch(".netlify/functions/createContext", {
method: 'POST',
Expand All @@ -51,10 +64,11 @@ async function createContext(context, lifespan, bot) {
}

async function chooseTruth(bot) {
deleteAllContexts(bot)
deleteAllContexts(bot);
let truth = truths[Math.floor(Math.random()*truths.length)];
const contextId = truth.context;
await createContext(contextId, 5, bot);
await listContexts(bot);

return truth.truth;
}
Expand Down Expand Up @@ -83,7 +97,7 @@ export const preProcessor = async (sent, bot) => {
}

//is this a truth challenge? NB should replace with button press
if(natural.LevenshteinDistance("truth", sent, {search: true}).distance < 3){
if(natural.LevenshteinDistance("truth", sent, {search: true}).distance < 1){
const output = await chooseTruth(bot);
return output;
}
Expand Down
35 changes: 35 additions & 0 deletions src/lambda/listContexts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fetch from 'node-fetch';
import { sessionClient } from './runSample';
import { contextsClient } from './createContext';
import dialogflow from 'dialogflow';
const botAuth = JSON.parse(process.env.master_bot);

exports.handler = async (event) => {
// "event" has information about the path, body, headers, etc. of the request
const request = JSON.parse(event.body);
console.log('got request', request)
const contextResponse = await listContexts(request);
return{
statusCode: 200,
body: contextResponse
}
}

async function listContexts(request){
//set sessionclient for this project
contextsClient.projectId = request.bot.projectId;
sessionClient.projectId = request.bot.projectId;

const sessionPath = sessionClient.sessionPath(request.bot.projectId, request.bot.sessionId);

const contextRequest = {
parent: sessionPath,
}

const [contextResponse] = await contextsClient.listContexts(contextRequest);
if(contextResponse !== undefined && contextResponse.length !== 0){
// var name = contextResponse[0].name.split("/").slice(-1)[0];
console.log('context set is', contextResponse)
}
return "contextResponse";
}
1 change: 1 addition & 0 deletions src/lambda/runSample.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ async function botRequest(request) {

const intentResponse = await sessionClient.detectIntent(intentRequest);
const intentResult = intentResponse[0].queryResult;
console.log(intentResult.intent.displayName);
return intentResult.fulfillmentText;
}

0 comments on commit 2337e35

Please sign in to comment.