diff --git a/.gitignore b/.gitignore index b1e6766..06b8ff6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/variables.ts +/.env /node_modules /src/test.ts /dist diff --git a/src/ai.ts b/src/ai.ts index 108d202..400d6ba 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -77,8 +77,13 @@ export async function ai( await edit(getTgResponse(bingRes, prompt)); } catch (e) { + console.log("[local] caught error:"); console.log(e); - await edit("Something went wrong!"); + try { + await edit( + "Unable to obtain a response from Bing due to a technical error.", + ); + } catch {} } // signal completion, let the next client proceed @@ -97,15 +102,18 @@ function getTgResponse(bingRes: ChatMessage | Error, prompt: string) { } if (!bingRes.text) { - return "Received an empty response. Make sure the bot is set up properly and that you haven't crossed the daily message limit."; + return "Received an empty response. Make sure the bot is set up properly and that you haven't crossed the daily message limit. You can also try starting a /newchat."; } return transformBingResponse(bingRes); } export function newChat(chatId: number) { - delete chats[chatId].res; - chats[chatId].index = 1; + const chat = chats[chatId]; + if (chat) { + delete chat.res; + chat.index = 1; + } } export function getVariant(chatId: number) { diff --git a/src/config.ts b/src/config.ts index a48534e..c415903 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,7 +5,18 @@ import { Update } from "typegram"; dotenv.config(); -export const bingChat = new BingChat({ cookie: process.env.BING_COOKIE! }); +const bingCookieConfig = process.env.BING_COOKIE!.trim().replace(/['"]/g, ""); +export const bingChat = new BingChat({ + cookie: bingCookieConfig.includes(";") + ? // this is the entire `document.cookie` + (bingCookieConfig + .split(";") + .map(text => text.split("=")) + .find(entry => entry[0] === " _U" || entry[0] === "_U") ?? [])[1] ?? + bingCookieConfig + : // this is the _U cookie + bingCookieConfig, +}); export const bot: Telegraf> = new Telegraf( process.env.TG_TOKEN!, diff --git a/src/index.ts b/src/index.ts index 4659e11..b0c9f71 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,9 +16,7 @@ async function main() { console.log("Usage allowed in all chats"); } - bot.command("ai", async ctx => { - await ai(ctx, args(ctx.message.text)); - }); + bot.command("ai", async ctx => ai(ctx, args(ctx.message.text))); bot.on(message("reply_to_message"), async ctx => { if (ctx.message.reply_to_message.from?.id != ctx.botInfo.id) return; @@ -26,7 +24,7 @@ async function main() { const reply = ctx.message.reply_to_message; const message = ctx.message; if ("text" in reply && "text" in message) { - await ai(ctx, message.text); + ai(ctx, message.text); } }); @@ -80,6 +78,7 @@ The variant command accepts the name of any of these 3 variants in a case-insens }); bot.catch(err => { + console.log("[global] caught error:"); console.log(err); }); diff --git a/src/transformers.ts b/src/transformers.ts index aaac151..4664c90 100644 --- a/src/transformers.ts +++ b/src/transformers.ts @@ -38,6 +38,16 @@ export const transformers: Transformers = [ return replace(reply, /\*(.*?)\*/g, (_, txt) => italic(txt)); }, + function stylePre(reply) { + return replace( + reply, + /```(\w+)\n([\s\S]*?)```/g, + (_, language, codeString) => { + return pre(language)(codeString); + }, + ); + }, + function styleCode(reply) { return replace(reply, /`(.*?)`/g, (_, codeString) => code(codeString)); }, @@ -52,18 +62,4 @@ export const transformers: Transformers = [ return fmt` ${bold(link(`[${oneBasedIndex}]`, referenceLink))}`; }); }, - - function stylePre(reply) { - return replace( - reply, - /```(\w+)\n([\s\S]*?)```/g, - (_, language, codeString) => { - return pre(language)(codeString); - }, - ); - }, - - function styleAlternateBold(reply) { - return replace(reply, /^`\n(.*?)`/gms, (_, txt) => bold(txt)); - }, ];