nestgram
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,2 +0,2 @@ | ||
import { IMessage, ContentTypes, ISendPhotoOptions, IOptions, MessageCreator, Keyboard } from '..'; | ||
import { IMessage, ContentTypes, ISendPhotoOptions, IOptions, MessageCreator, Keyboard, IWebhookConfig, IDeleteWebhookConfig } from '..'; | ||
import { Media } from './Media'; | ||
@@ -9,4 +9,6 @@ export declare class Api { | ||
private buildFormData; | ||
setWebhook(config: IWebhookConfig): Promise<boolean>; | ||
deleteWebhook(config?: IDeleteWebhookConfig): Promise<boolean>; | ||
send(chatId: string | number, content: MessageCreator | ContentTypes, keyboard?: Keyboard | null, moreOptions?: IOptions): Promise<IMessage>; | ||
sendPhoto(chatId: string | number, photo: Media, keyboard?: Keyboard | null, moreOptions?: ISendPhotoOptions): Promise<IMessage>; | ||
} |
@@ -45,2 +45,8 @@ "use strict"; | ||
} | ||
setWebhook(config) { | ||
return this.callApi('setWebhook', config); | ||
} | ||
deleteWebhook(config) { | ||
return this.callApi('deleteWebhook', config); | ||
} | ||
send(chatId, content, keyboard = null, moreOptions = {}) { | ||
@@ -47,0 +53,0 @@ if (content instanceof __1.MessageCreator) { |
@@ -1,5 +0,8 @@ | ||
import { IMessage, IUpdate } from '../..'; | ||
import { IMessage, IMessageEntity, IUpdate, MessageEntityTypes } from '../..'; | ||
export declare class Filter { | ||
static commandParamsRegExp: RegExp; | ||
static getChatId(update: IUpdate): number | string | undefined; | ||
static getMessage(update: IUpdate): IMessage | undefined; | ||
static getEntity(update: IUpdate, entityType: MessageEntityTypes): IMessageEntity | undefined; | ||
static getCommandParams(update: IUpdate): string[]; | ||
} |
@@ -12,4 +12,17 @@ "use strict"; | ||
} | ||
static getEntity(update, entityType) { | ||
return Filter.getMessage(update).entities.find((entity) => entity.type === entityType); | ||
} | ||
static getCommandParams(update) { | ||
const message = Filter.getMessage(update); | ||
if (!message) | ||
return []; | ||
const match = message.text.match(Filter.commandParamsRegExp); | ||
if (!match) | ||
return []; | ||
return match[1].split(' ') || []; | ||
} | ||
} | ||
exports.Filter = Filter; | ||
Filter.commandParamsRegExp = /\/.*? (.*)/; | ||
//# sourceMappingURL=Filter.js.map |
@@ -8,5 +8,5 @@ import { IHandler, IUpdate, MiddlewareFunction, NextFunction } from '../../types'; | ||
constructor(token: string, handlers: IHandler[], logging?: true); | ||
getNextFunction(update: IUpdate, answer: Answer, middlewares: MiddlewareFunction[], index: number): NextFunction | null; | ||
getNextFunction(update: IUpdate, answer: Answer, params: any, middlewares: MiddlewareFunction[], index: number, handler: NextFunction, failFunction: NextFunction): NextFunction | null; | ||
handleMiddleware(index: number, update: IUpdate, answer: Answer): void; | ||
handleUpdate(update: IUpdate): Promise<void>; | ||
} |
@@ -13,10 +13,8 @@ "use strict"; | ||
} | ||
getNextFunction(update, answer, middlewares, index) { | ||
getNextFunction(update, answer, params, middlewares, index, handler, failFunction) { | ||
const nextFunction = middlewares[++index]; | ||
if (!nextFunction) | ||
return null; | ||
return () => { | ||
if (this.logging) | ||
(0, logger_1.log)('blue', 'Calling next middleware', `(${update.update_id})`.grey); | ||
return nextFunction(update, answer, this.getNextFunction(update, answer, middlewares, index)); | ||
return (nextFunction || handler)(update, answer, params, this.getNextFunction(update, answer, params, middlewares, index, handler, failFunction.bind(null, index)), failFunction.bind(null, index)); | ||
}; | ||
@@ -28,2 +26,3 @@ } | ||
return; | ||
const params = {}; | ||
const baseNextFunction = async () => { | ||
@@ -33,4 +32,13 @@ if (this.logging) | ||
const message = Filter_1.Filter.getMessage(update); | ||
const args = [update, message, message === null || message === void 0 ? void 0 : message.text, answer, message === null || message === void 0 ? void 0 : message.entities]; | ||
const handlerMethod = handler.controller[handler.methodKey]; | ||
const commandParams = Filter_1.Filter.getCommandParams(update); | ||
const args = [ | ||
update, | ||
message, | ||
message === null || message === void 0 ? void 0 : message.text, | ||
answer, | ||
message === null || message === void 0 ? void 0 : message.entities, | ||
commandParams, | ||
params, | ||
]; | ||
const handlerMethod = handler.controller[handler.methodKey].bind(handler.controller); | ||
if (!['AsyncFunction', 'Function'].includes(handlerMethod.constructor.name)) | ||
@@ -42,3 +50,3 @@ return; | ||
} | ||
catch (e) { | ||
catch (_a) { | ||
resultMessageToSend = handlerMethod(...args); | ||
@@ -50,9 +58,10 @@ } | ||
}; | ||
const failNextFunction = () => { | ||
const failNextFunction = (i = index) => { | ||
if (this.logging) | ||
(0, logger_1.log)('blue', 'Middleware called fail function', `(${update.update_id})`.grey); | ||
return this.handleMiddleware(index + 1, update, answer); | ||
return this.handleMiddleware(i + 1, update, answer); | ||
}; | ||
(0, logger_1.log)('blue', 'Calling first middleware/handler', `(${update.update_id})`.grey); | ||
handler.middlewares[0](update, answer, this.getNextFunction(update, answer, handler.middlewares, 0) || baseNextFunction, failNextFunction); | ||
if (this.logging) | ||
(0, logger_1.log)('blue', 'Calling first middleware/handler', `(${update.update_id})`.grey); | ||
handler.middlewares[0](update, answer, this.getNextFunction(update, answer, params, handler.middlewares, 0, baseNextFunction, failNextFunction) || baseNextFunction, failNextFunction); | ||
const isContinue = Reflect.getMetadata('continue', handler.controller[handler.methodKey]); | ||
@@ -64,2 +73,4 @@ if (!isContinue) | ||
async handleUpdate(update) { | ||
if (this.logging) | ||
(0, logger_1.log)('blue', 'Got new update!', `(${update.update_id})`.grey); | ||
const answer = new Answer_1.Answer(this.token, update); | ||
@@ -66,0 +77,0 @@ const handler = this.handlers[0]; |
@@ -1,2 +0,2 @@ | ||
import { IConfig, IHandler } from '../../types'; | ||
import { IPollingConfig, IHandler } from '../../types'; | ||
import { Handler } from './Handler'; | ||
@@ -11,5 +11,5 @@ import { Api } from '../Api'; | ||
handler: Handler; | ||
constructor(token: string, handlers: IHandler[], config?: IConfig | null, logging?: true); | ||
constructor(token: string, handlers: IHandler[], config?: IPollingConfig | null, logging?: true); | ||
start(): Promise<void>; | ||
private updateGetter; | ||
} |
@@ -13,3 +13,3 @@ "use strict"; | ||
this.logging = logging; | ||
this.api = new Api_1.Api(); | ||
this.api = new Api_1.Api(this.token); | ||
this.handler = new Handler_1.Handler(this.token, this.handlers, this.logging); | ||
@@ -22,4 +22,2 @@ if (!this.token) | ||
for (const update of await updates) { | ||
if (this.logging) | ||
(0, logger_1.log)('blue', 'Got new update!', `(${update.update_id})`.grey); | ||
await this.handler.handleUpdate(update); | ||
@@ -26,0 +24,0 @@ await this.api.call(this.token, 'getUpdates', { |
@@ -1,2 +0,2 @@ | ||
import { MiddlewareFunction } from '..'; | ||
import { MiddlewareFunction, MessageEntityTypes } from '..'; | ||
import { MessageSubtypes } from '../types/listen-middlewares.types'; | ||
@@ -6,4 +6,4 @@ export declare class ListenMiddleware { | ||
static text(text?: string): MiddlewareFunction; | ||
static entity(entityType?: string): MiddlewareFunction; | ||
static entity(entityType?: MessageEntityTypes): MiddlewareFunction; | ||
static message(subtype: MessageSubtypes): MiddlewareFunction; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ListenMiddleware = void 0; | ||
const __1 = require(".."); | ||
class ListenMiddleware { | ||
@@ -8,7 +9,8 @@ static command(commandText) { | ||
const message = update.message; | ||
if (!message) | ||
if (!message || !message.text) | ||
return fail(); | ||
if (!message.text) | ||
const entity = __1.Filter.getEntity(update, 'bot_command'); | ||
if (!entity) | ||
return fail(); | ||
if (message.text !== `/${commandText}`) | ||
if (message.text.slice(entity.offset, entity.length) !== `/${commandText}`) | ||
return fail(); | ||
@@ -32,3 +34,2 @@ next(); | ||
return function use(update, answer, next, fail) { | ||
var _a; | ||
const message = update.message; | ||
@@ -39,3 +40,3 @@ if (!message) | ||
return fail(); | ||
if (!((_a = message.entities) === null || _a === void 0 ? void 0 : _a.find((entity) => entity.type === entityType))) | ||
if (!__1.Filter.getEntity(update, entityType)) | ||
return fail(); | ||
@@ -42,0 +43,0 @@ next(); |
@@ -9,1 +9,2 @@ import 'reflect-metadata'; | ||
export * from './updates/continue.decorator'; | ||
export * from './properties/api.decorator'; |
@@ -25,2 +25,3 @@ "use strict"; | ||
__exportStar(require("./updates/continue.decorator"), exports); | ||
__exportStar(require("./properties/api.decorator"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -7,1 +7,3 @@ export declare function Entity(entity: string): (target: any, propertyKey: string, parameterIndex: number) => any; | ||
export declare const Entities: () => ParameterDecorator; | ||
export declare const CommandParams: () => ParameterDecorator; | ||
export declare const Params: () => ParameterDecorator; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Entities = exports.Update = exports.Message = exports.Answ = exports.Text = exports.Entity = void 0; | ||
exports.Params = exports.CommandParams = exports.Entities = exports.Update = exports.Message = exports.Answ = exports.Text = exports.Entity = void 0; | ||
function defineArgumentMetadata(target, propertyKey, parameterIndex, gotIndexName, metadataKey = 'gotIndex') { | ||
@@ -35,2 +35,6 @@ Reflect.defineMetadata(metadataKey, { | ||
exports.Entities = Entities; | ||
const CommandParams = () => buildArgumentDecorator('commandParams'); | ||
exports.CommandParams = CommandParams; | ||
const Params = () => buildArgumentDecorator('params'); | ||
exports.Params = Params; | ||
//# sourceMappingURL=argumets.decorator.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.setupArguments = void 0; | ||
const argsProcedure = ['update', 'message', 'text', 'answer', 'entities']; | ||
const argsProcedure = [ | ||
'update', | ||
'message', | ||
'text', | ||
'answer', | ||
'entities', | ||
'commandParams', | ||
'params', | ||
]; | ||
function setupArguments(target, propertyName, descriptor) { | ||
const method = descriptor.value; | ||
descriptor.value = function () { | ||
const data = Reflect.getOwnMetadata('gotIndex', target, propertyName); | ||
const data = Reflect.getOwnMetadata('gotIndex', target, propertyName) || {}; | ||
const startArguments = { ...arguments }; | ||
@@ -10,0 +18,0 @@ Object.values(data) |
@@ -1,4 +0,5 @@ | ||
import { IConfig, IUser, IHandler } from '.'; | ||
import { ConfigTypes, IRunConfig, IHandler, IUser } from '.'; | ||
import { Api } from './classes'; | ||
import { Polling } from './classes/Launch/Polling'; | ||
import { Api } from './classes'; | ||
import { Webhook } from './classes/Launch/Webhook'; | ||
export declare class NestGram { | ||
@@ -8,8 +9,9 @@ private readonly token; | ||
private readonly config?; | ||
private readonly logging?; | ||
private readonly runConfig; | ||
handlers: IHandler[]; | ||
info: IUser; | ||
api: Api; | ||
polling: Polling; | ||
constructor(token: string, module?: any, config?: IConfig, logging?: true); | ||
polling?: Polling; | ||
webhook?: Webhook; | ||
constructor(token: string, module?: any, config?: ConfigTypes, runConfig?: IRunConfig); | ||
private setupImports; | ||
@@ -16,0 +18,0 @@ private setupModule; |
@@ -5,15 +5,22 @@ "use strict"; | ||
const logger_1 = require("./logger"); | ||
const classes_1 = require("./classes"); | ||
const Polling_1 = require("./classes/Launch/Polling"); | ||
const classes_1 = require("./classes"); | ||
const Webhook_1 = require("./classes/Launch/Webhook"); | ||
(0, logger_1.clear)(); | ||
class NestGram { | ||
constructor(token, module, config, logging) { | ||
constructor(token, module, config, runConfig = { port: 80, runType: 'polling', logging: true }) { | ||
this.token = token; | ||
this.module = module; | ||
this.config = config; | ||
this.logging = logging; | ||
this.runConfig = runConfig; | ||
this.handlers = []; | ||
this.api = new classes_1.Api(this.token); | ||
(0, logger_1.clear)(); | ||
if (logging) | ||
if (runConfig.logging) | ||
(0, logger_1.log)('blue', 'Bot initialized'); | ||
if (!runConfig.port) | ||
runConfig.port = 80; | ||
if (!runConfig.runType) | ||
runConfig.runType = 'polling'; | ||
if (typeof runConfig.logging !== 'boolean') | ||
runConfig.logging = true; | ||
if (module) | ||
@@ -39,2 +46,5 @@ this.setupEntry(module); | ||
}); | ||
const needApi = Reflect.getMetadata('getApi', controller, 'api'); | ||
if (needApi) | ||
controller.api = this.api; | ||
}); | ||
@@ -50,7 +60,7 @@ } | ||
this.setupModule(Module); | ||
if (this.logging) | ||
if (this.runConfig.logging) | ||
(0, logger_1.log)('blue', 'Entry module configured'); | ||
} | ||
async start() { | ||
if (this.logging) | ||
if (this.runConfig.logging) | ||
(0, logger_1.log)('blue', 'Starting bot...'); | ||
@@ -60,4 +70,12 @@ if (!this.token) | ||
this.info = await this.api.call(this.token, 'getMe'); | ||
this.polling = new Polling_1.Polling(this.token, this.handlers, this.config, this.logging); | ||
this.polling.start(); | ||
if (this.runConfig.runType === 'polling') { | ||
await this.api.deleteWebhook(this.runConfig); | ||
this.polling = new Polling_1.Polling(this.token, this.handlers, this.config, this.runConfig.logging); | ||
this.polling.start(); | ||
} | ||
else if (this.runConfig.runType === 'webhook') { | ||
if (!('url' in this.config)) | ||
throw (0, logger_1.error)('If you want to use webhooks, you need to pass webhook url in config'); | ||
this.webhook = new Webhook_1.Webhook(this.token, this.handlers, this.config, 80, this.runConfig.logging); | ||
} | ||
(0, logger_1.log)('green', 'Bot started on', `@${this.info.username}`.gray); | ||
@@ -64,0 +82,0 @@ return this.info.username; |
@@ -0,2 +1,14 @@ | ||
export declare type RunTypes = 'polling' | 'webhook'; | ||
export declare type ConfigTypes = IPollingConfig | IWebhookConfig; | ||
interface IGlobalRunnerConfig { | ||
allowed_updates?: string[]; | ||
} | ||
interface IGlobalWebhookConfig { | ||
drop_pending_updates?: boolean; | ||
} | ||
export interface IConfig { | ||
type: RunTypes; | ||
config: ConfigTypes; | ||
} | ||
export interface IPollingConfig extends IGlobalRunnerConfig { | ||
offset?: number; | ||
@@ -7,1 +19,15 @@ limit?: number; | ||
} | ||
export interface IWebhookConfig extends IGlobalRunnerConfig, IGlobalWebhookConfig { | ||
url: string; | ||
certificate?: any; | ||
ip_address?: string; | ||
max_connections?: number; | ||
} | ||
export interface IRunConfig extends IGlobalWebhookConfig { | ||
runType?: RunTypes; | ||
logging?: true; | ||
port?: number; | ||
} | ||
export interface IDeleteWebhookConfig extends IGlobalWebhookConfig { | ||
} | ||
export {}; |
@@ -15,3 +15,5 @@ import { ControllerClass } from './decorators.types'; | ||
Answer, | ||
IMessageEntity[] | undefined | ||
IMessageEntity[] | undefined, | ||
string[], | ||
any | ||
]; | ||
@@ -18,0 +20,0 @@ export declare type ContentTypes = Media | string | undefined | null; |
import { Answer, IUpdate } from '..'; | ||
export declare type NextFunction = () => any | Promise<any>; | ||
export declare type MiddlewareFunction = (update: IUpdate, answer: Answer, next?: NextFunction, fail?: NextFunction) => any | Promise<any>; | ||
export declare type MiddlewareFunction = (update: IUpdate, answer: Answer, params: any, next?: NextFunction, fail?: NextFunction) => any | Promise<any>; |
{ | ||
"name": "nestgram", | ||
"description": "Framework for working with Telegram Bot API on TypeScript like Nest.js", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"main": "dist/index.js", | ||
@@ -44,5 +44,4 @@ "types": "dist/index.d.ts", | ||
"form-data": "^4.0.0", | ||
"logger": "^0.0.1", | ||
"reflect-metadata": "^0.1.13" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
113099
4
129
1405
1
- Removedlogger@^0.0.1
- Removedlogger@0.0.1(transitive)