sdy

create actions

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