This commit is contained in:
Timofey Gelazoniya 2023-11-12 21:26:31 +03:00
commit 512a10ca29
Signed by: zeldon
GPG Key ID: 047886915281DD2A
11 changed files with 1839 additions and 0 deletions

3
.env.example Normal file
View File

@ -0,0 +1,3 @@
NODE_ENV=development
LOG_LEVEL=trace
TOKEN=

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
dist
.env

10
README.md Normal file
View File

@ -0,0 +1,10 @@
# remove-keyboard-bot
## Run bot
1) install NodeJS
2) clone this repo: `git clone https://git.zeldon.ru/zeldon/remove-keyboard-bot.git`
3) cd to dir with bot
4) write `npm i`
5) cp env: `cp .env.example .env`
6) edit `.env` (add token)
7) start in dev mode: `npm run dev` and prod: `npm run build && node dist/index.js`

19
index.ts Normal file
View File

@ -0,0 +1,19 @@
import { env } from "#root/env.js";
import { removeKeyboardFeature } from "#root/features/remove-keyboard.js";
import { welcomeFeature } from "#root/features/welcome.js";
import { logger } from "#root/logger.js";
import { Bot } from "grammy";
const bot = new Bot(env.TOKEN);
bot.use(welcomeFeature);
bot.use(removeKeyboardFeature);
bot.start({
onStart: ({ username }) =>
logger.info({
msg: "bot running...",
username,
}),
drop_pending_updates: true
});

1699
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

27
package.json Normal file
View File

@ -0,0 +1,27 @@
{
"name": "remove-keyboard-bot",
"main": "dist/index.js",
"type": "module",
"imports": {
"#root/*": "./dist/src/*"
},
"scripts": {
"dev": "npm run clean && tsc-watch --onSuccess \"tsx index.ts\"",
"build": "npm run clean && tsc --noEmit false",
"clean": "rimraf dist"
},
"devDependencies": {
"@types/node": "^20.9.0",
"rimraf": "^5.0.5",
"tsc-watch": "^6.0.4",
"tsx": "^3.14.0",
"typescript": "^5.2.2"
},
"dependencies": {
"dotenv": "^16.3.1",
"envalid": "^7.3.1",
"grammy": "^1.19.2",
"pino": "^8.16.1",
"pino-pretty": "^10.2.3"
}
}

10
src/env.ts Normal file
View File

@ -0,0 +1,10 @@
import 'dotenv/config';
import { cleanEnv, str } from 'envalid';
export const env = cleanEnv(process.env, {
NODE_ENV: str({ choices: ["development", "production"] }),
LOG_LEVEL: str({
choices: ["trace", "debug", "info", "warn", "error", "fatal", "silent"],
}),
TOKEN: str()
});

View File

@ -0,0 +1,14 @@
import { Composer, Context } from "grammy";
const composer = new Composer<Context>();
const feature = composer.chatType(['group', 'supergroup']);
feature.command('remove', async (ctx) => {
await ctx.reply('keboard removed', {
reply_markup: { remove_keyboard: true }
});
ctx.editMessageReplyMarkup();
});
export { composer as removeKeyboardFeature };

10
src/features/welcome.ts Normal file
View File

@ -0,0 +1,10 @@
import { Composer, Context } from "grammy";
const composer = new Composer<Context>();
const feature = composer.chatType('private');
feature.command(['help', 'start'], async (ctx) => {
ctx.reply('Add this bot to chat and type /remove command');
});
export { composer as welcomeFeature };

20
src/logger.ts Normal file
View File

@ -0,0 +1,20 @@
import { env } from "#root/env.js";
import { LoggerOptions, pino } from "pino";
import PinoPretty, { PrettyOptions } from "pino-pretty";
const options: LoggerOptions = {
level: env.LOG_LEVEL
};
const prettyOptions: PrettyOptions = {
ignore: 'pid,hostname',
colorize: env.isDev ? true : false,
translateTime: 'SYS:dd.mm.yyyy, HH:MM:ss'
};
export let logger = pino(options);
if (env.isDev) {
// @ts-ignore
logger = pino(options, PinoPretty(prettyOptions));
}

24
tsconfig.json Normal file
View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"strict": true,
"skipLibCheck": true,
"esModuleInterop": true,
"preserveWatchOutput": true,
"noEmit": true,
"module": "NodeNext",
"target": "ES2021",
"moduleResolution": "NodeNext",
"sourceMap": true,
"outDir": "dist",
"rootDir": ".",
"paths": {
"#root/*": [
"./src/*"
]
}
},
"include": [
"src/**/*",
"index.ts"
]
}