Browse Source

图文发送

d1
H Vs 3 weeks ago
parent
commit
7b809e61c5
3 changed files with 41 additions and 8 deletions
  1. +10
    -2
      app/endpoints/pipeline_endpoint.py
  2. +28
    -1
      common/utils.py
  3. +3
    -5
      tasks.py

+ 10
- 2
app/endpoints/pipeline_endpoint.py View File

@@ -469,8 +469,16 @@ async def ai_chat_text_async(request: Request,token_id, app_id, wxid, msg_data,
# logger.info(f'wxid_mesh33pw13e721 不发送到微信有关{msg_content}的AI回复到微信')
# else:
# await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content)
await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content)
img_urls,reply_content=extract_and_replace_image_urls(reply_content)
if img_urls:
await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content)
await asyncio.sleep(random.uniform(1.5, 3))
for img_url in img_urls:
await request.app.state.gewe_service.post_image_async(token_id, app_id, callback_to_user, img_url)
await asyncio.sleep(random.uniform(1.5, 3))
else:
await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content)

await request.app.state.gewe_service.save_session_messages_to_cache_async(hash_key, {"role": "assistant", "content": reply_content})
# 回复的对话
input_wx_content_dialogue_message = [{"type": "text", "text": reply_content}]


+ 28
- 1
common/utils.py View File

@@ -421,4 +421,31 @@ async def save_to_local_from_url_async(url):
logger.error(f"无法下载文件,HTTP状态码:{response.status}")
return None
return tmp_file_path
return tmp_file_path


def extract_and_replace_image_url(text):
# 正则表达式匹配图片地址(png、jpg、jpeg)
pattern = r'https?://\S+\.(?:png|jpg|jpeg)'
# 查找匹配的图片地址
match = re.search(pattern, text)
if match:
image_url = match.group() # 获取图片地址
updated_text = text.replace(image_url, "如下图") # 替换图片地址
return image_url, updated_text
else:
return None, text # 没有匹配到图片时,返回原文本

def extract_and_replace_image_urls(text):
# 正则表达式匹配所有图片地址(png、jpg、jpeg)
pattern = r'https?://\S+\.(?:png|jpg|jpeg)'
# 查找所有匹配的图片地址
image_urls = re.findall(pattern, text)
# 用 "如下图" 替换所有匹配的图片地址
updated_text = re.sub(pattern, "如下图", text)
return image_urls, updated_text

+ 3
- 5
tasks.py View File

@@ -84,13 +84,13 @@ def background_worker_task(self, redis_config, kafka_config, gewe_config):

# @celery_app.task(name='tasks.scheduled_task', bind=True, acks_late=True)
# def scheduled_task(self):
# print("🚀 定时任务执行成功!~~~~~~~~~~~~~~~~~")
# print("定时任务执行成功!~~~~~~~~~~~~~~~~~")
# return "Hello from Celery Beat + RedBeat!"


# @celery_app.task(name='tasks.scheduled_task_sync_wx', bind=True, acks_late=True)
# def scheduled_task_sync_wx(self,redis_service,kafka_service,gewe_service):
# print("🚀 scheduled_task_sync_wx 定时任务执行成功!")
# print("scheduled_task_sync_wx 定时任务执行成功!")
# return "Hello from Celery Beat + RedBeat!"

# @celery_app.task(name='tasks.scheduled_task_sync_wx_info_1', bind=True, acks_late=True)
@@ -164,6 +164,7 @@ def scheduled_task_sync_wx_info(self, redis_config, kafka_config, gewe_config):
wxid = r.get("wxid")
status = r.get('status')
if status == '0':
logger.warning(f"微信号 {wxid} 已经离线: {ret}-{msg}")
continue
ret, msg, profile = await gewe_service.get_profile_async(token_id, app_id)
if ret != 200:
@@ -183,9 +184,6 @@ def scheduled_task_sync_wx_info(self, redis_config, kafka_config, gewe_config):
except Exception as e:
logger.error(f"任务执行过程中发生异常: {e}")

print("scheduled_task_sync_wx_info 定时任务执行成功!")
return "Hello from Celery Beat + RedBeat!"

loop = asyncio.get_event_loop()
if loop.is_closed():
loop = asyncio.new_event_loop()


Loading…
Cancel
Save