action_watson.js
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {
INPUT_SUCCESS,
INPUT_FAIL,
SESSION_SUCCESS,
SESSION_FAIL,
MESSAGE_SUCCESS,
MESSAGE_FAIL,
} from "./types";
import axios from "axios";
export const userMessage = (message) => async (dispatch) => {
try {
dispatch({ type: INPUT_SUCCESS, payload: message });
} catch (err) {
dispatch({ type: INPUT_FAIL });
}
};
export const createSession = () => async (dispatch) => {
try {
const res = await axios.get("/api/watson/session");
dispatch({ type: SESSION_SUCCESS, payload: res.data });
} catch (err) {
dispatch({ type: SESSION_FAIL });
}
};
export const sendMessage = (message) => async (dispatch) => {
try {
const body = { input: message };
const res = await axios.post("/api/watson/message", body);
// bot message, bot options
console.log("res : ", res);
const generic = res.data.output.generic;
let botMsg = '', botOptions = [];
generic.map((e) => {
if (e.response_type === 'text') {
botMsg = e.text;
} else if (e.response_type === 'option') {
botOptions = e.options;
}
})
dispatch({
type: MESSAGE_SUCCESS,
payload: generic,
botMsg: botMsg,
botLabels: botOptions
});
} catch (err) {
dispatch({ type: MESSAGE_FAIL });
}
};