WIP: Initial commit

This commit is contained in:
Pavel 2020-02-15 19:07:55 +03:00
commit dc48c9635f
29 changed files with 3194 additions and 0 deletions

4
.env.dist Normal file
View File

@ -0,0 +1,4 @@
BOT_MODE=webhook
PORT=3000
LOG_LEVEL=debug
WEBHOOK_ROUTE="/webhook"

18
.eslintrc.js Normal file
View File

@ -0,0 +1,18 @@
module.exports = {
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
extends: [
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'prettier/@typescript-eslint', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
'plugin:prettier/recommended', // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
],
parserOptions: {
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
sourceType: 'module', // Allows for the use of imports
},
rules: {
'@typescript-eslint/explicit-member-accessibility': 0,
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/no-parameter-properties': 0,
'@typescript-eslint/interface-name-prefix': 0
},
};

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
/node_modules
.DS_Store
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.env*
!.env*.dist
build

7
LICENSE.md Normal file
View File

@ -0,0 +1,7 @@
Copyright 2020 Neur0toxine
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

4
README.md Normal file
View File

@ -0,0 +1,4 @@
## VK Speech-To-Text Bot
This bot should convert all audio messages in dialog to text. Useful in group chats.
It's not working yet, still WIP.

11
nodemon.json Normal file
View File

@ -0,0 +1,11 @@
{
"watch": [
"src",
".env"
],
"ext": "js,ts,json",
"ignore": [
"src/**/*.spec.ts"
],
"exec": "ts-node --transpile-only ./src/app.ts"
}

49
package.json Normal file
View File

@ -0,0 +1,49 @@
{
"name": "vk-stt-bot",
"version": "1.0.0",
"description": "Converts audio messages in dialogs into text",
"main": "./build/app.js",
"author": "Neur0toxine",
"license": "MIT",
"scripts": {
"build": "tsc",
"start": "node ./build/app.js",
"inspect": "npx cross-env DEBUG=express:* nodemon --inspect src/app.ts",
"lint": "yarn lint:js ",
"lint:eslint": "npx eslint --ignore-path .gitignore --ext .ts",
"lint:js": "yarn lint:eslint src/",
"lint:fix": "yarn lint:js -- --fix"
},
"dependencies": {
"body-parser": "^1.19.0",
"celebrate": "^11.0.1",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"lodash": "^4.17.15",
"method-override": "^3.0.0",
"node-wit": "^5.0.0",
"reflect-metadata": "^0.1.13",
"typedi": "^0.8.0",
"winston": "^3.2.1"
},
"devDependencies": {
"@types/cors": "^2.8.6",
"@types/express": "^4.17.2",
"@types/hapi__joi": "^16.0.10",
"@types/lodash": "^4.14.149",
"@types/node": "^13.7.1",
"@types/winston": "^2.4.4",
"@typescript-eslint/eslint-plugin": "^2.19.2",
"@typescript-eslint/parser": "^2.19.2",
"cross-env": "^7.0.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
"nodemon": "^2.0.2",
"prettier": "^1.19.1",
"ts-node": "^8.6.2",
"tslint": "^6.0.0",
"typescript": "^3.7.5"
}
}

9
src/api/index.ts Normal file
View File

@ -0,0 +1,9 @@
import { Router } from "express";
import vkwebhook from "./routes/vkwebhook";
export default () => {
const app = Router();
vkwebhook(app);
return app;
};

View File

View File

@ -0,0 +1,44 @@
import { Router, Request, Response, NextFunction } from "express";
import { Container } from "typedi";
import { Logger } from "winston";
import { isNil } from "lodash";
import config from "../../config";
import VKParsedMessageFactoryService from "../../services/VKParsedMessageFactory";
import IVKMessageObject from "../../interfaces/IVKMessageObject";
const route = Router();
export default (app: Router) => {
app.use(config.webhookRoute, route);
route.post("/", async (req: Request, res: Response, next: NextFunction) => {
const logger = Container.get("logger") as Logger;
logger.debug("got message with body: %o", req.body);
try {
const parser: VKParsedMessageFactoryService = Container.get(
VKParsedMessageFactoryService
);
const message: IVKMessageObject | null = parser.createParsedMessage(
req.body
);
if (!isNil(message)) {
logger.debug("message is parsed, see TODO");
}
/**
*TODO
* Create attachments parser service, it should return Array<VKAudioMessageItem>.
* Create audio processor service, it should receive TTS and array from previous service,
* then it should return Array<VKProcessedAudioMessageItem>.
* Create response service, it should get previous array and answer everybody.
*/
return res.status(200);
} catch (e) {
logger.error("🔥 error: %o", e);
return next(e);
}
});
};

40
src/app.ts Normal file
View File

@ -0,0 +1,40 @@
import "reflect-metadata";
import express from "express";
import config from "./config";
import Logger from "./loaders/logger";
import { BotMode } from "./interfaces/IConfig";
async function startServer() {
const app = express();
/**
* A little hack here
* Import/Export can only be used in 'top-level code'
* Well, at least in node 10 without babel and at the time of writing
* So we are using good old require.
**/
await require("./loaders").default({ expressApp: app });
app.listen(config.port, err => {
if (err) {
Logger.error(err);
process.exit(1);
return;
}
Logger.info(`
################################################
🛡 Server listening on port: ${config.port} 🛡
################################################
`);
});
}
async function startPoller() {
throw new Error("longpolling is not implemented yet");
}
if (config.botMode == BotMode.MODE_WEBHOOK) {
startServer();
} else {
startPoller();
}

View File

@ -0,0 +1,15 @@
export default class HTTPError implements Error {
public name: string;
public message: string;
public stack?: string;
public statusCode: number;
private baseError: Error;
constructor(message: string, statusCode: number) {
this.baseError = new Error(message);
this.name = this.baseError.name;
this.message = this.baseError.message;
this.stack = this.baseError.stack;
this.statusCode = statusCode;
}
}

View File

@ -0,0 +1,17 @@
export default class VKAudioMessageItem {
constructor(private peerId: string, private audioUrl: string) {}
/**
* getPeerId
*/
public getPeerId() {
return this.peerId;
}
/**
* getAudioUrl
*/
public getAudioUrl() {
return this.audioUrl;
}
}

View File

@ -0,0 +1,17 @@
export default class VKProcessedAudioMessageItem {
constructor(private peerId: string, private responseText: string) {}
/**
* getPeerId
*/
public getPeerId() {
return this.peerId;
}
/**
* getResponseText
*/
public getResponseText() {
return this.responseText;
}
}

19
src/config/index.ts Normal file
View File

@ -0,0 +1,19 @@
import dotenv from "dotenv";
import { IConfig, BotMode } from "../interfaces/IConfig";
process.env.NODE_ENV = process.env.NODE_ENV || "development";
const envFound = dotenv.config();
if (!envFound) {
throw new Error("⚠️ Couldn't find .env file ⚠️");
}
const config: IConfig = {
botMode: (process.env.BOT_MODE as BotMode) || BotMode.MODE_POLLING,
port: parseInt(process.env.PORT || "0", 10),
logLevel: process.env.LOG_LEVEL || "silly",
webhookRoute: process.env.WEBHOOK_ROUTE || "/webhook"
};
export default config;

View File

@ -0,0 +1,14 @@
export default interface IAttachment {
type: string;
photo?: object;
video?: object;
audio?: object;
doc?: object;
link?: object;
market?: object;
market_album?: object;
wall?: object;
wall_reply?: object;
sticker?: object;
gift?: object;
}

11
src/interfaces/IConfig.ts Normal file
View File

@ -0,0 +1,11 @@
export enum BotMode {
MODE_POLLING = "polling",
MODE_WEBHOOK = "webhook"
}
export interface IConfig {
botMode: BotMode;
port: number;
logLevel: string;
webhookRoute: string;
}

View File

@ -0,0 +1,20 @@
import IAttachment from "./IAttachment";
export default interface IVKMessageObject {
id: number;
date: number;
peer_id: number;
from_id: number;
text?: string;
random_id?: number;
ref?: string;
ref_source?: string;
attachments?: Array<IAttachment>;
important: boolean;
geo?: object;
payload?: string;
keyboard?: object;
fwd_messages?: Array<IVKMessageObject>;
reply_message?: IVKMessageObject;
action?: object;
}

View File

@ -0,0 +1,5 @@
export default interface IVKWebhookData {
type: string;
object?: object;
group_id?: number;
}

View File

@ -0,0 +1,18 @@
import { Container } from "typedi";
import LoggerInstance from "./logger";
import config from "../config";
export default () => {
try {
Container.set("config", config);
LoggerInstance.info("✌️ Config injected into container");
Container.set("logger", LoggerInstance);
LoggerInstance.info("✌️ Logger injected into container");
return { Logger: LoggerInstance };
} catch (e) {
LoggerInstance.error("🔥 Error on dependency injector loader: %o", e);
throw e;
}
};

59
src/loaders/express.ts Normal file
View File

@ -0,0 +1,59 @@
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import routes from "../api";
import HTTPError from "../components/HTTPError";
import ExpressLogger from "./../services/ExpressLogger";
import { Container } from "typedi";
export default ({ app }: { app: express.Application }) => {
/**
* Health Check endpoints
*/
app.get("/status", (req: express.Request, res: express.Response) => {
res.status(200).end();
});
app.head("/status", (req: express.Request, res: express.Response) => {
res.status(200).end();
});
// Useful if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
// It shows the real origin IP in the heroku or Cloudwatch logs
app.enable("trust proxy");
// The magic package that prevents frontend developers going nuts
// Alternate description:
// Enable Cross Origin Resource Sharing to all origins by default
app.use(cors());
// Some sauce that always add since 2014
// "Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it."
// Maybe not needed anymore ?
app.use(require("method-override")());
// Middleware that transforms the raw string of req.body into json
app.use(bodyParser.json());
// Load API routes
app.use("/", routes());
/// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new HTTPError("Not Found", 404);
next(err);
});
app.use(
(err: any, req: express.Request, res: express.Response, next: Function) => {
const status: number = err.statusCode || 500;
const logger: ExpressLogger = Container.get(ExpressLogger);
logger.logRequest(req, status, err.message);
res.status(status);
res.json({
success: false,
error: err.message
});
}
);
};

11
src/loaders/index.ts Normal file
View File

@ -0,0 +1,11 @@
import { Application } from "express";
import expressLoader from "./express";
import dependencyInjectorLoader from "./dependencyInjector";
export default async ({ expressApp }: { expressApp: Application }) => {
const { Logger } = await dependencyInjectorLoader();
Logger.info("✌️ Dependency Injector loaded");
await expressLoader({ app: expressApp });
Logger.info("✌️ Express loaded");
};

32
src/loaders/logger.ts Normal file
View File

@ -0,0 +1,32 @@
import winston from "winston";
import config from "../config";
const transports = [];
if (process.env.NODE_ENV !== "development") {
transports.push(new winston.transports.Console());
} else {
transports.push(
new winston.transports.Console({
format: winston.format.combine(
winston.format.cli(),
winston.format.splat()
)
})
);
}
const LoggerInstance = winston.createLogger({
level: config.logLevel,
levels: winston.config.npm.levels,
format: winston.format.combine(
winston.format.timestamp({
format: "YYYY-MM-DD HH:mm:ss"
}),
winston.format.errors({ stack: true }),
winston.format.splat(),
winston.format.json()
),
transports
});
export default LoggerInstance;

View File

@ -0,0 +1,15 @@
import { Service } from "typedi";
/**
* AbstractTTS is a unified way to implement TTS services.
*/
export default abstract class AbstractTTSService {
/**
* Fetches audio and converts it to text
*
* @param audioUrl URL to audio
*
* @returns Promise<string>
*/
public abstract async getTextFromAudio(audioUrl: string): Promise<string>;
}

View File

@ -0,0 +1,14 @@
import { Request } from "express";
import { Logger } from "winston";
import { Service, Inject } from "typedi";
@Service()
export default class ExpressLogger {
constructor(@Inject("logger") private logger: Logger) {}
public logRequest(req: Request, status: number, message: string) {
this.logger.info(
`[${req.ip}] ${req.method} "${req.url}" ${status} ${message}`
);
}
}

View File

@ -0,0 +1,20 @@
import { Service, Inject } from "typedi";
import { isNil } from "lodash";
import IVKMessageObject from "../interfaces/IVKMessageObject";
import IVKWebhookData from "../interfaces/IVKWebhookData";
@Service()
export default class VKParsedMessageFactoryService {
/**
* Parse message and return parsed representation of it
*/
public createParsedMessage(body: object): IVKMessageObject | null {
const data = body as IVKWebhookData;
if (isNil(data.object)) {
return null;
}
return data.object as IVKMessageObject;
}
}

18
src/services/WitTTS.ts Normal file
View File

@ -0,0 +1,18 @@
import { Service, Inject } from "typedi";
import { Logger } from "winston";
import AbstractTTS from "./AbstractTTS";
@Service()
export default class WitTTSService extends AbstractTTS {
constructor(@Inject("logger") private logger: Logger) {
super();
}
public async getTextFromAudio(audioUrl: string): Promise<string> {
const data = await fetch(audioUrl);
return new Promise<string>((resolve: Function, reject: Function) => {
reject("Not implemented yet");
});
}
}

69
tsconfig.json Normal file
View File

@ -0,0 +1,69 @@
{
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./build", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
"typeRoots": [
"./node_modules/@types",
"./src/types"
], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

2626
yarn.lock Normal file

File diff suppressed because it is too large Load Diff