From f075d60c43edc4133b273ae20b9879c831cd7e64 Mon Sep 17 00:00:00 2001 From: "a.hassssani@gmail.com" Date: Sat, 19 Jun 2021 19:20:56 +0430 Subject: [PATCH] add promise responses --- examples/weather-bot/bot.module.ts | 7 ++++ examples/weather-bot/main.ts | 9 +++++ examples/weather-bot/weather.controller.ts | 37 +++++++++++++++++++ packages/core/controllers/listener-builder.ts | 4 +- packages/core/controllers/listener-proxy.ts | 4 +- 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 examples/weather-bot/bot.module.ts create mode 100644 examples/weather-bot/main.ts create mode 100644 examples/weather-bot/weather.controller.ts diff --git a/examples/weather-bot/bot.module.ts b/examples/weather-bot/bot.module.ts new file mode 100644 index 0000000..d41f940 --- /dev/null +++ b/examples/weather-bot/bot.module.ts @@ -0,0 +1,7 @@ +import { Module } from "../../packages/common/mod.ts"; +import { WeatherController } from "./weather.controller.ts"; + +@Module({ + controllers: [WeatherController], +}) +export class BotModule {} diff --git a/examples/weather-bot/main.ts b/examples/weather-bot/main.ts new file mode 100644 index 0000000..6b809fb --- /dev/null +++ b/examples/weather-bot/main.ts @@ -0,0 +1,9 @@ +import { TeaFactory } from "../../packages/core/factory.ts"; +import { BotModule } from "./bot.module.ts"; + +function bootstrap() { + const bot = TeaFactory.create(BotModule, "BOT_TOKEN"); + bot.start(); +} + +bootstrap(); diff --git a/examples/weather-bot/weather.controller.ts b/examples/weather-bot/weather.controller.ts new file mode 100644 index 0000000..d2334a2 --- /dev/null +++ b/examples/weather-bot/weather.controller.ts @@ -0,0 +1,37 @@ +import { + Command, + Context, + Controller, + UpdateType, +} from "../../packages/common/mod.ts"; + +const weatherConditions = ["☀️🥵☀️", "❄️☃️❄️"]; + +@Controller({ updateTypes: [UpdateType.MESSAGE] }) +export class WeatherController { + @Command("forecast") + async forecast(@Context() context: any) { + const randomIndex = getRandomIndex(weatherConditions); + if (randomIndex === 0) { + context.reply("The air is warming up, get ready to swim"); + } else { + context.reply( + "The weather is going to be cold, make sure you have warm clothes", + ); + } + await sleep(3000); + return context.reply(weatherConditions[randomIndex]); + } + + @Command("current") + current() { + const randomIndex = getRandomIndex(weatherConditions); + return Promise.resolve(weatherConditions[randomIndex]); + } +} + +const getRandomIndex = (array: unknown[]) => + Math.trunc(Math.random() * array.length); + +const sleep = (time: number) => + new Promise((resolve) => setTimeout(resolve, time)); diff --git a/packages/core/controllers/listener-builder.ts b/packages/core/controllers/listener-builder.ts index 0aade2e..1b47336 100644 --- a/packages/core/controllers/listener-builder.ts +++ b/packages/core/controllers/listener-builder.ts @@ -86,7 +86,7 @@ export class ListenerBuilder { callback, ); - return (context: Context) => { + return async (context: Context) => { const canActivate = this.guardsConsumer.tryActivate( guards, controllerInstance, @@ -108,7 +108,7 @@ export class ListenerBuilder { ); return; } - const callbackResponse = this.listenerProxy.call( + const callbackResponse = await this.listenerProxy.call( controllerInstance, callback, paramArgs, diff --git a/packages/core/controllers/listener-proxy.ts b/packages/core/controllers/listener-proxy.ts index 97a839c..ee2fd9e 100644 --- a/packages/core/controllers/listener-proxy.ts +++ b/packages/core/controllers/listener-proxy.ts @@ -3,7 +3,7 @@ import { Context } from "../adapters/telegram.abstract.ts"; import { Controller } from "../../common/interfaces/controllers/controller.interface.ts"; export class ListenerProxy { - call( + async call( controllerInstance: Controller, callback: (...args: any) => any, paramArgs: any[], @@ -11,7 +11,7 @@ export class ListenerProxy { exceptionHandler: ExceptionHandler, ) { try { - return callback.call(controllerInstance, ...paramArgs); + return await callback.call(controllerInstance, ...paramArgs); } catch (err) { exceptionHandler.handle(err, context, controllerInstance, callback); }