8000 Aula 89 by xumes · Pull Request #23 · xumes/chatbot-react · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Aula 89 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const functions = require('firebase-functions');
const watson = require('watson-developer-cloud/assistant/v1')
require('dotenv').config()

const cors = require('cors')({ origin: true });

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions

Expand All @@ -18,18 +20,20 @@ const chatbot = new watson({
const workspace_id = process.env.WORKSPACE_ID;

exports.conversa = functions.https.onRequest((req, res) => {
let payload = {
workspace_id,
context: req.body.context || {},
input: req.body.input || {}
};

chatbot.message(payload, (err, data) => {
if (err) {
return res.status(err.code || 500).json(err);
}

return res.json(trataResposta(payload, data));
cors(req, res, () => {
let payload = {
workspace_id,
context: req.body.context || {},
input: req.body.input || {}
};

chatbot.message(payload, (err, data) => {
if (err) {
return res.status(err.code || 500).json(err);
}

return res.json(trataResposta(payload, data));
})
})
})

Expand Down
10000
1 change: 1 addition & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"logs": "firebase functions:log"
},
"dependencies": {
"cors": "^2.8.4",
"dotenv": "^6.0.0",
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.0.3",
Expand Down
11 changes: 7 additions & 4 deletions src/Componentes/chat/ChatMensagem.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { Component } from 'react'
import { InputGroup, InputGroupAddon, Input, Button } from 'reactstrap'
import {connect} from 'react-redux'
import { connect } from 'react-redux'

import {enviaMensagem} from './../../store/actions/chat'
import { enviaMensagem } from './../../store/actions/chat'
import { conversaWatson } from './../../store/actions/watson'

class ChatMensagem extends Component {
constructor(props) {
Expand All @@ -11,14 +12,15 @@ class ChatMensagem extends Component {
this.inputEnviaTexto = this.inputEnviaTexto.bind(this)
}

inputEnviaTexto(e){
inputEnviaTexto(e) {
if (e.keyCode === 13) {
console.log(e.target.value)
const mensagem = {
texto: e.target.value,
origem: 'user'
}
this.props.enviaTexto(mensagem)
this.props.conversaWatson(mensagem)
e.target.value = ''
}
}
Expand All @@ -40,7 +42,8 @@ class ChatMensagem extends Component {

const mapDispatchToProps = (dispatch) => {
return {
enviaTexto: (msg) => dispatch(enviaMensagem(msg))
enviaTexto: (msg) => dispatch(enviaMensagem(msg)),
conversaWatson: (msg) =>dispatch(conversaWatson(msg, ''))
}
}

Expand Down
2 chan 8000 ges: 1 addition & 1 deletion src/store/actions/watson.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const conversaWatson = ((mensagem, contexto) => {
//chama o backend do watson (Firebase cloud functions)
const url = 'https://us-central1-chatbot-em-react.cloudfunctions.net/conversa'
axios
.post(url, {mensagem, contexto})
.get(url)
.then((data) => dispatch(conversaWatsonSucess(data)))
.catch(() => dispatch(conversaWatsonError()))
}
Expand Down
12 changes: 8 additions & 4 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {createStore} from 'redux'
import {createStore, applyMiddleware, compose} from 'redux'
import thunk from 'redux-thunk'

import reducers from './reducers'

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const middlewares = [thunk]

const store = createStore(
reducers,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
reducers,composeEnhancers(
applyMiddleware(...middlewares)
));

export default store
0