-
Notifications
You must be signed in to change notification settings - Fork 3
/
messenger.js
64 lines (57 loc) · 1.68 KB
/
messenger.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
import request from "request-promise";
const TOKEN = process.env.TOKEN;
export function sendTextMessage(sender, text) {
let messageData = { text:text }
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: TOKEN},
method: 'POST',
json: {
recipient: {id:sender},
message: messageData,
}
})
.then(res => {
if (res.error) console.log('Error: ', response.body.error);
})
.catch(error => console.log('Error sending messages: ', error))
}
function toMarker({ lat, long, pokemon, id, remain, dist, end }) {
const imageURL = `https://maps.googleapis.com/maps/api/staticmap?size=764x400¢er=${lat},${long}&zoom=18&markers=${lat},${long}`;
const itemURL = `http://maps.apple.com/maps?q=${lat},${long}&z=16`;
return {
"title": `${pokemon}的位置,距離 ${dist} 公尺,剩下時間 ${remain} (${end})`,
"image_url": imageURL,
"item_url": itemURL,
};
}
const ids = [10, 13, 16, 19];
export function sendMapMessage(id, pokemons) {
const elements =
pokemons
.filter(({ id }) => ids.indexOf(id) === -1)
.slice(0, 5)
.map(toMarker);
const messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
elements,
}
}
};
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: TOKEN},
method: 'POST',
json: {
recipient: { id },
message: messageData,
}
})
.then(res => {
if (res.error) console.log('Error: ', response.body.error);
})
.catch(error => console.log('Error sending messages: ', error))
}