-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
131 lines (108 loc) · 3.34 KB
/
mod.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Bot } from "grammy";
import { verify } from "./verify.ts";
import {
deletePost,
getPostData,
setPostData,
updatePostCaption,
updatePostStatus,
} from "./db.ts";
import {
generateKeyboard,
generateText,
keyboardComposer,
} from "./keyboard.ts";
import { listComposer, updateList } from "./list.ts";
// Init main objects
export const bot = new Bot(Deno.env.get("BOT_TOKEN") || "");
export const kv = await Deno.openKv();
// Import some env
export const channelId = Number(Deno.env.get("CHANNEL_ID"));
export const groupId = Number(Deno.env.get("GROUP_ID"));
// Add update command to list of my commands
await bot.api.setMyCommands([
{ command: "update", description: "Обновить список заказ-нарядов" },
{ command: "buttons", description: "Добавить панель кнопок" },
]);
// Work only in my channel and group
bot.use(verify);
// On post in channel -> set post
bot.chatType("channel").on("channel_post:caption", async (ctx) => {
await setPostData(ctx.channelPost.message_id, {
caption: ctx.channelPost.caption,
status: "new_",
createdAt: new Date(),
});
await updateList();
});
bot.chatType("channel").on("edited_channel_post:media", async (c) => {
if (!c.editedChannelPost.caption) return;
await updatePostCaption(
c.editedChannelPost.message_id,
c.editedChannelPost.caption,
);
await updateList();
});
// On post in group -> add buttons
bot.chatType("supergroup").on(":is_automatic_forward", async (ctx) => {
const forward = ctx.message.forward_origin;
if (!forward) return;
if (forward.type != "channel") return;
const postData = await getPostData(forward.message_id);
if (!postData) return;
const reply_markup = generateKeyboard(
forward.message_id,
postData.status,
);
await ctx.reply(generateText(postData.status), {
reply_parameters: { message_id: ctx.msgId },
reply_markup,
});
});
// Update command (updates list (really))
bot.chatType("supergroup").command("update", async (ctx) => {
try {
await updateList();
await ctx.react("👍");
} catch {
await ctx.react("🌚");
}
});
// Add buttons manually
bot.chatType("supergroup").command("buttons", async (ctx) => {
const reply = ctx.message.reply_to_message;
if (!reply) return;
if (!reply.forward_origin) return;
if (reply.forward_origin.type != "channel") return;
const postData = await getPostData(reply.forward_origin.message_id);
if (!postData) return;
const reply_markup = generateKeyboard(
reply.forward_origin.message_id,
postData.status,
);
await ctx.reply(generateText(postData.status), {
reply_parameters: { message_id: reply.message_id },
reply_markup,
});
});
// Delete order manually
bot.chatType("supergroup").command("enqueue", async (ctx) => {
const reply = ctx.message.reply_to_message;
if (!reply) return;
if (!reply.forward_origin) return;
if (reply.forward_origin.type != "channel") return;
await kv.enqueue(reply.forward_origin.message_id);
await ctx.react("⚡");
});
// Other composers
bot.use(keyboardComposer);
bot.use(listComposer);
// Post deletion listener
kv.listenQueue(async (postId: number) => {
const post = await getPostData(postId);
if (post?.status != "recent") return;
await deletePost(postId);
await updateList();
});
// Graceful catch
bot.catch((error) => console.error(error.message));