-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
121 lines (107 loc) · 2.91 KB
/
index.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
const express = require('express');
const got = require('got');
const fs = require('fs');
const parser = require('body-parser');
const keys = require('./keys.json');
const WebClient = require('@slack/client').WebClient;
const twilio = require('twilio');
const app = express();
app.set('port', 8080);
app.use(express.static('web-app'));
app.use(parser.json());
app.post('/submit', function(req, res) {
let url = "https://jobs.github.com/positions.json?description=";
let data = req.body;
console.log(data);
let i = 1;
// Assumes the following fields are provided
let location = `${data.location}`;
let keywords = data.keywords;
let email = `${data.email}`
let phone = "+1" + data.phoneNumber
let slack = "#" + data.slack
if (keywords[0] != undefined) {
url += keywords[0];
while (i < keywords.length) {
if (keywords[i] != undefined) {
url += "+" + keywords[i];
i++;
}
}
}
url+="&location=" + location
if (data.jobType[0] == "fullTime") {
url += "&full_time=true"
}
if (email != undefined) {
getJobs(url).then(function(result) {sendEmail(result, email)});
}
if (slack != undefined) {
getJobs(url).then(function(result) {sendSlack(result, slack)});
}
if (phone != undefined) {
getJobs(url).then(function(result) {sendTwilio(result, phone)});
}
});
app.listen(8080, function() {
console.log('Server started on port 8080!')
});
async function getJobs(url) {
let json;
let jobs = [];
const response = await got(url);
let data = JSON.parse(response.body);
for (let i = 0; i < data.length; i++) {
let { title, location, company, url } = data[i];
jobs.push({
title, location, company, url
});
}
return jobs;
}
function sendEmail(json, email) {
const jobs = json;
got.post("https://api.mixmax.com/v1/send", {
headers: {
"content-type": "application/json",
"X-API-TOKEN": keys.MIXMAX_API_KEY
},
body: JSON.stringify({
"message": {
"to": email,
"subject": "Job Listings 4 U!",
"html": jobs.map(function(job) {
return `${job.title} ${job.location} ${job.company} ${job.url}`
}).join("<br/>")
}
})
})
.then(response => {
console.log(response.statusCode);
})
}
function sendSlack(json, slack) {
let token = keys.SLACK_API_TOKEN;
let web = new WebClient(token);
let jobs = json;
let messageText = JSON.stringify(jobs.map(function(job) {
return ` ${job.title}, ${job.location}, ${job.company}, ${job.url} `
}) + '');
console.log(slack);
console.log(json);
web.chat.postMessage(slack, messageText, function(err, res) {
if (err) {
console.log('Error:', err);
} else {
console.log('Message sent')
}
});
}
function sendTwilio(json, phone) {
let client = new twilio(keys.TWILIO_SID, keys.TWILIO_API_KEY);
client.messages.create({
to: phone,
from: '+13479675486',
body: 'Hello from Twilio!'
});
}