-
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.
Co-authored-by: IRADUKUNDA SANGWA CEDRIC <110623461+Dawaic6@users.noreply.github.com> user should make chat with chatbot
- Loading branch information
1 parent
4232bbb
commit dcdf53a
Showing
9 changed files
with
125 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Request, Response } from 'express'; | ||
import * as chatbotService from '../service/chatbotService'; | ||
import errorHandler from '../middlewares/errorHandler'; | ||
|
||
export const getChatResponse = errorHandler( | ||
async ( | ||
req: Request, | ||
res: Response | ||
): Promise<Response<Record<string, unknown>> | undefined> => { | ||
const { message } = req.body; | ||
const userId = req.user?.id; | ||
const response = await chatbotService.processMessage(userId, message); | ||
return res.status(200).json({ message: response }); | ||
} | ||
); | ||
|
||
export const getChatHistory = errorHandler( | ||
async ( | ||
req: Request, | ||
res: Response | ||
): Promise<Response<Record<string, unknown>> | undefined> => { | ||
const userId = req.user?.id; | ||
const history = await chatbotService.getChatHistory(userId); | ||
return res.status(200).json({ history }); | ||
} | ||
); |
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,20 @@ | ||
import { Entity,PrimaryGeneratedColumn,Column,ManyToOne,CreateDateColumn } from 'typeorm'; | ||
import User from './userModel'; | ||
|
||
@Entity() | ||
export default class chat{ | ||
@PrimaryGeneratedColumn() | ||
id:number; | ||
|
||
@ManyToOne(()=>User) | ||
user: User; | ||
|
||
@Column() | ||
message:string; | ||
|
||
@Column() | ||
response:string; | ||
|
||
@CreateDateColumn() | ||
createdAt: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
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 { Router } from 'express'; | ||
import { getChatResponse, getChatHistory } from '../controller/chatbotController'; | ||
import { IsLoggedIn } from '../middlewares/isLoggedIn'; | ||
import { checkRole } from '../middlewares/authorize'; | ||
|
||
const router = Router(); | ||
|
||
router.post('/chat', IsLoggedIn,checkRole(['Buyer']), getChatResponse); | ||
router.get('/chat/history', IsLoggedIn,checkRole(['Buyer']), getChatHistory); | ||
|
||
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
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,32 @@ | ||
import Chat from '../database/models/chatbotModel'; | ||
import User from '../database/models/userModel'; | ||
import dbConnection from '../database'; | ||
import { analyzeMessege, generateResponse } from '../utilis/nlp'; | ||
|
||
export const processMessage = async (userId: number, message: string): Promise<string> => { | ||
const userRepo = dbConnection.getRepository(User); | ||
const chatRepo = dbConnection.getRepository(Chat); | ||
|
||
const user = await userRepo.findOne({ | ||
where:{ id:userId } | ||
}); | ||
if (!user) { | ||
throw new Error('User not found'); | ||
} | ||
|
||
const analyzedMessage = analyzeMessege(message); | ||
const response = await generateResponse(analyzedMessage); | ||
|
||
const chat = new Chat(); | ||
chat.user = user; | ||
chat.message = message; | ||
chat.response = response; | ||
await chatRepo.save(chat); | ||
|
||
return response; | ||
}; | ||
|
||
export const getChatHistory = async (userId: number): Promise<Chat[]> => { | ||
const chatRepo = dbConnection.getRepository(Chat); | ||
return chatRepo.find({ where: { user: { id: userId } }, order: { createdAt: 'DESC' } }); | ||
}; |
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,19 @@ | ||
export const analyzeMessege = (message: string): string => { | ||
return message.toLowerCase() | ||
}; | ||
|
||
export const generateResponse= async(message:string):Promise<string>=>{ | ||
if (message.includes('product')) { | ||
return 'We sell various products such as electronics, clothing, and home appliances.'; | ||
} | ||
if (message.includes('recommend')) { | ||
return 'Based on your history, we recommend our latest smartphones and laptops.'; | ||
} | ||
if (message.includes('delivery')) { | ||
return 'Our delivery process usually takes 3-5 business days.'; | ||
} | ||
if (message.includes('help')) { | ||
return 'How can I assist you with navigating our platform or completing an order?'; | ||
} | ||
return 'I am sorry, I did not understand that. Could you please rephrase?'; | ||
} |
This file was deleted.
Oops, something went wrong.