fix slack

This commit is contained in:
jxxghp 2023-06-15 17:30:31 +08:00
parent 1d835d68b8
commit 237fe69d82
3 changed files with 64 additions and 35 deletions

View File

@ -182,6 +182,7 @@ class Command(metaclass=Singleton):
command['func'](data_str) command['func'](data_str)
else: else:
command['func']() command['func']()
logger.info(f"{command.get('description')} 执行完成")
except Exception as err: except Exception as err:
logger.error(f"执行命令 {cmd} 出错:{str(err)}") logger.error(f"执行命令 {cmd} 出错:{str(err)}")
traceback.print_exc() traceback.print_exc()

View File

@ -56,6 +56,22 @@ class SlackModule(_ModuleBase):
'event_ts': '1670143568.444289', 'event_ts': '1670143568.444289',
'channel_type': 'im' 'channel_type': 'im'
} }
# 命令
{
"token": "",
"team_id": "",
"team_domain": "",
"channel_id": "",
"channel_name": "directmessage",
"user_id": "",
"user_name": "",
"command": "/subscribes",
"text": "",
"api_app_id": "",
"is_enterprise_install": "false",
"response_url": "",
"trigger_id": ""
}
# 快捷方式 # 快捷方式
{ {
"type": "shortcut", "type": "shortcut",
@ -151,6 +167,10 @@ class SlackModule(_ModuleBase):
userid = msg_json.get("user", {}).get("id") userid = msg_json.get("user", {}).get("id")
text = msg_json.get("callback_id") text = msg_json.get("callback_id")
username = msg_json.get("user", {}).get("username") username = msg_json.get("user", {}).get("username")
elif msg_json.get("command"):
userid = msg_json.get("user_id")
text = msg_json.get("command")
username = msg_json.get("user_name")
else: else:
return None return None
logger.info(f"收到Slack消息userid={userid}, username={username}, text={text}") logger.info(f"收到Slack消息userid={userid}, username={username}, text={text}")

View File

@ -62,6 +62,12 @@ class Slack:
local_res = requests.post(self._ds_url, json=body, timeout=10) local_res = requests.post(self._ds_url, json=body, timeout=10)
logger.debug("message: %s processed, response is: %s" % (body, local_res.text)) logger.debug("message: %s processed, response is: %s" % (body, local_res.text))
@slack_app.command(re.compile(r"/*"))
def slack_command(ack, body):
ack()
local_res = requests.post(self._ds_url, json=body, timeout=10)
logger.debug("message: %s processed, response is: %s" % (body, local_res.text))
# 启动服务 # 启动服务
try: try:
self._service = SocketModeHandler( self._service = SocketModeHandler(
@ -101,24 +107,26 @@ class Slack:
else: else:
# 消息广播 # 消息广播
channel = self.__find_public_channel() channel = self.__find_public_channel()
# 拼装消息内容 # 消息文本
block = { message_text = ""
"type": "section", # 结构体
"text": { blocks = []
"type": "mrkdwn", if not image:
"text": f"*{title}*\n{text or ''}" message_text = f"*{title}*\n{text or ''}"
} else:
}
# 消息图片 # 消息图片
if image: if image:
block['accessory'] = { # 拼装消息内容
blocks.append({"type": "section", "text": {
"type": "mrkdwn",
"text": f"*{title}*\n{text or ''}"
}, 'accessory': {
"type": "image", "type": "image",
"image_url": f"{image}", "image_url": f"{image}",
"alt_text": f"{title}" "alt_text": f"{title}"
} }})
blocks = [block]
# 链接 # 链接
if image and url: if url:
blocks.append({ blocks.append({
"type": "actions", "type": "actions",
"elements": [ "elements": [
@ -138,7 +146,7 @@ class Slack:
# 发送 # 发送
result = self._client.chat_postMessage( result = self._client.chat_postMessage(
channel=channel, channel=channel,
text=f"*{title}*\n{text or ''}", text=message_text,
blocks=blocks blocks=blocks
) )
return True, result return True, result