Making the project more readable
The feature of editing text messages and captions is also added in this commit
This commit is contained in:
parent
bbe4d4f47f
commit
aa6f2d5ca7
8 changed files with 136 additions and 75 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -175,5 +175,5 @@ dist
|
|||
.DS_Store
|
||||
|
||||
# Custom ignores
|
||||
config.json
|
||||
config.env
|
||||
database.sqlite
|
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
@ -12,6 +12,7 @@
|
|||
"@types/bun": "latest"
|
||||
},
|
||||
"dependencies": {
|
||||
"dotenv": "^16.4.5",
|
||||
"grammy": "^1.29.0",
|
||||
"sequelize": "^6.37.3",
|
||||
"sqlite3": "^5.1.7"
|
||||
|
|
84
src/app.ts
84
src/app.ts
|
@ -1,79 +1,25 @@
|
|||
import { Bot } from "grammy";
|
||||
|
||||
import { connectToDatabase, readConfigFile } from "utils/utils";
|
||||
import { connectToDatabase, setupEnv } from "utils/utils";
|
||||
|
||||
import Archive from "models/archive";
|
||||
import messages from "composer/messages";
|
||||
import commands from "composer/commands";
|
||||
import edits from "composer/edits";
|
||||
|
||||
await connectToDatabase();
|
||||
await setupEnv();
|
||||
|
||||
const conf = await readConfigFile();
|
||||
const bot = new Bot(conf.token);
|
||||
const bot = new Bot(process.env.token!);
|
||||
|
||||
bot.command("start", (ctx) => {
|
||||
ctx.reply("Welcome to my magical mailbox 🪄");
|
||||
ctx.reply(`My mailbox doese support:
|
||||
💬 Text
|
||||
🔉 Voice/Audio
|
||||
🎞 Video/Video note
|
||||
🎆 Sticker/Gif
|
||||
📁 Document
|
||||
🥰 Reactions`);
|
||||
});
|
||||
|
||||
bot.on("message", async (ctx, next) => {
|
||||
if (!ctx.msg.reply_to_message) {
|
||||
return next();
|
||||
}
|
||||
try {
|
||||
const record: any = await Archive.findOne({
|
||||
where: { msgId: ctx.msg.reply_to_message.message_id },
|
||||
});
|
||||
const msg = await ctx.api.copyMessage(
|
||||
record.senderId,
|
||||
ctx.from.id,
|
||||
ctx.msgId,
|
||||
{
|
||||
reply_parameters: { message_id: record.senderMsgId },
|
||||
}
|
||||
);
|
||||
await Archive.create({
|
||||
msgId: msg.message_id,
|
||||
senderId: ctx.from.id,
|
||||
senderMsgId: ctx.msgId,
|
||||
});
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
|
||||
bot.on("message", async (ctx) => {
|
||||
try {
|
||||
const msg = await ctx.api.copyMessage(conf.admin, ctx.from.id, ctx.msgId);
|
||||
await Archive.create({
|
||||
msgId: msg.message_id,
|
||||
senderId: ctx.from.id,
|
||||
senderMsgId: ctx.msgId,
|
||||
});
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
|
||||
bot.on("message_reaction", async (ctx) => {
|
||||
try {
|
||||
const record: any = await Archive.findOne({
|
||||
where: { msgId: ctx.msgId },
|
||||
});
|
||||
await ctx.api.setMessageReaction(
|
||||
record.senderId,
|
||||
record.senderMsgId,
|
||||
ctx.messageReaction.new_reaction
|
||||
);
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
bot.use(commands);
|
||||
bot.use(messages);
|
||||
bot.use(edits);
|
||||
|
||||
bot.start({
|
||||
allowed_updates: ["message", "message_reaction", "message_reaction_count"],
|
||||
allowed_updates: [
|
||||
"message",
|
||||
"edited_message",
|
||||
"message_reaction",
|
||||
"message_reaction_count",
|
||||
],
|
||||
});
|
||||
|
|
18
src/composer/commands.ts
Normal file
18
src/composer/commands.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import { Composer } from "grammy";
|
||||
|
||||
import Archive from "models/archive";
|
||||
|
||||
const commands = new Composer();
|
||||
|
||||
commands.command("start", (ctx) => {
|
||||
ctx.reply("Welcome to my magical mailbox 🪄");
|
||||
ctx.reply(`My mailbox doese support:
|
||||
💬 Text
|
||||
🔉 Voice/Audio
|
||||
🎞 Video/Video note
|
||||
🎆 Sticker/Gif
|
||||
📁 Document
|
||||
🥰 Reactions`);
|
||||
});
|
||||
|
||||
export default commands;
|
35
src/composer/edits.ts
Normal file
35
src/composer/edits.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { Composer } from "grammy";
|
||||
|
||||
import Archive from "models/archive";
|
||||
|
||||
const edits = new Composer();
|
||||
|
||||
edits.on("edit:text", async (ctx, next) => {
|
||||
try {
|
||||
const record: any = await Archive.findOne({
|
||||
where: { senderMsgId: ctx.msgId },
|
||||
});
|
||||
await ctx.api.editMessageText(
|
||||
record.senderId,
|
||||
record.msgId,
|
||||
ctx.editedMessage?.text!
|
||||
);
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
|
||||
edits.on("edit:caption", async (ctx, next) => {
|
||||
try {
|
||||
const record: any = await Archive.findOne({
|
||||
where: { senderMsgId: ctx.msgId },
|
||||
});
|
||||
await ctx.api.editMessageCaption(record.senderId, record.msgId, {
|
||||
caption: ctx.editedMessage?.caption,
|
||||
});
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
|
||||
export default edits;
|
62
src/composer/messages.ts
Normal file
62
src/composer/messages.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
import { Composer, InputMediaBuilder } from "grammy";
|
||||
|
||||
import Archive from "models/archive";
|
||||
|
||||
const messages = new Composer();
|
||||
const env = process.env;
|
||||
|
||||
messages.on("message", async (ctx, next) => {
|
||||
if (!ctx.msg.reply_to_message) {
|
||||
return next();
|
||||
}
|
||||
try {
|
||||
const record: any = await Archive.findOne({
|
||||
where: { msgId: ctx.msg.reply_to_message.message_id },
|
||||
});
|
||||
const msg = await ctx.api.copyMessage(
|
||||
record.senderId,
|
||||
ctx.from.id,
|
||||
ctx.msgId,
|
||||
{
|
||||
reply_parameters: { message_id: record.senderMsgId },
|
||||
}
|
||||
);
|
||||
await Archive.create({
|
||||
msgId: msg.message_id,
|
||||
senderId: ctx.from.id,
|
||||
senderMsgId: ctx.msgId,
|
||||
});
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
|
||||
messages.on("message", async (ctx) => {
|
||||
try {
|
||||
const msg = await ctx.api.copyMessage(env.admin!, ctx.from.id, ctx.msgId);
|
||||
await Archive.create({
|
||||
msgId: msg.message_id,
|
||||
senderId: ctx.from.id,
|
||||
senderMsgId: ctx.msgId,
|
||||
});
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
|
||||
messages.on("message_reaction", async (ctx) => {
|
||||
try {
|
||||
const record: any = await Archive.findOne({
|
||||
where: { msgId: ctx.msgId },
|
||||
});
|
||||
await ctx.api.setMessageReaction(
|
||||
record.senderId,
|
||||
record.senderMsgId,
|
||||
ctx.messageReaction.new_reaction
|
||||
);
|
||||
} catch (err) {
|
||||
return ctx.reply("Oops, something wrong 😢");
|
||||
}
|
||||
});
|
||||
|
||||
export default messages;
|
|
@ -1,4 +1,5 @@
|
|||
import fs from "fs/promises";
|
||||
import { config } from "dotenv";
|
||||
|
||||
import { join } from "path";
|
||||
|
||||
import database from "database";
|
||||
|
@ -18,11 +19,9 @@ export async function connectToDatabase() {
|
|||
}
|
||||
}
|
||||
|
||||
export async function readConfigFile() {
|
||||
const path = join(rootPath(), "config.json");
|
||||
export async function setupEnv() {
|
||||
try {
|
||||
const buffer = await fs.readFile(path);
|
||||
return JSON.parse(buffer.toString());
|
||||
await config({ path: join(rootPath(), "config.env") });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue