Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Erkanerkisi/25d52196817c903d194dbe9c0760cfca to your computer and use it in GitHub Desktop.
Save Erkanerkisi/25d52196817c903d194dbe9c0760cfca to your computer and use it in GitHub Desktop.
You can put and trigger per this script every minute on google sheet appscript and get notification on slack channel.
function upworkJavaScript() {
analyzeFeedAndSendSlack('A3',
"feed-url-on-upwork",
"slack-web-hook-url")
}
var getSheetById = (ss, sheetId) => {
var foundSheets = ss.getSheets().filter(sheet => sheet.getSheetId() === sheetId);
return foundSheets.length ? foundSheets[0] : undefined;
}
const sendAlert = (payload, webHookUrl) => {
const webhook = webHookUrl;
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
try {
UrlFetchApp.fetch(webhook, options);
} catch (e) {
console.log(e)
Logger.log(e);
}
}
var analyzeFeedAndSendSlack = (cellValue, feedUrl, hookUrl) => {
const ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = getSheetById(ss, 0)
const lastDate = sheet.getRange(cellValue).getValue()
var txt = UrlFetchApp.fetch(feedUrl).getContentText();
var document = XmlService.parse(txt);
let root = document.getRootElement();
let channel = root.getChild('channel');
let items = channel.getChildren('item');
let maxLastDate;
console.log("lastDate -> " + lastDate)
for (let i = items.length - 1; i >= 0; i--) {
let pubDate = new Date(items[i].getChild('pubDate').getValue())
//console.log("pubDate : " + pubDate + " guid :" + items[i].getChild('guid').getText())
//console.log("decc : " + decc + "- punDate : " + pubDate + " condition : ")
//console.log("pub date naked: " + pubDate + " guid :" + items[i].getChild('guid').getText())
if (lastDate < pubDate) {
//console.log("pubDate : " + pubDate + " guid :" + items[i].getChild('guid').getText())
let desc = new String(items[i].getChild('description').getText())
desc = desc.replaceAll("<br />", "\n").replaceAll("<b>", "*").replaceAll("</b>", "*")
let payload = {
"text": items[i].getChild('title').getText(),
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": items[i].getChild('title').getText(),
"emoji": true
}
},
{
"type": "section",
"block_id": "section565",
"text": {
"type": "mrkdwn",
"text": "<" + items[i].getChild('link').getText() + "|job link> \n "
}
},
{
"type": "section",
"block_id": "section567",
"text": {
"type": "mrkdwn",
"text": desc
}
}
]
}
console.log("sent : " + items[i].getChild('guid').getText())
sendAlert(payload, hookUrl)
//sent slack
if (maxLastDate != null && maxLastDate != "") {
//console.log("if maxLastDate -> " + maxLastDate)
maxLastDate = pubDate > maxLastDate ? pubDate : maxLastDate
} else {
//console.log("if else pubDate -> " + pubDate)
maxLastDate = pubDate
}
console.log("last maxLastDate -> " + maxLastDate)
} else {
//console.log("no record found")
}
}
if (maxLastDate != null && maxLastDate != "") {
console.log("convertToDateTurkeyTimeZone(maxLastDate) -> " + maxLastDate)
sheet.getRange(cellValue).setValue(maxLastDate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment