65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
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 { UserDto } from 'src/identity-and-account/users-management/user.dto';
|
|
import { Message } from 'src/chatbot/dtos/dialog-message.dto';
|
|
|
|
@Injectable()
|
|
export class ChatbotService {
|
|
constructor(private readonly httpService: HttpService) { }
|
|
sessionId: string;
|
|
|
|
async pingExternalApi(body: UserMessageDto): Promise<Message> {
|
|
const response = await firstValueFrom(
|
|
this.httpService.post(
|
|
'https://n8nai.targo.ca/webhook/984c578e-59c7-4ca1-97f8-e225fd2acf01',
|
|
{ userInput: body.userInput, userId: this.sessionId },
|
|
),
|
|
);
|
|
|
|
console.log('chatbot response: ', response);
|
|
|
|
const cleanText =
|
|
Array.isArray(response.data) && response.data[0]?.output
|
|
? response.data[0].output
|
|
: JSON.stringify(response);
|
|
|
|
return {
|
|
text: cleanText,
|
|
sent: false,
|
|
};
|
|
}
|
|
|
|
async sendPageContext(body: PageContextDto) {
|
|
const response = await firstValueFrom(
|
|
this.httpService.post(
|
|
'https://n8nai.targo.ca/webhook/984c578e-59c7-4ca1-97f8-e225fd2acf01',
|
|
{ features: body, userId: this.sessionId, userInput: '' },
|
|
),
|
|
);
|
|
return response.data;
|
|
}
|
|
|
|
// Will have to modify later on to accomodate newer versions of User Auth/User type Structure
|
|
async sendUserContext(user_email: string) {
|
|
if (!this.sessionId) {
|
|
this.sessionId = 'SessionId = ' + user_email;
|
|
}
|
|
|
|
const response = await firstValueFrom(
|
|
this.httpService.post(
|
|
'https://n8nai.targo.ca/webhook/984c578e-59c7-4ca1-97f8-e225fd2acf01',
|
|
{
|
|
userId: this.sessionId,
|
|
userInput: '',
|
|
features: '',
|
|
},
|
|
{ headers: { 'Content-Tyoe': 'application/json' } },
|
|
),
|
|
);
|
|
return response.data;
|
|
}
|
|
}
|