Skip to content

Commit

Permalink
🐛 Fix webhook server response
Browse files Browse the repository at this point in the history
  • Loading branch information
mullwar committed Apr 19, 2021
1 parent e14f362 commit 6850a98
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
3 changes: 1 addition & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { TelegramErrorResponse } from "./types/telegram";
import { AxiosError, AxiosResponse, Method } from "axios";

export const ERROR_TELEBOT_ALREADY_RUNNING = "Telebot is already running. Terminate instance for safety.";
export const ERROR_TELEBOT_MAXIMUM_RETRY = "Maximum retries exceeded. Terminate instance for safety.";
export const ERROR_TELEBOT_MAXIMUM_RETRY = "Maximum retries exceeded. Terminate instance for safety."; // TODO

export type SomeKindOfError = TeleBotError | TelegramError | Error | EvalError | RangeError | ReferenceError |
SyntaxError | TypeError | URIError;
Expand Down
17 changes: 5 additions & 12 deletions src/telebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ import {
User,
WebhookInfo
} from "./types/telegram";
import {
ERROR_TELEBOT_ALREADY_RUNNING,
handleTelegramResponse,
normalizeError,
SomeKindOfError,
TeleBotError
} from "./errors";
import { handleTelegramResponse, normalizeError, SomeKindOfError, TeleBotError } from "./errors";
import { Levels, LID, TeleBotLogger, TeleBotLogOptions } from "./telebot/logger";
import { TELEGRAM_UPDATE_PROCESSORS, TelegramUpdateProcessors } from "./telebot/processors";
import { ALLOWED_WEBHOOK_PORTS, craftWebhookPath, creteWebhookServer } from "./telebot/webhook";
Expand Down Expand Up @@ -203,7 +197,6 @@ export class TeleBot {
} = this.polling;

const webhook = this.webhook;
const deleteWebhook = async () => await this.deleteWebhook();

if (!this.hasFlag("isRunning")) {
this.setFlag("isRunning");
Expand All @@ -230,24 +223,24 @@ export class TeleBot {
}

} else if (interval && interval > 0) {
await deleteWebhook();
await this.deleteWebhook();
this.startLifeInterval(interval);
} else if (interval === false) {
await deleteWebhook();
await this.deleteWebhook();
this.startLifeCycle();
}
} catch (e) {
const error = normalizeError(e);
this.dispatch("error", error);
this.logger.error(LID.TeleBot, { error });
this.stop();
// eslint-disable-next-line no-console
console.error("==== TELEBOT GLOBAL ERROR ====", error);
this.stop();
return Promise.reject(error);
}
} else {
this.stop();
throw new TeleBotError(ERROR_TELEBOT_ALREADY_RUNNING);
throw new TeleBotError("Telebot is already running. Terminate instance for safety.");
}

}
Expand Down
34 changes: 15 additions & 19 deletions src/telebot/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,31 @@ export function craftWebhookPath(url: string, token: string): string {
const listener = (bot: TeleBot, path: string) => (request: IncomingMessage, response: ServerResponse) => {
if (request.url === path && request.method === "POST") {
const body: string[] = [];

request.on("data", (chunk) => body.push(chunk));
request.on("end", () => {
let update: Update[];

try {
update = convertToArray<Update>(JSON.parse(body.join()));
const update = convertToArray<Update>(JSON.parse(body.join()));

bot.logger.debug(LID.Webhook, { meta: { update } });

bot.processTelegramUpdates(update).catch((e) => {
const error = normalizeError(e);
bot.logger.error(LID.Webhook, { error });
response.writeHead(500);
return error;
}).finally(() => {
response.end();
});
} catch (error) {
bot.logger.error(LID.Webhook, { error });
response.writeHead(415);
response.end();
return error;
}

bot.logger.debug(LID.Webhook, { meta: { update } });

bot.processTelegramUpdates(update).catch((e) => {
const error = normalizeError(e);
bot.logger.error(LID.Webhook, { error });
response.writeHead(500);
return error;
}).finally(() => {
response.end();
});
});
} else {
response.writeHead(404);
response.end();
}

response.writeHead(404);
response.end();
};

export function creteWebhookServer(bot: TeleBot, options: WebhookServer & { url: string }): Promise<void> {
Expand Down

0 comments on commit 6850a98

Please sign in to comment.