-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
93 lines (71 loc) · 2.47 KB
/
app.js
File metadata and controls
93 lines (71 loc) · 2.47 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
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
import closeWithGrace from "close-with-grace";
import Fastify from "fastify";
import autoLoad from "@fastify/autoload";
import fastifyStatic from "@fastify/static";
import path from "path";
import { fileURLToPath } from "url";
import { Bot, InlineKeyboard, InlineQueryResultBuilder } from "grammy";
import { config } from "./config.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Pass --options via CLI arguments in command to enable these options.
const options = {};
const app = Fastify({
logger: true
});
const bot = new Bot(config.BOT_TOKEN);
app.register(autoLoad, {
dir: path.join(__dirname, "plugins"),
options: Object.assign({}, options)
});
app.register(fastifyStatic, {
root: path.join(process.cwd(), config.PUBLIC_DIR),
prefix: "/"
});
// Receive webhook updates on path https://example.com/<BOT-TOKEN>
//app.post("/" + config.BOT_TOKEN, webhookCallback(bot, "fastify", { secretToken: config.SECRET_TOKEN }));
// delay is the number of milliseconds for the graceful close to finish
const closeListeners = closeWithGrace(
{ delay: process.env.FASTIFY_CLOSE_GRACE_DELAY || 500 },
// eslint-disable-next-line no-unused-vars
async function ({ signal, err, manual }) {
if (err) {
app.log.error(err);
}
await app.close();
}
);
app.addHook("onClose", async (instance, done) => {
closeListeners.uninstall();
done();
});
bot.command("start", async (ctx) => {
const keyboard = new InlineKeyboard();
keyboard.webApp("Open my CTRL wallet", config.WEBAPP_URL);
keyboard.switchInline("Invite contact");
await ctx.reply("What would you like to do?", { reply_markup: keyboard });
});
bot.on("inline_query", async (ctx) => {
const keyboard = new InlineKeyboard().url("Open CTRL", config.TG_APP_URL);
const result = InlineQueryResultBuilder
.article("id-0", "Invite contact to try CTRL", { reply_markup: keyboard })
.text("You've been invited to try the CTRL wallet!");
await ctx.answerInlineQuery(
[result],
{ cache_time: 0 },
);
});
const startServer = async () => {
try {
await app.listen({ port: config.APP_PORT, host: config.APP_HOST }, async (error) => {
await bot.start();
console.log("Bot started");
//await bot.api.setWebhook(config.WEBHOOK_URL + config.BOT_TOKEN, { secret_token: config.SECRET_TOKEN });
});
console.log("Server listening on " + config.APP_PORT);
} catch (err) {
console.error(err);
process.exit(1);
}
};
startServer();