wa-chat-server
Version:
Watson Assistant powered chat server
68 lines (67 loc) • 3.17 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoogleSheets = void 0;
const google_spreadsheet_1 = require("google-spreadsheet");
class GoogleSheets {
constructor(config, logger) {
this.config = config;
this.logger = logger;
}
initialize() {
return __awaiter(this, void 0, void 0, function* () {
try {
const googleConfig = {
client_email: this.config.google_client_email, // eslint-disable-line
private_key: this.config.google_private_key.replace(/\\n/gm, '\n'), // eslint-disable-line
};
this.logger.info('GoogleSheets - Google config', Object.assign({ client_url_id: this.config.google_client_url_id }, googleConfig));
const auth = { apiKey: this.config.google_api_key };
this.doc = yield new google_spreadsheet_1.GoogleSpreadsheet(this.config.google_client_url_id, auth);
yield this.doc.useServiceAccountAuth(googleConfig);
}
catch (error) {
this.logger.error(`${this.constructor.name}: initialize FAILED`, {
error,
// eslint-disable-next-line @typescript-eslint/camelcase
client_id: this.config.google_client_url_id,
// eslint-disable-next-line @typescript-eslint/camelcase
client_email: this.config.google_client_email,
});
throw error;
}
});
}
// Upload feedback on Google Sheets.
uploadFeedback(assistantId, sessionId, feedback, utterance, message, reason) {
return __awaiter(this, void 0, void 0, function* () {
const row = {
assistantId,
sessionId,
feedback,
conversation: `${utterance} === ${message}`,
reason,
};
try {
yield this.initialize();
yield this.doc.loadInfo();
const sheet = this.doc.sheetsByIndex[0];
this.logger.info(`${this.constructor.name}: uploadFeedback`, { row });
yield sheet.addRow(row);
}
catch (error) {
this.logger.error(`${this.constructor.name}: uploadFeedback failed`, { row, error });
throw error;
}
});
}
}
exports.GoogleSheets = GoogleSheets;