2024-10-25 17:35:12 +00:00
|
|
|
import { Bot, Context, session, BotError, GrammyError, HttpError } from "https://deno.land/x/grammy/mod.ts";
|
2024-10-19 10:00:35 +00:00
|
|
|
import { Ctx, defaultSessionData } from "./ctx.ts";
|
2024-10-23 16:09:10 +00:00
|
|
|
import * as config from "../cfg/config.ts"
|
2024-10-19 10:00:35 +00:00
|
|
|
import { ERR_CODES, Err } from "../utils/errors.ts";
|
|
|
|
|
|
|
|
class BotUnknownOnStartErr extends Err {
|
|
|
|
code: ERR_CODES = ERR_CODES.UnknownErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const runBot = async (cfg: config.BotConfig, initMode: (bot: Bot<Ctx>) => void) => {
|
|
|
|
const bot = new Bot<Ctx>(cfg.bot_token)
|
|
|
|
bot.use(session({ initial: defaultSessionData }))
|
|
|
|
bot.catch((err: BotError<Context>) => {
|
2024-10-25 17:35:12 +00:00
|
|
|
const ctx = err.ctx;
|
|
|
|
console.error(`Error while handling update ${ctx.update.update_id}:`);
|
|
|
|
const e = err.error;
|
|
|
|
if (e instanceof GrammyError) {
|
|
|
|
if (e.error_code === 400 && e.method === "deleteMessage") return
|
|
|
|
console.error("Error in request:", e.description);
|
|
|
|
} else if (e instanceof HttpError) {
|
|
|
|
console.error("Could not contact Telegram:", e);
|
|
|
|
} else {
|
|
|
|
throw new BotUnknownOnStartErr("Unknown error while starting the bot",
|
|
|
|
{ cause: err })
|
|
|
|
}
|
2024-10-19 10:00:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
initMode(bot)
|
2024-10-24 08:35:13 +00:00
|
|
|
await bot.init()
|
2024-10-19 10:00:35 +00:00
|
|
|
|
2024-10-24 08:35:13 +00:00
|
|
|
console.log(`starting bot ${bot.botInfo.username}`)
|
2024-10-25 17:35:12 +00:00
|
|
|
await bot.start().catch(err => console.log(err))
|
2024-10-19 10:00:35 +00:00
|
|
|
}
|