Skip to content

Instantly share code, notes, and snippets.

@jakubsadura
Created June 28, 2020 20:10
Show Gist options
  • Save jakubsadura/8a68256d104210765015fa7d80442d1c to your computer and use it in GitHub Desktop.
Save jakubsadura/8a68256d104210765015fa7d80442d1c to your computer and use it in GitHub Desktop.
slack bot for Ikea items availability
var request = require('request');
const ITEM_URL = 'https://www.ikea.com/pl/pl/p/gubboen-fotel-bujany-wew-zew-bialy-00469035/';
const URL = 'https://iows.ikea.com/retail/iows/pl/pl/stores/204/availability/ART/00469035';
const SLACK = 'https://hooks.slack.com/services/X/Y/Z';
request({
url: URL,
method: 'GET',
headers: {
"accept": "application/vnd.ikea.iows+json;version=1.0",
"accept-language": "pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "no-cache",
"consumer": "MAMMUT",
"contract": "37249",
"pragma": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site"
}
}, function(error, response, body) {
if (error) {
console.error(error);
}
if (response.statusCode === 200) {
const { StockAvailability: { RetailItemAvailability } } = JSON.parse(response.body);
const { AvailableStock: { $: availableStock }, RestockDateTime: { $: restockDateTime } } = RetailItemAvailability;
const isAvailable = parseInt(availableStock) > 0;
let messageText = `${availableStock} sztuk, data nastepnej dostawy: ${restockDateTime}, produkt: ${ITEM_URL} dostepnosc: ${URL}`;
if (isAvailable) {
messageText = `JEST JUZ W SKLEPIE ${messageText}`;
} else {
messageText = `jeszcze nie ma w sklepie ${messageText}`;
}
const payload = {
"channel": "@jakub.sadura",
"username": "IkeaBot",
"icon_emoji": ":dworak:",
"text": messageText
};
//if (isAvailable) {
request({
url: SLACK,
method: 'POST',
body: JSON.stringify(payload),
headers: {
'content-type': 'application/json'
}
}, function(err, rsp, body) {
if (!err && rsp.statusCode == 200) {
console.log(body.name);
} else {
console.log(rsp);
}
});
//}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment