This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
/
index.js
75 lines (65 loc) · 1.71 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
require('dotenv').config();
const accessToken = process.env.INSTAGRAM_ACCESS_TOKEN;
const userID = process.env.USER_ID_TO_MONITOR;
const slackURL = process.env.SLACK_INCOMING_WEBHOOK_URL;
const InstagramAPI = require('instagram-api');
const request = require('request');
const instagramAPI = new InstagramAPI(accessToken);
const express = require('express');
const app = express();
const sendPostToSlack=(media)=> {
let caption;
if (media.caption === null) {
caption = 'Unknown';
} else {
caption = media.caption.text;
}
request.post(
slackURL,
{
json: {
attachments: [
{
pretext: caption,
image_url: media.images.standard_resolution.url,
footer: `Instagram ${media.type} liked`,
footer_icon: 'https://lh3.googleusercontent.com/aYbdIM1abwyVSUZLDKoE0CDZGRhlkpsaPOg9tNnBktUQYsXflwknnOn2Ge1Yr7rImGk=w300',
ts: Math.floor(Date.now() / 1000),
},
],
},
},
(error, response) => response,
);
}
app.get('/run', (req, res) => {
instagramAPI.userMedia(userID).then(
(result) => {
const likePromise = [];
result.data.forEach((media) => {
const {
id,
user_has_liked: userHasLiked,
} = media;
if (!userHasLiked) {
likePromise.push(instagramAPI
.postMediaLike(id)
.then(() => sendPostToSlack(media)));
}
});
Promise.all(likePromise).then(
(values) => {
res.send(values);
},
(err) => {
res.send(err);
},
);
},
(err) => {
res.send(err);
},
);
});
app.set('port', (process.env.PORT || 3000));
app.listen(app.get('port'));