Skip to content

Commit

Permalink
feat: add onStart hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Feb 24, 2024
1 parent 161ae65 commit 69a9f22
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
33 changes: 29 additions & 4 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ export class Bot<
},
],
onError: [],
onStart: [],
};

constructor(token: string, options?: Omit<BotOptions, "token">) {
this.options = { ...options, token };
}

private async runHooks<
T extends Exclude<keyof Hooks.Store<Errors>, "onError">,
T extends Exclude<keyof Hooks.Store<Errors>, "onError" | "onStart">,
>(type: T, context: Parameters<Hooks.Store<Errors>[T][0]>[0]) {
let data = context;

Expand All @@ -90,9 +91,11 @@ export class Bot<
}

private async runImmutableHooks<
T extends Extract<keyof Hooks.Store<Errors>, "onError">,
T extends Extract<keyof Hooks.Store<Errors>, "onError" | "onStart">,
>(type: T, context: Parameters<Hooks.Store<Errors>[T][0]>[0]) {
for await (const hook of this.hooks[type]) {
//TODO: solve that later
//@ts-expect-error
await hook(context);
}
}
Expand Down Expand Up @@ -272,6 +275,12 @@ export class Bot<
return this;
}

onStart(handler: Hooks.OnStart) {
this.hooks.onStart.push(handler);

return this;
}

on<T extends UpdateName>(
updateName: MaybeArray<T>,
handler: Handler<
Expand Down Expand Up @@ -340,17 +349,33 @@ export class Bot<
drop_pending_updates: dropPendingUpdates,
});

return this.updates.startPolling({
await this.updates.startPolling({
allowed_updates: allowedUpdates,
});

this.runImmutableHooks("onStart", {
plugins: this.dependencies,
info: this.info,
updatesFrom: "long-polling",
});

return this.info;
}

if (this.updates.isStarted) this.updates.stopPolling();

return this.api.setWebhook({
await this.api.setWebhook({
...webhook,
drop_pending_updates: dropPendingUpdates,
allowed_updates: allowedUpdates,
});

this.runImmutableHooks("onStart", {
plugins: this.dependencies,
info: this.info,
updatesFrom: "long-polling",
});

return this.info;
}
}
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BotLike, Context, UpdateName } from "@gramio/contexts";
import { APIMethodParams, APIMethods } from "@gramio/types";
import { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types";
import { NextMiddleware } from "middleware-io";
import { TelegramError } from "./errors";

Expand Down Expand Up @@ -57,9 +57,16 @@ export namespace Hooks {
Ctx extends Context<BotLike> = Context<BotLike>,
> = (options: OnErrorContext<Ctx, T>) => unknown;

export type OnStart = (context: {
plugins: string[];
info: TelegramUser;
updatesFrom: "webhook" | "long-polling";
}) => unknown;

export interface Store<T extends ErrorDefinitions> {
preRequest: PreRequest[];
onError: OnError<T>[];
onStart: OnStart[];
}
}

Expand Down

0 comments on commit 69a9f22

Please sign in to comment.