Skip to content

Commit

Permalink
add promise responses
Browse files Browse the repository at this point in the history
  • Loading branch information
disizali committed Jun 19, 2021
1 parent ab9d663 commit f075d60
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
7 changes: 7 additions & 0 deletions examples/weather-bot/bot.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from "../../packages/common/mod.ts";
import { WeatherController } from "./weather.controller.ts";

@Module({
controllers: [WeatherController],
})
export class BotModule {}
9 changes: 9 additions & 0 deletions examples/weather-bot/main.ts
Original file line number Diff line number Diff line change
@@ -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();
37 changes: 37 additions & 0 deletions examples/weather-bot/weather.controller.ts
Original file line number Diff line number Diff line change
@@ -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));
4 changes: 2 additions & 2 deletions packages/core/controllers/listener-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class ListenerBuilder {
callback,
);

return (context: Context) => {
return async (context: Context) => {
const canActivate = this.guardsConsumer.tryActivate(
guards,
controllerInstance,
Expand All @@ -108,7 +108,7 @@ export class ListenerBuilder {
);
return;
}
const callbackResponse = this.listenerProxy.call(
const callbackResponse = await this.listenerProxy.call(
controllerInstance,
callback,
paramArgs,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/controllers/listener-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ 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[],
context: Context,
exceptionHandler: ExceptionHandler,
) {
try {
return callback.call(controllerInstance, ...paramArgs);
return await callback.call(controllerInstance, ...paramArgs);
} catch (err) {
exceptionHandler.handle(err, context, controllerInstance, callback);
}
Expand Down

0 comments on commit f075d60

Please sign in to comment.