-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
125 lines (123 loc) · 2.48 KB
/
handler.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
122
123
124
125
const axios = require('axios');
const producyHuntUrl = 'https://www.producthunt.com/frontend/graphql';
const graphqlQuery = `query LegacyHomePage(
$cursor: String
$postCursor: String
) {
sections(first: 1, after: $cursor) {
edges {
cursor
node {
id
date
posts(after: $postCursor) {
edges {
node {
...PostItemList
featuredComment {
id
body: bodyText
user {
id
__typename
}
__typename
}
__typename
}
__typename
}
pageInfo {
endCursor
hasNextPage
__typename
}
__typename
}
__typename
}
__typename
}
pageInfo {
endCursor
hasNextPage
__typename
}
__typename
}
}
fragment PostItemList on Post {
id
...PostItem
__typename
}
fragment PostItem on Post {
id
_id
commentsCount
name
shortenedUrl
slug
tagline
updatedAt
pricingType
topics(first: 1) {
edges {
node {
id
name
slug
__typename
}
__typename
}
__typename
}
...PostThumbnail
...PostVoteButton
__typename
}
fragment PostThumbnail on Post {
id
name
thumbnailImageUuid
...PostStatusIcons
__typename
}
fragment PostStatusIcons on Post {
name
productState
__typename
}
fragment PostVoteButton on Post {
createdAt
... on Votable {
id
votesCount
__typename
}
__typename
}`;
const productHuntBody = {
operationName: 'LegacyHomePage',
variables: { cursor: 'MA==' },
query: graphqlQuery,
};
module.exports.run = async () => {
const response = await axios.post(producyHuntUrl, productHuntBody);
const slackMsg = response.data.data.sections.edges[0].node.posts.edges.sort(
(a, b) => b.node.votesCount - a.node.votesCount,
).slice(0, process.env.POST_COUNT).map((post) => ({
type: 'section',
text: {
type: 'mrkdwn',
text: `*<https://www.producthunt.com/posts/${post.node.slug}|${post.node.name}>*\n:star: ${post.node.votesCount} votes\n ${post.node.tagline}`,
},
accessory: {
type: 'image',
image_url: `https://ph-files.imgix.net/${post.node.thumbnailImageUuid}`,
alt_text: post.node.name,
},
}));
await axios.post(process.env.WEBHOOK_URL, { blocks: slackMsg, text: 'Product Hunt Daily Digest' });
};