-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
124 lines (110 loc) · 3.08 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
const axios = require('axios')
module.exports.vk = async (event, context, callback) => {
const data = JSON.parse(event.body)
if (!data || !data.secret || data.secret !== process.env.VK_SECRET_CODE) {
callback(null, {
statusCode: 401,
body: 'No access',
})
return
}
switch (data.type) {
case 'confirmation':
if (data.group_id === parseInt(process.env.VK_GROUP_ID)) {
callback(null, {
statusCode: 200,
body: process.env.VK_CONFIRM_CODE,
})
} else {
callback(null, { success: false, message: 'Group id do not match' })
}
break
case 'wall_post_new':
const { post_type, attachments, text } = data.object
if (text && text.indexOf('@rnative') !== -1 && post_type === 'post') {
const post = {
help: text.indexOf('#help@rnative') !== -1,
text: formatTextFromHashtags(text),
images: attachments
.filter(
(file) =>
file.type === 'photo' &&
file.photo &&
(file.photo.photo_604 || file.photo.photo_807)
)
.map((file) => file.photo.photo_807 || file.photo.photo_604),
}
try {
if (!post.help) {
await Promise.all([
sendMessageToTelegramChannel(post),
sendMessageToDiscordChannel(post),
])
} else {
await sendMessageToDiscordChannel(post)
}
} catch (error) {
console.error(error)
}
}
callback(null, {
statusCode: 200,
body: 'ok',
})
break
}
}
const formatTextFromHashtags = (text) =>
text
.split(' ')
.map((w) => {
if (w.indexOf('#') !== -1 && w.indexOf('@rnative') !== -1)
w = w.substr(0, w.lastIndexOf('#')).replace(/\r|\\n/g, '')
return w
}, [])
.join(' ')
.trim()
const formatTextEllipsis = (text, length) =>
text.length > length ? text.substring(0, length - 3) + '...' : text
const sendMessageToDiscordChannel = ({ help, text: content, images }) => {
const channel = help
? process.env.DISCORD_CHANNEL_HELP
: process.env.DISCORD_CHANNEL_NEWS
const embed =
images && images.length
? {
image: {
url: images[0],
},
}
: null
return axios({
url: `https://discordapp.com/api/channels/${channel}/messages`,
headers: {
Authorization: `Bot ${process.env.DISCORD_TOKEN}`,
'Content-Type': 'application/json',
},
method: 'POST',
json: true,
data: {
content,
embed,
},
})
}
const sendMessageToTelegramChannel = ({ text, images }) => {
const photo = images && images.length ? images[0] : null
const content = photo ? formatTextEllipsis(text, 200) : text
return axios({
url: `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/${
photo ? 'sendPhoto' : 'sendMessage'
}`,
method: 'POST',
json: true,
data: {
chat_id: process.env.TELEGRAM_CHAT_ID,
[photo ? 'caption' : 'text']: content,
photo,
},
})
}