mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 15:06:41 +07:00
initial
This commit is contained in:
commit
ddefc2743a
21 changed files with 873 additions and 0 deletions
0
src/api/http/index.ts
Normal file
0
src/api/http/index.ts
Normal file
3
src/api/http/types.ts
Normal file
3
src/api/http/types.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export interface HttpConfig extends Record<string, any> {
|
||||
port: number
|
||||
}
|
5
src/api/http/validation.ts
Normal file
5
src/api/http/validation.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { number, object } from 'yup'
|
||||
|
||||
export const httpConfigSchema = object().required().shape({
|
||||
port: number().defined().required().positive(),
|
||||
})
|
0
src/api/telegram/index.ts
Normal file
0
src/api/telegram/index.ts
Normal file
18
src/config/index.ts
Normal file
18
src/config/index.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import yaml from 'js-yaml'
|
||||
import fs from 'fs'
|
||||
import path from 'path';
|
||||
import { Config } from './types';
|
||||
import { mergeRight } from 'ramda';
|
||||
import { validateConfig } from './validate';
|
||||
import logger from '../service/logger';
|
||||
|
||||
const defaultConfig = yaml.load<Config>(fs.readFileSync(path.join(__dirname, '../config.example.yml'), 'utf8'));
|
||||
const userConfig = yaml.load<Config>(fs.readFileSync(path.join(__dirname, '../config.yml'), 'utf8'));
|
||||
|
||||
const config = userConfig && mergeRight(defaultConfig, userConfig) || defaultConfig
|
||||
|
||||
export default function prepareConfig() {
|
||||
validateConfig(config)
|
||||
logger.debug('config is ok:', config)
|
||||
return config
|
||||
}
|
9
src/config/types.ts
Normal file
9
src/config/types.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { TelegramConfig } from '../service/telegram/types';
|
||||
import { VkConfig } from '../service/vk/types';
|
||||
import { HttpConfig } from '../api/http/types';
|
||||
|
||||
export interface Config extends Record<string, any>{
|
||||
http: HttpConfig
|
||||
telegram: TelegramConfig
|
||||
vk: VkConfig
|
||||
}
|
13
src/config/validate.ts
Normal file
13
src/config/validate.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { object } from 'yup'
|
||||
import { httpConfigSchema } from '../api/http/validation';
|
||||
import { Config } from './types';
|
||||
import { vkConfigSchema } from '../service/vk/validation';
|
||||
import { telegramConfigSchema } from '../service/telegram/validation';
|
||||
|
||||
const configSchema = object<Config>().required().shape({
|
||||
http: httpConfigSchema,
|
||||
vk: vkConfigSchema,
|
||||
telegram: telegramConfigSchema,
|
||||
})
|
||||
|
||||
export const validateConfig = (config: Config) => configSchema.validateSync(config)
|
14
src/index.ts
Normal file
14
src/index.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import prepareConfig from './config';
|
||||
import { TelegramService } from './service/telegram';
|
||||
import logger from './service/logger';
|
||||
import { VkService } from './service/vk';
|
||||
|
||||
|
||||
try {
|
||||
const config = prepareConfig()
|
||||
const telegramService = new TelegramService(config.telegram)
|
||||
const vkService = new VkService(config.vk)
|
||||
|
||||
} catch (e) {
|
||||
logger.error(e.message)
|
||||
}
|
7
src/service/logger/index.ts
Normal file
7
src/service/logger/index.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { createLogger, format, transports } from 'winston';
|
||||
|
||||
const logger = createLogger({
|
||||
transports: new transports.Console({ format: format.simple() })
|
||||
})
|
||||
|
||||
export default logger
|
9
src/service/telegram/index.ts
Normal file
9
src/service/telegram/index.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { TelegramConfig } from './types';
|
||||
|
||||
export class TelegramService {
|
||||
constructor(private props: TelegramConfig) {
|
||||
if (!props.key) {
|
||||
throw new Error('Telegram api key not found. Get it from bot father')
|
||||
}
|
||||
}
|
||||
}
|
4
src/service/telegram/types.ts
Normal file
4
src/service/telegram/types.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export interface TelegramConfig {
|
||||
key: string
|
||||
webhookUrl?: string
|
||||
}
|
6
src/service/telegram/validation.ts
Normal file
6
src/service/telegram/validation.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import * as yup from 'yup';
|
||||
|
||||
export const telegramConfigSchema = yup.object().required().shape({
|
||||
key: yup.string().required(),
|
||||
webhookUrl: yup.string().notRequired(),
|
||||
})
|
9
src/service/vk/index.ts
Normal file
9
src/service/vk/index.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { VkConfig } from './types';
|
||||
|
||||
export class VkService {
|
||||
constructor(private config: VkConfig) {
|
||||
if (!config.groups.length) {
|
||||
throw new Error('No vk groups to handle. Specify them in config')
|
||||
}
|
||||
}
|
||||
}
|
26
src/service/vk/types.ts
Normal file
26
src/service/vk/types.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
export interface VkConfig extends Record<string, any> {
|
||||
groups: ConfigGroup[]
|
||||
}
|
||||
|
||||
interface ConfigGroup {
|
||||
id: number
|
||||
name: string
|
||||
testResponse: string
|
||||
secretKey: string
|
||||
apiKey: string
|
||||
channels: GroupChannel[]
|
||||
}
|
||||
|
||||
interface GroupChannel {
|
||||
id: string,
|
||||
events: VkEvent[]
|
||||
}
|
||||
|
||||
export enum VkEvent {
|
||||
Confirmation = 'confirmation',
|
||||
WallPostNew = 'wall_post_new',
|
||||
PostSuggestion = 'post_suggestion',
|
||||
GroupJoin = 'group_join',
|
||||
GroupLeave = 'group_leave',
|
||||
MessageNew = 'message_new',
|
||||
}
|
20
src/service/vk/validation.ts
Normal file
20
src/service/vk/validation.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import * as yup from 'yup'
|
||||
import { VkConfig, VkEvent } from './types';
|
||||
|
||||
const vkChannelEventSchema = yup.string().oneOf(Object.values(VkEvent))
|
||||
|
||||
const vkChannelSchema = yup.object().required().shape({
|
||||
id: yup.string().required().matches(/^@/, ({ path }) => `${path} should start with "@"`),
|
||||
events: yup.array().of(vkChannelEventSchema)
|
||||
})
|
||||
|
||||
export const vkConfigSchema = yup.object<VkConfig>().required().shape({
|
||||
groups: yup.array().required().of(yup.object().shape({
|
||||
id: yup.number().positive(),
|
||||
name: yup.string().required(),
|
||||
testResponse: yup.string().required(),
|
||||
secretKey: yup.string().required(),
|
||||
apiKey: yup.string().required(),
|
||||
channels: yup.array().of(vkChannelSchema),
|
||||
}))
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue