Escape html text in openai chatbot

Thanks to mas bagus yg sudah ingetin 🤣

Signed-off-by: Yasir Aris M <git@yasirdev.my.id>
This commit is contained in:
Yasir Aris M 2023-12-14 07:20:40 +07:00 committed by GitHub
parent fc58079ee9
commit 593527f5a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,7 +7,7 @@ import html
import random import random
from openai import AsyncOpenAI, APIConnectionError, RateLimitError, APIStatusError from openai import AsyncOpenAI, APIConnectionError, RateLimitError, APIStatusError
from pyrogram import filters from pyrogram import filters, enums
from pyrogram.errors import MessageTooLong from pyrogram.errors import MessageTooLong
from pyrogram.types import Message from pyrogram.types import Message
@ -34,7 +34,7 @@ async def bard_chatbot(_, ctx: Message, strings):
) )
random_choice = random.choice(req.json().get("choices")) random_choice = random.choice(req.json().get("choices"))
await msg.edit_msg( await msg.edit_msg(
random_choice["content"][0] html.escape(random_choice["content"][0])
if req.json().get("content") != "" if req.json().get("content") != ""
else "Failed getting data from Bard" else "Failed getting data from Bard"
) )
@ -50,7 +50,7 @@ async def openai_chatbot(_, ctx: Message, strings):
strings("no_question").format(cmd=ctx.command[0]), quote=True, del_in=5 strings("no_question").format(cmd=ctx.command[0]), quote=True, del_in=5
) )
if not OPENAI_API: if not OPENAI_API:
return await ctx.reply_msg("OPENAI_API env is mising!!!") return await ctx.reply_msg("OPENAI_API env is missing!!!")
uid = ctx.from_user.id if ctx.from_user else ctx.sender_chat.id uid = ctx.from_user.id if ctx.from_user else ctx.sender_chat.id
is_in_gap, _ = await check_time_gap(uid) is_in_gap, _ = await check_time_gap(uid)
if is_in_gap and (uid not in SUDO): if is_in_gap and (uid not in SUDO):
@ -73,10 +73,10 @@ async def openai_chatbot(_, ctx: Message, strings):
num += 1 num += 1
answer += chunk.choices[0].delta.content answer += chunk.choices[0].delta.content
if num == 30: if num == 30:
await msg.edit_msg(answer) await msg.edit_msg(html.escape(answer), parse_mode=enums.ParseMode.HTML)
await asyncio.sleep(1.5) await asyncio.sleep(1.5)
num = 0 num = 0
await msg.edit_msg(answer) await msg.edit_msg(html.escape(answer), parse_mode=enums.ParseMode.HTML)
except MessageTooLong: except MessageTooLong:
answerlink = await post_to_telegraph( answerlink = await post_to_telegraph(
False, "MissKaty ChatBot ", html.escape(f"<code>{answer}</code>") False, "MissKaty ChatBot ", html.escape(f"<code>{answer}</code>")
@ -91,3 +91,5 @@ async def openai_chatbot(_, ctx: Message, strings):
await msg.edit_msg("A 429 status code was received; we should back off a bit.") await msg.edit_msg("A 429 status code was received; we should back off a bit.")
except APIStatusError as e: except APIStatusError as e:
await msg.edit_msg(f"Another {e.status_code} status code was received with response {e.response}") await msg.edit_msg(f"Another {e.status_code} status code was received with response {e.response}")
except Exception as e:
await msg.edit_msg(f"ERROR: {e}")