mirror of
https://github.com/hibobmaster/matrix_chatgpt_bot.git
synced 2024-10-01 05:35:36 -04:00
prevent blocking execution flow
This commit is contained in:
parent
51bde67149
commit
bbd45e78fa
39
bot.py
39
bot.py
@ -147,7 +147,6 @@ class Bot:
|
|||||||
if bard_token is not None:
|
if bard_token is not None:
|
||||||
self.bardbot = Bardbot(self.bard_token)
|
self.bardbot = Bardbot(self.bard_token)
|
||||||
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
try:
|
try:
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
@ -156,7 +155,6 @@ class Bot:
|
|||||||
asyncio.set_event_loop(loop)
|
asyncio.set_event_loop(loop)
|
||||||
loop.run_until_complete(self._close())
|
loop.run_until_complete(self._close())
|
||||||
|
|
||||||
|
|
||||||
async def _close(self):
|
async def _close(self):
|
||||||
await self.session.close()
|
await self.session.close()
|
||||||
|
|
||||||
@ -196,13 +194,13 @@ class Bot:
|
|||||||
prompt = n.group(1)
|
prompt = n.group(1)
|
||||||
if self.api_key != '':
|
if self.api_key != '':
|
||||||
try:
|
try:
|
||||||
await self.chat(room_id,
|
asyncio.create_task(self.chat(room_id,
|
||||||
reply_to_event_id,
|
reply_to_event_id,
|
||||||
prompt,
|
prompt,
|
||||||
sender_id,
|
sender_id,
|
||||||
raw_user_message
|
raw_user_message
|
||||||
)
|
)
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e, exc_info=True)
|
logger.error(e, exc_info=True)
|
||||||
await send_room_message(self.client, room_id, reply_message=str(e))
|
await send_room_message(self.client, room_id, reply_message=str(e))
|
||||||
@ -214,12 +212,13 @@ class Bot:
|
|||||||
if m:
|
if m:
|
||||||
prompt = m.group(1)
|
prompt = m.group(1)
|
||||||
try:
|
try:
|
||||||
await self.gpt(
|
asyncio.create_task(self.gpt(
|
||||||
room_id,
|
room_id,
|
||||||
reply_to_event_id,
|
reply_to_event_id,
|
||||||
prompt, sender_id,
|
prompt, sender_id,
|
||||||
raw_user_message
|
raw_user_message
|
||||||
)
|
)
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e, exc_info=True)
|
logger.error(e, exc_info=True)
|
||||||
await send_room_message(self.client, room_id, reply_message=str(e))
|
await send_room_message(self.client, room_id, reply_message=str(e))
|
||||||
@ -231,14 +230,14 @@ class Bot:
|
|||||||
prompt = b.group(1)
|
prompt = b.group(1)
|
||||||
# raw_content_body used for construct formatted_body
|
# raw_content_body used for construct formatted_body
|
||||||
try:
|
try:
|
||||||
await self.bing(
|
asyncio.create_task(self.bing(
|
||||||
room_id,
|
room_id,
|
||||||
reply_to_event_id,
|
reply_to_event_id,
|
||||||
prompt,
|
prompt,
|
||||||
sender_id,
|
sender_id,
|
||||||
raw_user_message
|
raw_user_message
|
||||||
)
|
)
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e, exc_info=True)
|
logger.error(e, exc_info=True)
|
||||||
await send_room_message(self.client, room_id, reply_message=str(e))
|
await send_room_message(self.client, room_id, reply_message=str(e))
|
||||||
@ -249,7 +248,7 @@ class Bot:
|
|||||||
if i:
|
if i:
|
||||||
prompt = i.group(1)
|
prompt = i.group(1)
|
||||||
try:
|
try:
|
||||||
await self.pic(room_id, prompt)
|
asyncio.create_task(self.pic(room_id, prompt))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e, exc_info=True)
|
logger.error(e, exc_info=True)
|
||||||
await send_room_message(self.client, room_id, reply_message=str(e))
|
await send_room_message(self.client, room_id, reply_message=str(e))
|
||||||
@ -260,23 +259,22 @@ class Bot:
|
|||||||
if b:
|
if b:
|
||||||
prompt = b.group(1)
|
prompt = b.group(1)
|
||||||
try:
|
try:
|
||||||
await self.bard(
|
asyncio.create_task(self.bard(
|
||||||
room_id,
|
room_id,
|
||||||
reply_to_event_id,
|
reply_to_event_id,
|
||||||
prompt,
|
prompt,
|
||||||
sender_id,
|
sender_id,
|
||||||
raw_user_message
|
raw_user_message
|
||||||
)
|
)
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e, exc_info=True)
|
logger.error(e, exc_info=True)
|
||||||
await send_room_message(self.client, room_id, reply_message={e})
|
await send_room_message(self.client, room_id, reply_message={e})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# help command
|
# help command
|
||||||
h = self.help_prog.match(content_body)
|
h = self.help_prog.match(content_body)
|
||||||
if h:
|
if h:
|
||||||
await self.help(room_id)
|
asyncio.create_task(self.help(room_id))
|
||||||
|
|
||||||
# message_callback decryption_failure event
|
# message_callback decryption_failure event
|
||||||
async def decryption_failure(self, room: MatrixRoom, event: MegolmEvent) -> None:
|
async def decryption_failure(self, room: MatrixRoom, event: MegolmEvent) -> None:
|
||||||
@ -528,7 +526,6 @@ class Bot:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(e)
|
raise Exception(e)
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
await send_room_message(self.client, room_id, reply_message=text,
|
await send_room_message(self.client, room_id, reply_message=text,
|
||||||
@ -549,7 +546,6 @@ class Bot:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(e)
|
raise Exception(e)
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
await send_room_message(self.client, room_id, reply_message=text,
|
await send_room_message(self.client, room_id, reply_message=text,
|
||||||
@ -570,7 +566,6 @@ class Bot:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(e)
|
raise Exception(e)
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
await send_room_message(self.client, room_id, reply_message=text,
|
await send_room_message(self.client, room_id, reply_message=text,
|
||||||
@ -590,12 +585,12 @@ class Bot:
|
|||||||
try:
|
try:
|
||||||
content = str(response['content']).strip()
|
content = str(response['content']).strip()
|
||||||
await send_room_message(self.client, room_id, reply_message=content,
|
await send_room_message(self.client, room_id, reply_message=content,
|
||||||
reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted)
|
reply_to_event_id="", sender_id=sender_id, user_message=raw_user_message, markdown_formatted=self.markdown_formatted)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e, exc_info=True)
|
logger.error(e, exc_info=True)
|
||||||
|
|
||||||
|
|
||||||
# !pic command
|
# !pic command
|
||||||
|
|
||||||
async def pic(self, room_id, prompt):
|
async def pic(self, room_id, prompt):
|
||||||
try:
|
try:
|
||||||
await self.client.room_typing(room_id, timeout=180000)
|
await self.client.room_typing(room_id, timeout=180000)
|
||||||
|
3
main.py
3
main.py
@ -1,7 +1,8 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
|
from functools import partial
|
||||||
from bot import Bot
|
from bot import Bot
|
||||||
from log import getlogger
|
from log import getlogger
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ async def send_room_message(client: AsyncClient,
|
|||||||
NORMAL_BODY = content = {"msgtype": "m.text", "body": reply_message, }
|
NORMAL_BODY = content = {"msgtype": "m.text", "body": reply_message, }
|
||||||
if reply_to_event_id == '':
|
if reply_to_event_id == '':
|
||||||
if markdown_formatted:
|
if markdown_formatted:
|
||||||
# only format message contains multiline codes
|
# only format message contains multiline codes, *, |
|
||||||
if re.search(r"```", reply_message) is not None:
|
if re.search(r"```|\*|\|", reply_message) is not None:
|
||||||
content = {
|
content = {
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": reply_message,
|
"body": reply_message,
|
||||||
|
Loading…
Reference in New Issue
Block a user