Skip to content

Commit

Permalink
wow finally the fucker works for reasons far too complex to explain
Browse files Browse the repository at this point in the history
  • Loading branch information
agnescameron committed Jan 22, 2020
1 parent 33d4e1c commit fb86c31
Show file tree
Hide file tree
Showing 10 changed files with 3,867 additions and 2,445 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# production
/build
/functions

# misc
.DS_Store
Expand Down
4 changes: 4 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[build]
functions = "functions/"
publish = "build/"
command = "npm run build"
6,033 changes: 3,767 additions & 2,266 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@
"name": "bot_test",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:3001",
"proxy": "http://localhost:9000",
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"http-proxy-middleware": "^0.20.0",
"netlify-lambda": "^1.6.3",
"node-fetch": "^2.6.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-entypo": "^1.3.0",
"react-scripts": "3.3.0",
"serialize-javascript": "^2.1.2"
"request": "^2.79.0",
"serialize-javascript": "^2.1.2",
"webpack": "^4.41.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"start": "npm-run-all --parallel start:app start:server",
"start:app": "react-scripts start",
"build:app": "react-scripts build",
"build": "run-p build:**",
"test": "react-scripts test",
"eject": "react-scripts eject",
"server": "nodemon server",
"dev": "run-p server start"
"dev": "run-p server start",
"start:server": "netlify-lambda serve --config ./webpack.functions.js ./src/lambda",
"build:lambda": "netlify-lambda build --config ./webpack.functions.js src/lambda",
"serve:lambda": "netlify-lambda serve --config ./webpack.functions.js src"
},
"eslintConfig": {
"extends": "react-app"
Expand All @@ -38,7 +48,7 @@
},
"devDependencies": {
"body-parser": "^1.19.0",
"dialogflow": "^1.0.0",
"dialogflow": "^1.1.1",
"express": "^4.17.1",
"express-pino-logger": "^4.0.0",
"node-env-run": "^3.0.2",
Expand Down
23 changes: 23 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const proxy = require('http-proxy-middleware')
const Bundler = require('parcel-bundler')
const express = require('express')

const bundler = new Bundler('src/site/index.html', {
cache: false
})

const app = express()

app.use(
'/.netlify/functions',
proxy({
target: 'http://localhost:9000',
pathRewrite: {
'^/\.netlify/functions':''
}
})
)

app.use(bundler.middleware())

app.listen(Number(process.env.PORT || 1234))
126 changes: 0 additions & 126 deletions server/index.js

This file was deleted.

46 changes: 0 additions & 46 deletions server/lib/truths.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/ReactBotUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ReactBotUI extends Component {
// append user text
this.appendMessage(text, true);

const response = await fetch('/api/botRequest', {
const response = await fetch(".netlify/functions/botRequest", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
52 changes: 52 additions & 0 deletions src/lambda/botRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import fetch from 'node-fetch';
import dialogflow from 'dialogflow';
const uuid = require('uuid');
let truthbotPrivateKey = JSON.parse(process.env.truthbot_private_key).private_key;
let truthbotClientEmail = process.env.truthbot_client_email;
let truthbotProjectId = process.env.truthbot_project_id;

exports.handler = async (event) => {
// "event" has information about the path, body, headers, etc. of the request
console.log('event', event)
const response = await botRequest("hello");
// The "callback" ends the execution of the function and returns a response back to the caller
return {
statusCode: 200,
body: response
}
}


const sessionId = uuid.v4();

// Create a new session
const sessionClient = new dialogflow.SessionsClient({
projectId: truthbotProjectId,
credentials: {
private_key: truthbotPrivateKey,
client_email: truthbotClientEmail
}
});

const sessionPath = sessionClient.sessionPath(truthbotProjectId, sessionId);
console.log('sessionpath is', sessionClient);


async function botRequest(userString) {
// The text query request.
// Send request and log result
// The text query request.
const intentRequest = {
session: sessionPath,
queryInput: {
text: {
text: userString,
languageCode: 'en-US',
},
},
};

const intentResponse = await sessionClient.detectIntent(intentRequest);
const intentResult = intentResponse[0].queryResult;
return intentResult.fulfillmentText;
}
3 changes: 3 additions & 0 deletions webpack.functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
externals: { dialogflow: 'dialogflow' }
};

0 comments on commit fb86c31

Please sign in to comment.