feat: add support for slack

This commit is contained in:
wangcham
2025-03-30 22:24:53 -04:00
parent c0dbf6fd13
commit be1328cee9
4 changed files with 40 additions and 24 deletions

View File

@@ -17,23 +17,33 @@ class SlackClient():
self._message_handlers = {
"example":[],
}
self.bot_user_id = None # avoid block
self.bot_user_id = None # 避免机器人回复自己的消息
async def handle_callback_request(self):
try:
body = await request.get_data()
data = json.loads(body)
print("shoudao:")
print(data)
if 'type' in data:
if data['type'] == 'url_verification':
return data['challenge']
bot_user_id = data.get("event",{}).get("bot_id","")
if self.bot_user_id and bot_user_id == self.bot_user_id:
return jsonify({'status': 'ok'})
if data and data.get("event", {}).get("channel_type") in ["im", "channel"]:
# 处理私信
if data and data.get("event", {}).get("channel_type") in ["im"]:
event = SlackEvent.from_payload(data)
await self._handle_message(event)
return jsonify({'status': 'ok'})
#处理群聊
if data.get("event",{}).get("type") == 'app_mention':
data.setdefault("event", {})["channel_type"] = "channel"
event = SlackEvent.from_payload(data)
await self._handle_message(event)
return jsonify({'status':'ok'})
except Exception as e:
raise(e)
@@ -66,10 +76,6 @@ class SlackClient():
)
if self.bot_user_id is None and response.get("ok"):
self.bot_user_id = response["message"]["bot_id"]
print("bot_id:")
print(self.bot_user_id)
print("fanhui:")
print(response)
return
except Exception as e:
raise e
@@ -82,8 +88,6 @@ class SlackClient():
)
if self.bot_user_id is None and response.get("ok"):
self.bot_user_id = response["message"]["bot_id"]
print("bot_id:")
print(self.bot_user_id)
return
except Exception as e:

View File

@@ -16,11 +16,9 @@ class SlackEvent(dict):
for el in elements:
if el.get("type") == "text":
return el.get("text", "")
if self.get("event",{}).get("channel_type") == "channel":
elements = self["event"]["blocks"][0]["elements"][0]["elements"]
text_result = next((el["text"] for el in elements if el["type"] == "text"), "")
return text_result
if self.get("event",{}).get("channel_type") == 'channel':
message_text = next((el["text"] for block in self.get("event", {}).get("blocks", []) if block.get("type") == "rich_text" for element in block.get("elements", []) if element.get("type") == "rich_text_section" for el in element.get("elements", []) if el.get("type") == "text"), "")
return message_text
return ""
@@ -49,7 +47,7 @@ class SlackEvent(dict):
files = self.get("event", {}).get("files", [])
if files:
return files[0].get("url_private", "")
return ""
return None
@property