-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
105 lines (96 loc) · 2.63 KB
/
app.js
File metadata and controls
105 lines (96 loc) · 2.63 KB
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
import bodyParser from 'body-parser';
import express from 'express';
import axios from 'axios';
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('Welcome to marys api');
});
app.post('/strain', (req, res) => {
const { searchString } = req.body;
axios.get(`https://www.cannabisreports.com/api/v1.0/strains/search/${searchString}`)
.then(({ data }) => {
let result;
if (data.data.length > 0) {
result = data.data.map(res => (
{
title: res.name,
image_url: res.image,
subtitle: `Seed Company name: ${res.seedCompany.name}, Ucpc: ${res.seedCompany.ucpc}. Link: ${res.link}`,
buttons: [
{
url: `https://marys-api.herokuapp.com/detail?url=${res.link}`,
type: 'json_plugin_url',
title: 'Get Details'
}
]
}
));
res.send([
{ attachment: {
type: 'template',
payload: {
template_type: 'generic',
elements: [
...result
]
}
} }
]);
} else {
res.send([{ text: 'No strain found' }]);
}
}, ({ response }) => {
res.send({
status: 400,
error: response.data
});
});
});
app.post('/detail', (req, res) => {
const { url } = req.query;
/**
* Get strain details from the api
* @method getStrainDetails
* @param {string} strain
* @returns {promise} - Promise containing the data to be resolved
*/
function getStrainDetails(strain) {
return Promise.all([
axios.get(`${strain}`),
axios.get(`${strain}/effectsFlavors`),
axios.get(`${strain}/seedCompany`)
]);
}
getStrainDetails(url).then((result) => {
// console.log(result[0].data, 'uefjsfnjsnfsjnfsjfnsjf', result[1].data);
let text = `Details for ${result[0].data.data.name}. \n \n `;
if (result[1].data.data) {
const data = result[1].data.data;
text += 'Effects Flavours \n';
for (const n in data) {
text += `${n}: ${data[n]}. \n `;
}
text += '\n ';
}
if (result[2].data.data) {
const companyDetails = result[2].data.data;
text += 'Company Detail. \n';
text += `Name: ${companyDetails.name}. \n `;
if (companyDetails.lineage) {
const lineage = companyDetails.lineage;
text += 'Lineage -: \n ';
for (const i in lineage) {
text += `${i}. \n `;
}
}
}
res.send([{
text
}]);
});
});
app.listen(process.env.PORT || 3000, () => {
console.log('App started here');
});