Skip to content
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

Add integration with dialogflow #381

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f769273
:feat: adding dialogflow support in the backend
ricardoapaes Feb 20, 2022
09e815f
:fix: Fixing table Dialogflow name
ricardoapaes Feb 20, 2022
4ac88ce
:feat: Adding routes in backend to enable/disable `useQueue` in `useD…
ricardoapaes Feb 21, 2022
7c0364c
:feat: Adding switch in ticket detail to enable/disable dialogflow fo…
ricardoapaes Feb 21, 2022
4984e0e
:sparkles: Send delayed multi-line dialogflow response
ricardoapaes Feb 22, 2022
6fad165
:hammer: Removing Code Smells
ricardoapaes Feb 22, 2022
0fd973f
:sparkles: Add button in Frontend to enable/disable Contact Queues
ricardoapaes Mar 1, 2022
08767b7
:sparkles: Show the dialogflow key only when it is configured for tha…
ricardoapaes Mar 1, 2022
ce98ddc
:bug: Removing bugs
ricardoapaes Mar 1, 2022
19faa22
Merge branch 'master' into feature/dialogflow
ricardoapaes Mar 14, 2022
62c0375
Merge branch 'master' into feature/dialogflow
ricardoapaes Aug 19, 2022
f23611c
Merge branch 'canove:master' into feature/dialogflow
ricardoapaes Aug 19, 2022
c913419
:feat: adding dialogflow integration configuration in the frontend an…
izaias-nascimento Jun 6, 2022
e614eaa
:fix: more than one dialogflow answer
izaias-nascimento Jun 6, 2022
7e62be6
:fix: Reference action in reducer
izaias-nascimento Jun 13, 2022
a4011ec
:feat: Add Dialogflow field to register\change queues in Frontend
izaias-nascimento Jun 14, 2022
1a5924d
:feat: Add field Use Dialogflow on add\update contact
izaias-nascimento Jun 21, 2022
58ccb32
✨ Remove `dialogFlow` requirement in queue register
ricardoapaes Aug 22, 2022
33a2a93
Merge pull request #8 from ricardoapaes/feature/dialogflow-izaias.nas…
ricardoapaes Aug 22, 2022
2af5659
✨ Fixing sonarqube bugs
ricardoapaes Aug 22, 2022
42acf5e
✨ Fixing sonarqube bugs
ricardoapaes Aug 22, 2022
2af94a3
✨ Fixing sonarqube bugs
ricardoapaes Aug 22, 2022
6df6b4e
✨ Fixing sonarqube bugs
ricardoapaes Aug 23, 2022
fb4c012
✨ Fixing sonarqube code smells
ricardoapaes Aug 23, 2022
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
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"start": "nodemon dist/server.js",
"start": "nodemon --ignore dialogflow/ dist/server.js",
"dev:server": "ts-node-dev --respawn --transpile-only --ignore node_modules src/server.ts",
"pretest": "NODE_ENV=test sequelize db:migrate && NODE_ENV=test sequelize db:seed:all",
"test": "NODE_ENV=test jest",
Expand All @@ -15,8 +15,10 @@
"author": "",
"license": "MIT",
"dependencies": {
"@google-cloud/dialogflow": "^4.6.0",
"@sentry/node": "^5.29.2",
"@types/pino": "^6.3.4",
"actions-on-google": "^3.0.0",
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
Expand Down
40 changes: 40 additions & 0 deletions backend/src/controllers/ContactController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
import AppError from "../errors/AppError";
import GetContactService from "../services/ContactServices/GetContactService";
import ToggleUseQueuesContactService from "../services/ContactServices/ToggleUseQueuesContactService";
import ToggleUseDialogflowContactService from "../services/ContactServices/ToggleUseDialogflowContactService";

type IndexQuery = {
searchParam: string;
Expand All @@ -32,6 +34,7 @@ interface ContactData {
name: string;
number: string;
email?: string;
useDialogflow: boolean;
extraInfo?: ExtraInfo[];
}

Expand Down Expand Up @@ -83,11 +86,14 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
let number = validNumber
let email = newContact.email
let extraInfo = newContact.extraInfo
let useDialogflow = newContact.useDialogflow


const contact = await CreateContactService({
name,
number,
email,
useDialogflow,
extraInfo,
profilePicUrl
});
Expand Down Expand Up @@ -144,6 +150,40 @@ export const update = async (
return res.status(200).json(contact);
};

export const toggleUseQueue = async (
req: Request,
res: Response
): Promise<Response> => {
const { contactId } = req.params;

const contact = await ToggleUseQueuesContactService({ contactId });

const io = getIO();
io.emit("contact", {
action: "update",
contact
});

return res.status(200).json(contact);
};

export const toggleUseDialogflow = async (
req: Request,
res: Response
): Promise<Response> => {
const { contactId } = req.params;

const contact = await ToggleUseDialogflowContactService({ contactId });

const io = getIO();
io.emit("contact", {
action: "update",
contact
});

return res.status(200).json(contact);
};

export const remove = async (
req: Request,
res: Response
Expand Down
85 changes: 85 additions & 0 deletions backend/src/controllers/DialogflowController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Request, Response } from "express";
import { getIO } from "../libs/socket";
import CreateDialogflowService from "../services/DialogflowServices/CreateDialogflowService";
import DeleteDialogflowService from "../services/DialogflowServices/DeleteDialogflowService";
import ListDialogflowsService from "../services/DialogflowServices/ListDialogflowService";
import ShowDialogflowService from "../services/DialogflowServices/ShowDialogflowService";
import TestSessionDialogflowService from "../services/DialogflowServices/TestSessionDialogflowService";
import UpdateDialogflowService from "../services/DialogflowServices/UpdateDialogflowService";

export const index = async (req: Request, res: Response): Promise<Response> => {
const dialogflows = await ListDialogflowsService();

return res.status(200).json(dialogflows);
};

export const store = async (req: Request, res: Response): Promise<Response> => {
const { name, projectName, jsonContent, language } = req.body;

const dialogflow = await CreateDialogflowService({ name, projectName, jsonContent, language });

const io = getIO();
io.emit("dialogflow", {
action: "update",
dialogflow
});

return res.status(200).json(dialogflow);
};

export const show = async (req: Request, res: Response): Promise<Response> => {
const { dialogflowId } = req.params;

const dialogflow = await ShowDialogflowService(dialogflowId);

return res.status(200).json(dialogflow);
};

export const update = async (
req: Request,
res: Response
): Promise<Response> => {
const { dialogflowId } = req.params;
const dialogflowData = req.body;

const dialogflow = await UpdateDialogflowService({dialogflowData, dialogflowId });

const io = getIO();
io.emit("dialogflow", {
action: "update",
dialogflow
});

return res.status(201).json(dialogflow);
};

export const remove = async (
req: Request,
res: Response
): Promise<Response> => {
const { dialogflowId } = req.params;

await DeleteDialogflowService(dialogflowId);

const io = getIO();
io.emit("dialogflow", {
action: "delete",
dialogflowId: +dialogflowId
});

return res.status(200).send();
};

export const testSession = async (req: Request, res: Response): Promise<Response> => {
const { projectName, jsonContent, language } = req.body;

const response = await TestSessionDialogflowService({ projectName, jsonContent, language });

const io = getIO();
io.emit("dialogflow", {
action: "testSession",
response
});

return res.status(200).json(response);
};
2 changes: 2 additions & 0 deletions backend/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Queue from "../models/Queue";
import WhatsappQueue from "../models/WhatsappQueue";
import UserQueue from "../models/UserQueue";
import QuickAnswer from "../models/QuickAnswer";
import Dialogflow from "../models/Dialogflow";

// eslint-disable-next-line
const dbConfig = require("../config/database");
Expand All @@ -25,6 +26,7 @@ const models = [
Whatsapp,
ContactCustomField,
Setting,
Dialogflow,
Queue,
WhatsappQueue,
UserQueue,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { QueryInterface, DataTypes } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.createTable("Dialogflows", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
projectName: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
jsonContent: {
type: DataTypes.TEXT,
allowNull: false,
},
language: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false
}
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.dropTable("Dialogflows");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { QueryInterface, DataTypes } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Queues", "dialogflowId", {
type: DataTypes.INTEGER,
references: { model: "Dialogflows", key: "id" },
onUpdate: "CASCADE",
onDelete: "SET NULL"
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Queues", "dialogflowId");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { QueryInterface, DataTypes } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Contacts", "useDialogflow", {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Contacts", "useDialogflow");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { QueryInterface, DataTypes } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Contacts", "useQueues", {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Contacts", "useQueues");
}
};
8 changes: 8 additions & 0 deletions backend/src/models/Contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class Contact extends Model<Contact> {
@Column
isGroup: boolean;

@Default(true)
@Column
useQueues: boolean;

@Default(true)
@Column
useDialogflow: boolean;

@CreatedAt
createdAt: Date;

Expand Down
45 changes: 45 additions & 0 deletions backend/src/models/Dialogflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
Table,
Column,
CreatedAt,
UpdatedAt,
Model,
DataType,
PrimaryKey,
HasMany,
AutoIncrement
} from "sequelize-typescript";
import Queue from "./Queue";

@Table
class Dialogflow extends Model<Dialogflow> {
@PrimaryKey
@AutoIncrement
@Column
id: number;

@Column(DataType.TEXT)
name: string;

@Column(DataType.TEXT)
projectName: string;

@Column(DataType.TEXT)
jsonContent: string;

@Column(DataType.TEXT)
language: string;

@CreatedAt
@Column(DataType.DATE(6))
createdAt: Date;

@UpdatedAt
@Column(DataType.DATE(6))
updatedAt: Date;

@HasMany(() => Queue)
queues: Queue[]
}

export default Dialogflow;
12 changes: 11 additions & 1 deletion backend/src/models/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import {
AutoIncrement,
AllowNull,
Unique,
BelongsToMany
BelongsToMany,
BelongsTo,
ForeignKey
} from "sequelize-typescript";
import User from "./User";
import UserQueue from "./UserQueue";

import Whatsapp from "./Whatsapp";
import WhatsappQueue from "./WhatsappQueue";
import Dialogflow from "./Dialogflow";

@Table
class Queue extends Model<Queue> {
Expand All @@ -36,6 +39,13 @@ class Queue extends Model<Queue> {
@Column
greetingMessage: string;

@ForeignKey(() => Dialogflow)
@Column
dialogflowId: number;

@BelongsTo(() => Dialogflow)
dialogflow: Dialogflow;

@CreatedAt
createdAt: Date;

Expand Down
4 changes: 4 additions & 0 deletions backend/src/routes/contactRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ contactRoutes.post("/contact", isAuth, ContactController.getContact);

contactRoutes.put("/contacts/:contactId", isAuth, ContactController.update);

contactRoutes.put("/contacts/toggleUseQueues/:contactId", isAuth, ContactController.toggleUseQueue);

contactRoutes.put("/contacts/toggleUseDialogflow/:contactId", isAuth, ContactController.toggleUseDialogflow);

contactRoutes.delete("/contacts/:contactId", isAuth, ContactController.remove);

export default contactRoutes;
Loading