2021-01-06 19:37:30 +03:00
|
|
|
import { ParamData } from '@nestjs/common';
|
2021-01-05 12:37:04 +03:00
|
|
|
import { ParamsFactory } from '@nestjs/core/helpers/external-context-creator';
|
|
|
|
import { Context } from 'telegraf';
|
2021-01-03 01:30:57 +03:00
|
|
|
import { TelegrafParamtype } from '../enums/telegraf-paramtype.enum';
|
|
|
|
|
2021-01-05 12:37:04 +03:00
|
|
|
export class TelegrafParamsFactory implements ParamsFactory {
|
|
|
|
exchangeKeyForValue(
|
|
|
|
type: TelegrafParamtype,
|
2021-01-06 19:37:30 +03:00
|
|
|
data: ParamData,
|
|
|
|
args: unknown[],
|
2021-01-05 12:37:04 +03:00
|
|
|
): unknown {
|
2021-01-06 19:37:30 +03:00
|
|
|
const ctx = args[0] as Context;
|
|
|
|
const next = args[1] as Function;
|
|
|
|
|
2021-01-05 12:37:04 +03:00
|
|
|
switch (type) {
|
2021-01-03 01:30:57 +03:00
|
|
|
case TelegrafParamtype.CONTEXT:
|
2021-01-05 12:37:04 +03:00
|
|
|
return ctx;
|
2021-01-03 01:30:57 +03:00
|
|
|
case TelegrafParamtype.NEXT:
|
2021-01-05 12:37:04 +03:00
|
|
|
return next;
|
2021-01-03 01:30:57 +03:00
|
|
|
case TelegrafParamtype.SENDER:
|
2021-01-06 20:49:50 +03:00
|
|
|
return ctx.from ? ctx.from[data as string] : ctx.from;
|
2021-01-03 01:30:57 +03:00
|
|
|
case TelegrafParamtype.MESSAGE:
|
2021-01-06 20:49:50 +03:00
|
|
|
return ctx.message ? ctx.message[data as string] : ctx.message;
|
2021-01-03 01:30:57 +03:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|