-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.js
More file actions
67 lines (62 loc) · 1.83 KB
/
Copy pathCode.js
File metadata and controls
67 lines (62 loc) · 1.83 KB
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
require("dotenv").config();
const Telegraf = require("telegraf");
const app = new Telegraf(process.env.BOT_KEY, {
username: process.env.BOT_NAME,
});
const request = require("request");
(async function () {
app.command("start", (ctx) => {
ctx.reply("Welcome to Yu-Gi-Oh! Bot.\nLost? Use /help");
});
app.command("help", (ctx) => {
ctx.reply(
"To search: Use inline mode, mention the bot's username followed by card's name.\nexample : @YGOCard_bot Dark Magician.\n\nTo send a feedback: /feedback [suggestion]."
);
});
app.on("inline_query", (ctx) => {
const result = [];
if (
ctx.inlineQuery.query != undefined &&
ctx.inlineQuery.query.length >= 3
) {
request(
"https://db.ygoprodeck.com/api/v7/cardinfo.php?fname=" +
ctx.inlineQuery.query,
{ json: true },
(err, res, body) => {
if (err) {
// Return empty result
ctx.answerInlineQuery([]);
return;
} else if (res.body.data != undefined) {
res.body.data.forEach((element) => {
let name = element.name;
let card_id = element.id;
let image = element.card_images[0].image_url;
let thumb = element.card_images[0].image_url_small;
if (result.length < 50) {
result.push({
photo_url: image,
type: "photo",
title: name,
id: card_id,
thumb_url: thumb,
});
}
});
// Using context shortcut
ctx.answerInlineQuery(result);
}
}
);
} else {
// Do Nothing
ctx.answerInlineQuery([]);
return;
}
});
if (app.state.started) {
await app.stop();
}
await app.startPolling();
})();