UNPKG

n8n-nodes-yourgpt

Version:

Integrate YourGPT AI chatbot node into your n8n workflows

135 lines 5.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.YourGPT = void 0; const n8n_workflow_1 = require("n8n-workflow"); class YourGPT { constructor() { this.description = { displayName: 'YourGPT Chatbot', name: 'yourGPT', icon: 'file:yourgpt.png', group: ['transform'], version: 1, description: 'Integrate YourGPT Chatbot into your n8n workflows', defaults: { name: 'YourGPT Chatbot', }, inputs: ["main"], outputs: ["main"], credentials: [ { name: 'YourGPTApi', required: true, }, ], properties: [ { displayName: 'Action', name: 'action', type: 'options', options: [ { name: 'Create Session', value: 'createSession', }, { name: 'Send Message', value: 'sendMessage', }, ], default: 'createSession', required: true, }, { displayName: 'Widget UID', name: 'widgetUid', type: 'string', default: '', required: true, }, { displayName: 'Message', name: 'message', type: 'string', default: '', displayOptions: { show: { action: ['sendMessage'], }, }, }, { displayName: 'Session UID', name: 'session_uid', type: 'string', default: '', placeholder: 'Enter session UID ', displayOptions: { show: { action: ['sendMessage'], }, }, required: true, }, ], }; } async execute() { var _a, _b, _c, _d; const items = this.getInputData(); const returnData = []; const credentials = await this.getCredentials('yourGPTApi'); const apiKey = credentials.apiKey; for (let i = 0; i < items.length; i++) { const action = this.getNodeParameter('action', i); const widgetUid = this.getNodeParameter('widgetUid', i); try { if (action === 'createSession') { const response = await this.helpers.request({ method: 'POST', url: 'https://api.yourgpt.ai/chatbot/v1/createSession', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'api-key': apiKey, }, form: { widget_uid: widgetUid, }, json: true, }); returnData.push({ json: response }); } if (action === 'sendMessage') { const message = this.getNodeParameter('message', i); const sessionUid = this.getNodeParameter('session_uid', i); const messageResponse = await this.helpers.request({ method: 'POST', url: 'https://api.yourgpt.ai/chatbot/v1/sendMessage', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'api-key': apiKey, }, form: { widget_uid: widgetUid, session_uid: sessionUid, message: message, }, json: true, }); returnData.push({ json: { reply: (_b = (_a = messageResponse.data) === null || _a === void 0 ? void 0 : _a.message) !== null && _b !== void 0 ? _b : '', choices: (_d = (_c = messageResponse.data) === null || _c === void 0 ? void 0 : _c.choices) !== null && _d !== void 0 ? _d : [], }, }); } } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } return this.prepareOutputData(returnData); } } exports.YourGPT = YourGPT; //# sourceMappingURL=YourGPT.node.js.map