-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathmain.ts
96 lines (84 loc) · 2.54 KB
/
main.ts
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
/*
ChannelActions Bot
Telegram: @ChannelActionsBot
(c) Aditya, https://xditya.me
*/
import config from "$env";
import composer from "./src/modules/mod.ts";
import { MyContext } from "./src/core/types.ts";
import { sessionsCollection } from "./src/database/sessionsDb.ts";
import i18n from "./src/core/i18n.ts";
import { serve } from "server";
import {
Bot,
GrammyError,
HttpError,
session,
webhookCallback,
} from "grammy/mod.ts";
import { autoQuote } from "autoQuote";
import { hydrate } from "hydrate";
import { MongoDBAdapter } from "mongo_sessions";
import { conversations, createConversation } from "conversations";
import { inputWelcomeMsg } from "./src/helpers/conversationHelpers.ts";
import { run } from "grammy_runner";
await i18n.loadLocalesDir("locales");
// initialize the bot
const bot = new Bot<MyContext>(config.BOT_TOKEN);
await bot.init();
bot.use(hydrate());
bot.use(autoQuote);
bot.use(
session({
initial: () => ({}),
storage: new MongoDBAdapter({ collection: sessionsCollection }),
}),
);
bot.use(i18n);
bot.use(conversations());
bot.use(createConversation(inputWelcomeMsg));
bot.use(composer);
bot.catch((err) => {
const ctx = err.ctx;
console.error(`Error while handling update ${ctx.update.update_id}:`);
const e = err.error;
if (e instanceof GrammyError) {
console.error("Error in request:", e.description);
} else if (e instanceof HttpError) {
console.error("Could not contact Telegram:", e);
} else {
console.error("Unknown error:", e);
}
});
if (Deno.args[0] == "--polling") {
console.info(`Started as @${bot.botInfo.username} on long polling.`);
// we use grammy's runner for concurrency
// basically, on local hosts, for broadcast plugin
// to work without killing the main bot process.
const runner = run(bot, undefined, {
allowed_updates: ["chat_join_request", "message", "callback_query"],
});
const stopRunner = () => runner.isRunning() && runner.stop();
Deno.addSignalListener("SIGINT", stopRunner);
Deno.addSignalListener(
Deno.build.os != "windows" ? "SIGTERM" : "SIGINT",
() => stopRunner,
);
} else {
console.info(`Started as @${bot.botInfo.username} on webhooks.`);
const handleUpdate = webhookCallback(bot, "std/http");
serve(async (req) => {
if (req.method === "POST") {
const url = new URL(req.url);
if (url.pathname.slice(1) === bot.token) {
try {
return await handleUpdate(req);
} catch (err) {
console.error(err);
}
}
}
return new Response("Welcome!");
});
}
export default bot;