You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.2KB

  1. import os
  2. import requests
  3. from dingtalk_stream import ChatbotMessage
  4. from bridge.context import ContextType
  5. from channel.chat_message import ChatMessage
  6. # -*- coding=utf-8 -*-
  7. from common.log import logger
  8. from common.tmp_dir import TmpDir
  9. class DingTalkMessage(ChatMessage):
  10. def __init__(self, event: ChatbotMessage, image_download_handler):
  11. super().__init__(event)
  12. self.image_download_handler = image_download_handler
  13. self.msg_id = event.message_id
  14. self.message_type = event.message_type
  15. self.incoming_message = event
  16. self.sender_staff_id = event.sender_staff_id
  17. self.other_user_id = event.conversation_id
  18. self.create_time = event.create_at
  19. self.image_content = event.image_content
  20. self.rich_text_content = event.rich_text_content
  21. if event.conversation_type == "1":
  22. self.is_group = False
  23. else:
  24. self.is_group = True
  25. if self.message_type == "text":
  26. self.ctype = ContextType.TEXT
  27. self.content = event.text.content.strip()
  28. elif self.message_type == "audio":
  29. # 钉钉支持直接识别语音,所以此处将直接提取文字,当文字处理
  30. self.content = event.extensions['content']['recognition'].strip()
  31. self.ctype = ContextType.TEXT
  32. elif (self.message_type == 'picture') or (self.message_type == 'richText'):
  33. self.ctype = ContextType.IMAGE
  34. # 钉钉图片类型或富文本类型消息处理
  35. image_list = event.get_image_list()
  36. if len(image_list) > 0:
  37. download_code = image_list[0]
  38. download_url = image_download_handler.get_image_download_url(download_code)
  39. self.content = download_image_file(download_url, TmpDir().path())
  40. else:
  41. logger.debug(f"[Dingtalk] messageType :{self.message_type} , imageList isEmpty")
  42. if self.is_group:
  43. self.from_user_id = event.conversation_id
  44. self.actual_user_id = event.sender_id
  45. self.is_at = True
  46. else:
  47. self.from_user_id = event.sender_id
  48. self.actual_user_id = event.sender_id
  49. self.to_user_id = event.chatbot_user_id
  50. self.other_user_nickname = event.conversation_title
  51. def download_image_file(image_url, temp_dir):
  52. headers = {
  53. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
  54. }
  55. # 设置代理
  56. # self.proxies
  57. # , proxies=self.proxies
  58. response = requests.get(image_url, headers=headers, stream=True, timeout=60 * 5)
  59. if response.status_code == 200:
  60. # 生成文件名
  61. file_name = image_url.split("/")[-1].split("?")[0]
  62. # 检查临时目录是否存在,如果不存在则创建
  63. if not os.path.exists(temp_dir):
  64. os.makedirs(temp_dir)
  65. # 将文件保存到临时目录
  66. file_path = os.path.join(temp_dir, file_name)
  67. with open(file_path, 'wb') as file:
  68. file.write(response.content)
  69. return file_path
  70. else:
  71. logger.info(f"[Dingtalk] Failed to download image file, {response.content}")
  72. return None