-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
169 lines (136 loc) · 4.55 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const scrapper = require('x-ray')();
const axios = require('axios');
const {parse} = require('querystring');
const AWS = require('aws-sdk');
const sqs = new AWS.SQS({region: 'us-east-1'});
const {stringify} = require('flatted/cjs');
const empty200 = {statusCode: 200, body: ''};
const getOracleDocs = async (query) => {
const baseUrl = 'https://docs.oracle.com/apps/search/search.jsp?q=';
const url = `${baseUrl}${query}&product=${process.env.PRODUCT}`;
const results = await scrapper(url, '.srch-result', [{
title: '.srch-title',
topic: '.topictitle',
topic_link: '.topictitle@href',
link: 'p span a@href',
main: 'p',
}]);
const topFive = results.splice(0, 5);
return topFive.map((item) => ({
author_name: `${item.title} - ${item.topic}`,
author_link: item.topic_link,
title: item.link,
title_link: item.link,
text: item.main,
}));
};
const getMosLinks = async (query) => {
const baseUrl = process.env.MOSURL;
const url = `${baseUrl}${encodeURIComponent(query)}`;
const results = await scrapper(url, '.cb19', [{
link: 'cite a@href',
}]);
return results.splice(0, 5).map((item) => item.link);
};
const sendResults = async (query, url, attachments) => {
let data;
if (attachments.length === 0) {
data = {
response_type: 'in_channel',
text: 'sorry, I wasn\'t able to find anything',
};
} else {
data = {
response_type: 'in_channel',
text: `here are the results I found for: '${query}'`,
attachments,
};
}
try {
await axios.post(url, data);
} catch (error) {
console.error(`processEvent catch ${stringify(error)}`);
};
return empty200;
};
const enqueueRequest = async (event, type) => {
const body = parse(event.body);
body.type = type;
const params = {
MessageBody: JSON.stringify(body),
QueueUrl: process.env.QUEUE,
};
try {
await sqs.sendMessage(params).promise();
} catch (error) {
console.error(error);
return {statusCode: 200, body: JSON.stringify({
text: 'error processing request - check logs...',
})};
}
return {statusCode: 200, body: JSON.stringify({
response_type: 'in_channel',
text: 'working on your request...',
})};
};
const processMosCommand = async (text, url) => {
const query = text.replace(' ', '+');
const links = await getMosLinks(query);
const getPage = async (url) => {
return await scrapper(url, {items: ['p@html'], title: '.KM ', docid: '.KM docid'});
};
const pages = links.map((link) => getPage(link));
const results = await Promise.all(pages);
const items = results.map((item) => {
const cleaned = [];
for ( let i = 0; i < item.items.length -1; i++) {
let line = item.items[i];
line = line.replace(/<br>/gi, '\n').replace(/ /gi, ' ');
line = line.replace(/<hr>|<span[^>]*>|<\/span>|\r/gi, '');
line = line.replace(/<strong>|<\/strong>/gi, '*');
line = line.replace(/"/gi, '"').replace(/'/gi, '\'').replace(/\n\n+/g, '\n');
line = line.replace(/ +/g, ' ').trim();
/* skip the login sections */
if (line.includes('<b>In this Document')) {
break;
}
cleaned.push(line);
}
const text = cleaned.join('\n');
const docid = item.docid ? item.docid.substring(8, item.docid.length-1) : '';
const title = item.title ? item.title.replace('\n', ' - ').replace(/ +/g, ' ').trim() : docid;
return {title, docid: docid, text};
});
const attachments = items.map((item) => {
const attachment = {
title: item.title,
text: item.text,
};
if (item.docid) {
attachment.title_link = `https://support.oracle.com/epmos/faces/DocumentDisplay?id=${item.docid}`;
}
return attachment;
});
return await sendResults(text, url, attachments);
};
const processPbCommand = async (text, url) => {
const query = encodeURIComponent(text);
const attachments = await getOracleDocs(query);
return await sendResults(text, url, attachments);
};
module.exports.pbCommand = async (event) => await enqueueRequest(event, 'pb');
module.exports.mosCommand = async (event) => await enqueueRequest(event, 'mos');
module.exports.commandHandler = async (event) => {
const body = JSON.parse(event.Records[0].body);
const text = body.text.toLowerCase().trim();
const url = body.response_url;
switch (body.type) {
case 'mos':
return await processMosCommand(text, url);
case 'pb':
return await processPbCommand(text, url);
default: // bad command, log and ignore
console.error(`invalid command type: ${body.type}`);
return empty200;
}
};