Add centralized pipeline fallback notifier
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import type {Message} from "typescript-telegram-bot-api";
|
||||
import {replyToMessage, logError} from "../../util/utils.js";
|
||||
import type {PipelineFallbackDecision} from "./fallback-executor.js";
|
||||
import {PipelineFallbackNotificationRegistry} from "./fallback-notifier-registry.js";
|
||||
import {resolvePipelineFallbackText} from "./fallback-notifier-text.js";
|
||||
|
||||
export class PipelineFallbackNotifier {
|
||||
private readonly registry = new PipelineFallbackNotificationRegistry();
|
||||
|
||||
constructor(
|
||||
private readonly sourceMessage: Message,
|
||||
private readonly sendFallbackMessage: (text: string) => Promise<void> = async text => {
|
||||
await replyToMessage({
|
||||
message: this.sourceMessage,
|
||||
text,
|
||||
});
|
||||
},
|
||||
) {}
|
||||
|
||||
async notify(requestId: string, decision: PipelineFallbackDecision): Promise<{notified: boolean; text?: string}> {
|
||||
if (!this.registry.claim(requestId, decision)) {
|
||||
return {notified: false};
|
||||
}
|
||||
|
||||
const text = resolvePipelineFallbackText(decision.stage, decision.action);
|
||||
if (!text) {
|
||||
return {notified: false};
|
||||
}
|
||||
|
||||
try {
|
||||
await this.sendFallbackMessage(text);
|
||||
return {notified: true, text};
|
||||
} catch (error) {
|
||||
logError(error instanceof Error ? error : String(error));
|
||||
return {notified: false, text};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user