forked from FlowiseAI/Flowise
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Collect contact information from users inside the chatbot (F…
…lowiseAI#1948) * Add leads settings to chatflow configuration * Add leads tab to chatflow configuration with options for lead capture * Add database entity and migrations for leads * Add endpoint for adding and fetching leads * Show lead capture form in UI chat window when enabled * Add view leads dialog * Make export leads functional * Add input for configuring message on successful lead capture * Add migrations for adding lead email in chat message if available * show lead email in view messages * ui touch up * Remove unused code and update how lead email is shown in view messages dialog * Fix lead not getting saved * Disable input when lead form is shown and save lead info to localstorage * Fix lead capture form not working * disabled lead save button until at least one form field is turned on, get rid of local storage _LEAD * add leads API to as whitelist public endpoint * Send leadEmail in internal chat inputs * Fix condition for disabling input field and related buttons when lead is enabled/disabled and when lead is saved * update leads ui * update error message and alter table add column sqlite migration --------- Co-authored-by: Henry <[email protected]>
- Loading branch information
1 parent
adea2f0
commit db452cd
Showing
31 changed files
with
979 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Request, Response, NextFunction } from 'express' | ||
import leadsService from '../../services/leads' | ||
import { StatusCodes } from 'http-status-codes' | ||
import { InternalFlowiseError } from '../../errors/internalFlowiseError' | ||
|
||
const getAllLeadsForChatflow = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
if (typeof req.params.id === 'undefined' || req.params.id === '') { | ||
throw new InternalFlowiseError( | ||
StatusCodes.PRECONDITION_FAILED, | ||
`Error: leadsController.getAllLeadsForChatflow - id not provided!` | ||
) | ||
} | ||
const chatflowid = req.params.id | ||
const apiResponse = await leadsService.getAllLeads(chatflowid) | ||
return res.json(apiResponse) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} | ||
|
||
const createLeadInChatflow = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
if (typeof req.body === 'undefined' || req.body === '') { | ||
throw new InternalFlowiseError( | ||
StatusCodes.PRECONDITION_FAILED, | ||
`Error: leadsController.createLeadInChatflow - body not provided!` | ||
) | ||
} | ||
const apiResponse = await leadsService.createLead(req.body) | ||
return res.json(apiResponse) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} | ||
|
||
export default { | ||
createLeadInChatflow, | ||
getAllLeadsForChatflow | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint-disable */ | ||
import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm' | ||
import { ILead } from '../../Interface' | ||
|
||
@Entity() | ||
export class Lead implements ILead { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string | ||
|
||
@Column() | ||
name?: string | ||
|
||
@Column() | ||
email?: string | ||
|
||
@Column() | ||
phone?: string | ||
|
||
@Column() | ||
chatflowid: string | ||
|
||
@Column() | ||
chatId: string | ||
|
||
@CreateDateColumn() | ||
createdDate: Date | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/server/src/database/migrations/mysql/1710832127079-AddLead.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class AddLead1710832127079 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE IF NOT EXISTS \`lead\` ( | ||
\`id\` varchar(36) NOT NULL, | ||
\`chatflowid\` varchar(255) NOT NULL, | ||
\`chatId\` varchar(255) NOT NULL, | ||
\`name\` text, | ||
\`email\` text, | ||
\`phone\` text, | ||
\`createdDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), | ||
PRIMARY KEY (\`id\`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;` | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE lead`) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/server/src/database/migrations/mysql/1711538023578-AddLeadToChatMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class AddLeadToChatMessage1711538023578 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
const columnExists = await queryRunner.hasColumn('chat_message', 'leadEmail') | ||
if (!columnExists) queryRunner.query(`ALTER TABLE \`chat_message\` ADD COLUMN \`leadEmail\` TEXT;`) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE \`chat_message\` DROP COLUMN \`leadEmail\`;`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/server/src/database/migrations/postgres/1710832137905-AddLead.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class AddLead1710832137905 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE IF NOT EXISTS lead ( | ||
id uuid NOT NULL DEFAULT uuid_generate_v4(), | ||
"chatflowid" varchar NOT NULL, | ||
"chatId" varchar NOT NULL, | ||
"name" text, | ||
"email" text, | ||
"phone" text, | ||
"createdDate" timestamp NOT NULL DEFAULT now(), | ||
CONSTRAINT "PK_98419043dd704f54-9830ab78f0" PRIMARY KEY (id) | ||
);` | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE lead`) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/server/src/database/migrations/postgres/1711538016098-AddLeadToChatMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class AddLeadToChatMessage1711538016098 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "chat_message" ADD COLUMN IF NOT EXISTS "leadEmail" TEXT;`) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "chat_message" DROP COLUMN "leadEmail";`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/server/src/database/migrations/sqlite/1710832117612-AddLead.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class AddLead1710832117612 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE IF NOT EXISTS "lead" ("id" varchar PRIMARY KEY NOT NULL, "chatflowid" varchar NOT NULL, "chatId" varchar NOT NULL, "name" text, "email" text, "phone" text, "createdDate" datetime NOT NULL DEFAULT (datetime('now')));` | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE IF EXISTS "lead";`) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/server/src/database/migrations/sqlite/1711537986113-AddLeadToChatMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class AddLeadToChatMessage1711537986113 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "chat_message" ADD COLUMN "leadEmail" TEXT;`) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "chat_message" DROP COLUMN "leadEmail";`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import express from 'express' | ||
import leadsController from '../../controllers/leads' | ||
const router = express.Router() | ||
|
||
// CREATE | ||
router.post('/', leadsController.createLeadInChatflow) | ||
|
||
// READ | ||
router.get(['/', '/:id'], leadsController.getAllLeadsForChatflow) | ||
|
||
export default router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { v4 as uuidv4 } from 'uuid' | ||
import { StatusCodes } from 'http-status-codes' | ||
import { getRunningExpressApp } from '../../utils/getRunningExpressApp' | ||
import { Lead } from '../../database/entities/Lead' | ||
import { ILead } from '../../Interface' | ||
import { InternalFlowiseError } from '../../errors/internalFlowiseError' | ||
import { getErrorMessage } from '../../errors/utils' | ||
|
||
const getAllLeads = async (chatflowid: string) => { | ||
try { | ||
const appServer = getRunningExpressApp() | ||
const dbResponse = await appServer.AppDataSource.getRepository(Lead).find({ | ||
where: { | ||
chatflowid | ||
} | ||
}) | ||
return dbResponse | ||
} catch (error) { | ||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: leadsService.getAllLeads - ${getErrorMessage(error)}`) | ||
} | ||
} | ||
|
||
const createLead = async (body: Partial<ILead>) => { | ||
try { | ||
const chatId = body.chatId ?? uuidv4() | ||
|
||
const newLead = new Lead() | ||
Object.assign(newLead, body) | ||
Object.assign(newLead, { chatId }) | ||
|
||
const appServer = getRunningExpressApp() | ||
const lead = appServer.AppDataSource.getRepository(Lead).create(newLead) | ||
const dbResponse = await appServer.AppDataSource.getRepository(Lead).save(lead) | ||
return dbResponse | ||
} catch (error) { | ||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: leadsService.createLead - ${getErrorMessage(error)}`) | ||
} | ||
} | ||
|
||
export default { | ||
createLead, | ||
getAllLeads | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import client from './client' | ||
|
||
const getLeads = (id) => client.get(`/leads/${id}`) | ||
const addLead = (body) => client.post(`/leads/`, body) | ||
|
||
export default { | ||
getLeads, | ||
addLead | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.