-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
104 lines (95 loc) · 2.52 KB
/
app.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
// imports
const { bot, key } = require("./token");
const {
Client,
PlacesNearbyRanking,
} = require("@googlemaps/google-maps-services-js");
const { Telegraf, Markup } = require("telegraf");
const client = new Client({});
let long = "";
let lat = "";
// restaurant, museum, shopping mall, cafe
let type = "";
// when the bot is first started
bot.start((ctx) => {
ctx.reply(
"Hello! This is GGCoolSpotBot! I will try to help " +
"you find all the cool spots within your current area!"
);
ctx.reply(
"What kind of spots would you like to find?",
Markup.keyboard(["restaurant", "cafe", "museum", "shoppingmall"])
.oneTime()
.resize()
);
});
bot.help((ctx) => {
ctx.reply(
"Do choose what kind of spots to look for!",
Markup.keyboard(["restaurant", "cafe", "museum", "shoppingmall"])
.oneTime()
.resize()
);
});
// takes in what kind of spot to find, prompts to share location
bot.on("text", (ctx) => {
let str = ctx.message.text;
if (
str == "museum" ||
str == "restaurant" ||
str == "cafe" ||
str == "shoppingmall"
) {
type = str;
ctx.reply(
"Can I get your location to get started?",
Markup.keyboard([Markup.button.locationRequest("Sure! Get my location!")])
.oneTime()
.resize()
);
} else {
ctx.reply("Do press the buttons!");
ctx.reply(
"Do choose what kind of spots to look for!",
Markup.keyboard(["restaurant", "cafe", "museum", "shoppingmall"])
.oneTime()
.resize()
);
}
});
// when the location is finally sent
bot.on("location", (ctx) => {
long = ctx.message.location.longitude;
lat = ctx.message.location.latitude;
ctx.reply("Thanks for sending me ur location!");
client
.placesNearby({
params: {
location: { lat: lat, lng: long },
key: key,
radius: 500,
opennow: true,
rankby: PlacesNearbyRanking.prominence,
type: type != "shoppingmall" ? type : "shopping_mall",
},
timeout: 1000, // milliseconds
})
.then((r) => {
const resultArray = r.data.results;
if (resultArray.length == 0) {
ctx.reply("Sorry! None found.");
}
for (let i = 0; i < resultArray.length; i++) {
ctx.reply(
resultArray[i].name + "\n" + "Address: " + resultArray[i].vicinity
);
}
})
.catch((e) => {
console.log(e);
});
});
bot.launch();
// graceful stop
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));