sdy

create actions

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 });
}
};
export const INPUT_SUCCESS = "INPUT_SUCCESS";
export const INPUT_FAIL = "INPUT_FAIL";
export const SESSION_SUCCESS = "SESSION_SUCCESS";
export const SESSION_FAIL = "SESSION_FAIL";
export const MESSAGE_SUCCESS = "MESSAGE_SUCCESS";
export const MESSAGE_FAIL = "MESSAGE_FAIL";