fix: fix crashes, use _U only

This commit is contained in:
Udit Karode 2023-04-14 15:51:07 +05:30
parent 0fff450e38
commit bc1f2901ba
No known key found for this signature in database
GPG Key ID: 864BAA48465205F0
5 changed files with 38 additions and 24 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
/variables.ts
/.env
/node_modules
/src/test.ts
/dist

View File

@ -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) {

View File

@ -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<Context<Update>> = new Telegraf(
process.env.TG_TOKEN!,

View File

@ -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);
});

View File

@ -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));
},
];