refactor(chatbot): better handling of potential errors
This commit is contained in:
parent
b7cf7d7208
commit
043bc91a56
|
|
@ -4,7 +4,11 @@ import { HttpModule } from '@nestjs/axios';
|
|||
import { ChatbotService } from 'src/chatbot/chatbot.service';
|
||||
|
||||
@Module({
|
||||
imports: [HttpModule],
|
||||
imports: [
|
||||
HttpModule.register({
|
||||
timeout: 5 * 60 * 1000, // 5 minutes in milliseconds
|
||||
})
|
||||
],
|
||||
controllers: [ChatbotController],
|
||||
providers: [ChatbotService],
|
||||
exports: [],
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ import { Injectable } from '@nestjs/common';
|
|||
import { UserMessageDto } from 'src/chatbot/dtos/user-message.dto';
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import { PageContextDto } from 'src/chatbot/dtos/page-context.dto';
|
||||
import { Message } from 'src/chatbot/dtos/dialog-message.dto';
|
||||
import { AxiosError, isAxiosError } from 'axios';
|
||||
|
||||
@Injectable()
|
||||
export class ChatbotService {
|
||||
|
|
@ -11,15 +11,24 @@ export class ChatbotService {
|
|||
sessionId: string = 'testing';
|
||||
|
||||
async pingExternalApi(body: UserMessageDto, email: string): Promise<Message> {
|
||||
const { data } = await firstValueFrom(this.httpService.post(
|
||||
'https://n8nai.targo.ca/webhook/chatty-Mcbot',
|
||||
{ userInput: body.userInput, userId: email, sessionId: this.sessionId, pageContext: body.pageContext ?? undefined }
|
||||
));
|
||||
try {
|
||||
const { data } = await firstValueFrom(this.httpService.post(
|
||||
'https://n8nai.targo.ca/webhook/chatty-Mcbot',
|
||||
{ userInput: body.userInput, userId: email, sessionId: this.sessionId, pageContext: body.pageContext ?? undefined }
|
||||
));
|
||||
|
||||
return {
|
||||
text: data[0].output,
|
||||
sent: false,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
return {
|
||||
text: data[0].output,
|
||||
sent: false,
|
||||
};
|
||||
return {
|
||||
text: 'chatbot.error.GENERIC_RESPONSE_ERROR',
|
||||
sent: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// async sendPageContext(body: PageContextDto, email: string) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user