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