feat: add initial code

main
drendog 2024-01-23 00:49:50 +01:00
parent 40cf004299
commit e4af51d08e
25 changed files with 350 additions and 0 deletions

3
.env.example Normal file
View File

@ -0,0 +1,3 @@
BOT_TOKEN=
CHANNEL_ID=
GROUP_ID=

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env

8
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,8 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.codeLens.test": true,
"deno.documentPreloadLimit": 2000,
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno"
}

9
deno.json Normal file
View File

@ -0,0 +1,9 @@
{
"imports": {
"grammy": "https://deno.land/x/grammy@v1.20.3/mod.ts",
"grammy_conversations": "https://deno.land/x/grammy_conversations@v1.2.0/mod.ts"
},
"tasks": {
"dev": "deno run --watch --allow-net --allow-read --allow-env main.ts"
}
}

63
deno.lock Normal file
View File

@ -0,0 +1,63 @@
{
"version": "3",
"redirects": {
"https://lib.deno.dev/x/grammy@v1/mod.ts": "https://deno.land/x/grammy@v1.20.3/mod.ts",
"https://lib.deno.dev/x/grammy@v1/types.ts": "https://deno.land/x/grammy@v1.20.3/types.ts"
},
"remote": {
"https://cdn.skypack.dev/-/debug@v4.3.4-o4liVvMlOnQWbLSYZMXw/dist=es2019,mode=imports/optimized/debug.js": "671100993996e39b501301a87000607916d4d2d9f8fc8e9c5200ae5ba64a1389",
"https://cdn.skypack.dev/-/ms@v2.1.2-giBDZ1IA5lmQ3ZXaa87V/dist=es2019,mode=imports/optimized/ms.js": "fd88e2d51900437011f1ad232f3393ce97db1b87a7844b3c58dd6d65562c1276",
"https://cdn.skypack.dev/debug@4.3.4": "7b1d010cc930f71b940ba5941da055bc181115229e29de7214bdb4425c68ea76",
"https://deno.land/std@0.208.0/dotenv/mod.ts": "039468f5c87d39b69d7ca6c3d68ebca82f206ec0ff5e011d48205eea292ea5a6",
"https://deno.land/std@0.211.0/path/_common/assert_path.ts": "2ca275f36ac1788b2acb60fb2b79cb06027198bc2ba6fb7e163efaedde98c297",
"https://deno.land/std@0.211.0/path/_common/basename.ts": "569744855bc8445f3a56087fd2aed56bdad39da971a8d92b138c9913aecc5fa2",
"https://deno.land/std@0.211.0/path/_common/constants.ts": "dc5f8057159f4b48cd304eb3027e42f1148cf4df1fb4240774d3492b5d12ac0c",
"https://deno.land/std@0.211.0/path/_common/strip_trailing_separators.ts": "7024a93447efcdcfeaa9339a98fa63ef9d53de363f1fbe9858970f1bba02655a",
"https://deno.land/std@0.211.0/path/_os.ts": "8fb9b90fb6b753bd8c77cfd8a33c2ff6c5f5bc185f50de8ca4ac6a05710b2c15",
"https://deno.land/std@0.211.0/path/basename.ts": "5d341aadb7ada266e2280561692c165771d071c98746fcb66da928870cd47668",
"https://deno.land/std@0.211.0/path/posix/_util.ts": "1e3937da30f080bfc99fe45d7ed23c47dd8585c5e473b2d771380d3a6937cf9d",
"https://deno.land/std@0.211.0/path/posix/basename.ts": "39ee27a29f1f35935d3603ccf01d53f3d6e0c5d4d0f84421e65bd1afeff42843",
"https://deno.land/std@0.211.0/path/windows/_util.ts": "d5f47363e5293fced22c984550d5e70e98e266cc3f31769e1710511803d04808",
"https://deno.land/std@0.211.0/path/windows/basename.ts": "e2dbf31d1d6385bfab1ce38c333aa290b6d7ae9e0ecb8234a654e583cf22f8fe",
"https://deno.land/std@0.211.0/streams/_common.ts": "4f9f2958d853b9a456be033631dabb7519daa68ee4d02caf53e2ecbffaf5805f",
"https://deno.land/std@0.211.0/streams/iterate_reader.ts": "353e516908ce637e8b2a2e1301fa60316825667d0d880d47ea4c427a9a7758cf",
"https://deno.land/x/grammy@v1.20.3/bot.ts": "bbfc31f976a27a48992ebb21bcdc137f216eb28e32cc5de0041dcc8fca53d5b8",
"https://deno.land/x/grammy@v1.20.3/composer.ts": "a86dcd6c83e91f720ceb85dab2b1c7b966fc18fc6440848b87b4897fcaa63fc8",
"https://deno.land/x/grammy@v1.20.3/context.ts": "3e9b8e277f8b75bed20b46047ad93b027a182e9d3504f1f2d2bba0852d0bb77f",
"https://deno.land/x/grammy@v1.20.3/convenience/constants.ts": "8d7e2fb9b0f5bd4c10585d8a7528dee573dea5096041b35bebadbb943318d1fc",
"https://deno.land/x/grammy@v1.20.3/convenience/frameworks.ts": "77e2f9fc841ab92d4310b556126447a42f131ad976a6adfff454c016f339b28e",
"https://deno.land/x/grammy@v1.20.3/convenience/inline_query.ts": "409d1940c7670708064efa495003bcbfdf6763a756b2e6303c464489fd3394ff",
"https://deno.land/x/grammy@v1.20.3/convenience/input_media.ts": "7af72a5fdb1af0417e31b1327003f536ddfdf64e06ab8bc7f5da6b574de38658",
"https://deno.land/x/grammy@v1.20.3/convenience/keyboard.ts": "88aeab16f2aaf0b4098135b5f7a678f7d2ce288f28c7eba93eaa5553a7395152",
"https://deno.land/x/grammy@v1.20.3/convenience/session.ts": "f92d57b6b2b61920912cf5c44d4db2f6ca999fe4f9adef170c321889d49667c2",
"https://deno.land/x/grammy@v1.20.3/convenience/webhook.ts": "f1da7d6426171fb7b5d5f6b59633f91d3bab9a474eea821f714932650965eb9e",
"https://deno.land/x/grammy@v1.20.3/core/api.ts": "840c5d39ca953d5bdbf89e61836a8212b22110d142b1606127b9c1a5f7d0b96a",
"https://deno.land/x/grammy@v1.20.3/core/client.ts": "df622a135e71229ffe722406850c9c08b90dcdd4d049b46926128599c73f9dc5",
"https://deno.land/x/grammy@v1.20.3/core/error.ts": "4638b2127ebe60249c78b83011d468f5e1e1a87748d32fe11a8200d9f824ad13",
"https://deno.land/x/grammy@v1.20.3/core/payload.ts": "420e17c3c2830b5576ea187cfce77578fe09f1204b25c25ea2f220ca7c86e73b",
"https://deno.land/x/grammy@v1.20.3/filter.ts": "8bfd76005929c22c42c6fd70064467dd4e8cf6e83de3c8d9d0522930d433e45d",
"https://deno.land/x/grammy@v1.20.3/mod.ts": "7723e08709ff7fd01df3e463503e14e4fd1a581669380eed70351e1121e8a833",
"https://deno.land/x/grammy@v1.20.3/platform.deno.ts": "68272a7e1d9a2d74d8a45342526485dbc0531dee812f675d7f8a4e7fc8393028",
"https://deno.land/x/grammy@v1.20.3/types.deno.ts": "d43290407cdd90eaa67393f8ab53139bfb9f692335a92cb6f1c607ddac706b26",
"https://deno.land/x/grammy@v1.20.3/types.ts": "729415590dfa188dbe924dea614dff4e976babdbabb28a307b869fc25777cdf0",
"https://deno.land/x/grammy_conversations@v1.2.0/conversation.ts": "4bcad06e2ac562969a7a6661bb1cf1477a3fe9e537c18419de65c456feb69499",
"https://deno.land/x/grammy_conversations@v1.2.0/deps.deno.ts": "c982798d7ca4cd3ebcd5a24319d03c5980dc47827b5a172a77022859abdb404e",
"https://deno.land/x/grammy_conversations@v1.2.0/form.ts": "d2d527fdcb26eb489b4aa1a183ae3aa14bf185a375ebf7d5c6ab8360e8b0e170",
"https://deno.land/x/grammy_conversations@v1.2.0/mod.ts": "4234b7a353ebb6770352c8b1fcafb0de40abd764cc44ae018f10b29c3a065b50",
"https://deno.land/x/grammy_conversations@v1.2.0/utils.ts": "139ebe78dbf078d3bbf8cbc78ad286125a08e7e7895d4985fff7be9cef328e20",
"https://deno.land/x/grammy_types@v3.4.6/api.ts": "ae04d6628e3d25ae805bc07a19475065044fc44cde0a40877405bc3544d03a5f",
"https://deno.land/x/grammy_types@v3.4.6/inline.ts": "594e1e487c94bde6f1d17f457b7b002786eb40b7b3b77ed32b830571e6285b7e",
"https://deno.land/x/grammy_types@v3.4.6/manage.ts": "1d2b76d8735cdb56a2afe89097169ff4403b14016adb73e682fce61bc4c2efad",
"https://deno.land/x/grammy_types@v3.4.6/markup.ts": "bef977ea4c2f17791d6f8a0a8f37f6f7f622967030da4f6b42e41c3a9d797113",
"https://deno.land/x/grammy_types@v3.4.6/message.ts": "24de17e147a992eedebc8b28a553eeb1d62b0aecb93649fbde5f642bd5ad2b47",
"https://deno.land/x/grammy_types@v3.4.6/methods.ts": "9c4d413f1a240e356b58c41a2c9417ccbd42b287d7cab9375c568d5d80ffcb2b",
"https://deno.land/x/grammy_types@v3.4.6/mod.ts": "7b5f421b4fbb1761f7f0d68328eaddd515f3222ce3f3cdfbedd8d5a4781e91a7",
"https://deno.land/x/grammy_types@v3.4.6/passport.ts": "e3fb63aec96510bcc317ef48fd25b435444b8f407502d7568c00fce15f2958fd",
"https://deno.land/x/grammy_types@v3.4.6/payment.ts": "d23e9038c5b479b606e620dd84e3e67b6642ada110a962f2d5b5286e99ec7de5",
"https://deno.land/x/grammy_types@v3.4.6/settings.ts": "5e989f5bd6c587d55673bd8052293869aa2f372e9223dd7f6e28632bfe021b6e",
"https://deno.land/x/grammy_types@v3.4.6/update.ts": "597465794cbf6a6ab8a6e69c24645ed928e79af3d384dce63ae83716ba78ba3e",
"https://deno.land/x/oson@1.0.1/constructors.ts": "2b77dcdc8d8db5ece2860d1657f4dcef37dd761684f1d4b9535c7e56a0fbfcf6",
"https://deno.land/x/oson@1.0.1/mod.ts": "54e494dc517ce0de6c727c25d9731ccc748e8646c883e922dc5d656f523a4e5b",
"https://deno.land/x/oson@1.0.1/oson.ts": "ec3908ae5c9ceff7bfd869d95a2183b929b9d96fbff44b57d28d3b742d06a4a1"
}
}

5
main.ts Normal file
View File

@ -0,0 +1,5 @@
import { bot } from "./src/mod.ts";
if (import.meta.main) {
await bot.start();
}

19
src/bot.ts Normal file
View File

@ -0,0 +1,19 @@
/*
bot's main configuration and middleware setup.
*/
import { Bot } from "grammy";
import { BotContext } from "./mod.ts";
import { BOT_TOKEN } from "./config/mod.ts";
import { setupCallbackQueries } from "./callbackQueries/mod.ts";
import { setupCommands } from "./commands/mod.ts";
import { setupConversations } from "./conversations/mod.ts";
import { setupSession } from "./session/mod.ts";
export const bot = new Bot<BotContext>(BOT_TOKEN);
setupSession(bot);
setupConversations(bot);
setupCommands(bot);
setupCallbackQueries(bot);

View File

@ -0,0 +1,7 @@
export { setupCallbackQueries } from "./setupCallbackQueries.ts";
export { voteCallback } from "./voteCallback.ts";
export enum CallbackQueryEnum {
APPROVE = "approve",
REJECT = "reject",
}

View File

@ -0,0 +1,23 @@
import { Bot } from "grammy";
import { BotContext } from "../mod.ts";
import { IButtonCallbackData } from "../keyboards/mod.ts";
import { CallbackQueryEnum, voteCallback } from "./mod.ts";
export const setupCallbackQueries = (bot: Bot<BotContext>) => {
bot.on("callback_query:data", (ctx) => {
const callbackQueryData = JSON.parse(
ctx.callbackQuery.data,
) as IButtonCallbackData;
switch (callbackQueryData.cq) {
case CallbackQueryEnum.APPROVE:
case CallbackQueryEnum.REJECT:
voteCallback(ctx, bot, callbackQueryData);
break;
default:
throw new Error("Invalid callback query data");
}
});
console.log("Callback Queries setup complete");
};

View File

@ -0,0 +1,39 @@
import { Bot, Context } from "grammy";
import { CHANNEL_ID } from "../config/mod.ts";
import { BotContext } from "../mod.ts";
import {
getPostOutcomeKeyboard,
IButtonCallbackData,
} from "../keyboards/mod.ts";
import { CallbackQueryEnum } from "./mod.ts";
import { getChatIdFromSession } from "../session/mod.ts";
export const voteCallback = async (
ctx: Context,
bot: Bot<BotContext>,
callbackQueryData: IButtonCallbackData,
) => {
await ctx.answerCallbackQuery();
if (!callbackQueryData.sid) return;
const isApproved = callbackQueryData.cq === CallbackQueryEnum.APPROVE;
if (isApproved) {
await ctx.copyMessage(CHANNEL_ID);
bot.api.sendMessage(
getChatIdFromSession(callbackQueryData.sid),
"Post Approvato",
);
} else {
bot.api.sendMessage(
getChatIdFromSession(callbackQueryData.sid),
"Post Rifiutato",
);
}
// edit the message to show the outcome of approval
await ctx.editMessageReplyMarkup({
reply_markup: {
inline_keyboard: getPostOutcomeKeyboard(isApproved),
},
});
};

7
src/commands/cancel.ts Normal file
View File

@ -0,0 +1,7 @@
import { CommandContext } from "grammy";
import { BotContext } from "../mod.ts";
export const cancel = async (ctx: CommandContext<BotContext>) => {
await ctx.reply("Post cancelled");
await ctx.conversation.exit();
};

4
src/commands/mod.ts Normal file
View File

@ -0,0 +1,4 @@
export { setupCommands } from "./setupCommands.ts";
export { cancel } from "./cancel.ts";
export { post } from "./post.ts";
export { start } from "./start.ts";

7
src/commands/post.ts Normal file
View File

@ -0,0 +1,7 @@
import { CommandContext } from "grammy";
import { BotContext } from "../mod.ts";
import { ConversationsEnum } from "../conversations/mod.ts";
export const post = async (ctx: CommandContext<BotContext>) => {
await ctx.conversation.enter(ConversationsEnum.CREATE_POST);
};

View File

@ -0,0 +1,11 @@
import { Bot } from "grammy";
import { BotContext } from "../mod.ts";
import { cancel, post, start } from "./mod.ts";
export const setupCommands = (bot: Bot<BotContext>) => {
bot.command("start", start);
bot.command("post", post);
bot.command("cancel", cancel);
console.log("Commands setup complete");
};

6
src/commands/start.ts Normal file
View File

@ -0,0 +1,6 @@
import { CommandContext } from "grammy";
import { BotContext } from "../mod.ts";
export const start = async (ctx: CommandContext<BotContext>) => {
await ctx.reply("Bot is running!");
};

11
src/config/mod.ts Normal file
View File

@ -0,0 +1,11 @@
/*
Manages loading environment variables and configuration settings.
*/
import { load } from "https://deno.land/std@0.208.0/dotenv/mod.ts";
await load({ export: true });
export const BOT_TOKEN = Deno.env.get("BOT_TOKEN") ?? (() => { throw new Error("BOT_TOKEN is unset") })();
export const CHANNEL_ID = Deno.env.get("CHANNEL_ID") ?? (() => { throw new Error("CHANNEL_ID is unset") })();
export const GROUP_ID = Deno.env.get("GROUP_ID") ?? (() => { throw new Error("GROUP_ID is unset") })();

6
src/conversations/mod.ts Normal file
View File

@ -0,0 +1,6 @@
export { post } from "./post.ts";
export { setupConversations } from "./setupConversations.ts";
export enum ConversationsEnum {
CREATE_POST = "create_post",
}

21
src/conversations/post.ts Normal file
View File

@ -0,0 +1,21 @@
import { BotContext } from "../mod.ts";
import { Conversation } from "grammy_conversations";
import { GROUP_ID } from "../config/mod.ts";
import { getPostApprovalKeyboard } from "../keyboards/vote.ts";
// Handler for the post conversation
export const post = async (
conversation: Conversation<BotContext>,
ctx: BotContext,
) => {
// Start the conversation
await ctx.reply("Please send the content you'd like to post.");
// Wait for a message response
const postContext = await conversation.wait();
await postContext.copyMessage(GROUP_ID, {
reply_markup: {
inline_keyboard: getPostApprovalKeyboard(ctx.session.sessionId),
},
});
};

View File

@ -0,0 +1,17 @@
import { Bot } from "grammy";
import { BotContext } from "../mod.ts";
import { conversations, createConversation } from "grammy_conversations";
import { ConversationsEnum, post } from "./mod.ts";
export const setupConversations = (bot: Bot<BotContext>) => {
bot.use(conversations());
const postConversation = createConversation(
post,
ConversationsEnum.CREATE_POST,
);
bot.use(postConversation);
// Add Log
console.log("Conversations setup complete");
};

11
src/keyboards/mod.ts Normal file
View File

@ -0,0 +1,11 @@
export * from "./vote.ts";
import { CallbackQueryEnum } from "../callbackQueries/mod.ts";
export interface IButtonCallbackData {
/** Callback Query */
cq: CallbackQueryEnum.APPROVE | CallbackQueryEnum.REJECT;
/** Session ID */
sid: number;
}

35
src/keyboards/vote.ts Normal file
View File

@ -0,0 +1,35 @@
import { InlineKeyboardButton } from "https://deno.land/x/grammy_types@v3.4.6/mod.ts";
import { CallbackQueryEnum } from "../callbackQueries/mod.ts";
import { IButtonCallbackData } from "./mod.ts";
const getVoteButtonCallbackData = (sid: number, isApprove: boolean) =>
JSON.stringify({
cq: isApprove ? CallbackQueryEnum.APPROVE : CallbackQueryEnum.REJECT,
sid,
} as IButtonCallbackData);
export const getPostApprovalKeyboard = (
sid: number,
): InlineKeyboardButton[][] => [
[
{
text: "🟢",
callback_data: getVoteButtonCallbackData(sid, true),
},
{
text: "🔴",
callback_data: getVoteButtonCallbackData(sid, false),
},
],
];
export const getPostOutcomeKeyboard = (
isApproved: boolean,
): InlineKeyboardButton[][] => [
[
{
text: isApproved ? "✅ Approved" : "❌ Rejected",
callback_data: "noop",
},
],
];

9
src/mod.ts Normal file
View File

@ -0,0 +1,9 @@
export { bot } from "./bot.ts";
import { Context, SessionFlavor } from "grammy";
import { ConversationFlavor } from "grammy_conversations";
import { SessionData } from "./session/mod.ts";
export type BotContext =
& Context
& SessionFlavor<SessionData>
& ConversationFlavor;

10
src/session/mod.ts Normal file
View File

@ -0,0 +1,10 @@
export * from "./utils.ts";
export { setupSession } from "./setupSession.ts";
import { MemorySessionStorage } from "grammy";
export interface SessionData {
sessionId: number;
}
export const SessionStorage = new MemorySessionStorage<SessionData>();

View File

@ -0,0 +1,10 @@
import { Bot, session } from "grammy";
import { BotContext } from "../mod.ts";
import { SessionStorage } from "./mod.ts";
export const setupSession = (bot: Bot<BotContext>) => {
bot.use(session({
initial: () => ({ sessionId: new Date().getTime() }),
storage: SessionStorage,
}));
};

8
src/session/utils.ts Normal file
View File

@ -0,0 +1,8 @@
import { SessionStorage } from "./mod.ts";
export const getChatIdFromSession = (sid: number) => {
const sessionKeys = SessionStorage.readAllKeys();
return sessionKeys.find((key) =>
SessionStorage.read(key)?.sessionId === sid
) ?? -1;
};