|
|
@@ -25,6 +25,14 @@ class Feishu:
|
|
|
# rank_bot
|
|
|
rank_monitor_bot = "https://open.feishu.cn/open-apis/bot/v2/hook/f9dae7ba-decf-436b-b438-41994c35af1e"
|
|
|
|
|
|
+ # 用户名与手机号映射
|
|
|
+ name_phone_dict = {
|
|
|
+ "卓异": "18624010360",
|
|
|
+ "俊辉": "18801281360",
|
|
|
+ "范军": "15200827642",
|
|
|
+ "林强": "15810236123",
|
|
|
+ }
|
|
|
+
|
|
|
def __init__(self):
|
|
|
self.token = None
|
|
|
self.headers = {"Content-Type": "application/json"}
|
|
|
@@ -46,6 +54,36 @@ class Feishu:
|
|
|
tenant_access_token = response["tenant_access_token"]
|
|
|
self.token = tenant_access_token
|
|
|
|
|
|
+ async def get_user_open_id(self, username):
|
|
|
+ """
|
|
|
+ 根据用户名获取飞书 open_id
|
|
|
+
|
|
|
+ :param username: 用户名
|
|
|
+ :return: open_id
|
|
|
+ """
|
|
|
+ if not self.token:
|
|
|
+ await self.fetch_token()
|
|
|
+
|
|
|
+ mobile = self.name_phone_dict.get(username)
|
|
|
+ if not mobile:
|
|
|
+ return None
|
|
|
+
|
|
|
+ url = "https://open.feishu.cn/open-apis/user/v1/batch_get_id"
|
|
|
+ headers = {
|
|
|
+ "Authorization": f"Bearer {self.token}",
|
|
|
+ "Content-Type": "application/json; charset=utf-8",
|
|
|
+ }
|
|
|
+ params = {"mobiles": [mobile]}
|
|
|
+
|
|
|
+ async with AsyncHttpClient() as client:
|
|
|
+ response = await client.get(url=url, params=params, headers=headers)
|
|
|
+
|
|
|
+ try:
|
|
|
+ open_id = response["data"]["mobile_users"][mobile][0]["open_id"]
|
|
|
+ return open_id
|
|
|
+ except (KeyError, IndexError):
|
|
|
+ return None
|
|
|
+
|
|
|
|
|
|
class FeishuSheetApi(Feishu):
|
|
|
async def prepend_value(self, sheet_token, sheet_id, ranges, values):
|
|
|
@@ -87,6 +125,24 @@ class FeishuSheetApi(Feishu):
|
|
|
|
|
|
|
|
|
class FeishuBotApi(Feishu):
|
|
|
+ async def create_mention_text(self, usernames):
|
|
|
+ """
|
|
|
+ 创建 @ 用户的文本
|
|
|
+
|
|
|
+ :param usernames: 用户名列表
|
|
|
+ :return: @ 用户的 markdown 文本
|
|
|
+ """
|
|
|
+ if not usernames:
|
|
|
+ return ""
|
|
|
+
|
|
|
+ mention_parts = []
|
|
|
+ for username in usernames:
|
|
|
+ open_id = await self.get_user_open_id(username)
|
|
|
+ if open_id:
|
|
|
+ mention_parts.append(f"<at id={open_id}></at>")
|
|
|
+
|
|
|
+ return " ".join(mention_parts) + "\n" if mention_parts else ""
|
|
|
+
|
|
|
@classmethod
|
|
|
def create_feishu_columns_sheet(
|
|
|
cls,
|
|
|
@@ -151,14 +207,30 @@ class FeishuBotApi(Feishu):
|
|
|
}
|
|
|
|
|
|
# 表格形式
|
|
|
- def create_feishu_table(self, title, columns, rows, mention):
|
|
|
+ async def create_feishu_table(self, title, columns, rows, mention, mention_users=None):
|
|
|
+ """
|
|
|
+ 创建飞书表格消息
|
|
|
+
|
|
|
+ :param title: 标题
|
|
|
+ :param columns: 列定义
|
|
|
+ :param rows: 行数据
|
|
|
+ :param mention: 是否 @ 所有人
|
|
|
+ :param mention_users: 要 @ 的具体用户列表,例如 ["mark"]
|
|
|
+ """
|
|
|
+ mention_element = self.not_mention
|
|
|
+ if mention:
|
|
|
+ mention_element = self.mention_all
|
|
|
+ elif mention_users:
|
|
|
+ mention_text = await self.create_mention_text(mention_users)
|
|
|
+ mention_element = {"content": mention_text, "tag": "lark_md"}
|
|
|
+
|
|
|
table_base = {
|
|
|
"header": {
|
|
|
"template": "blue",
|
|
|
"title": {"content": title, "tag": "plain_text"},
|
|
|
},
|
|
|
"elements": [
|
|
|
- self.mention_all if mention else self.not_mention,
|
|
|
+ mention_element,
|
|
|
{
|
|
|
"tag": "table",
|
|
|
"page_size": len(rows) + 1,
|
|
|
@@ -178,15 +250,27 @@ class FeishuBotApi(Feishu):
|
|
|
}
|
|
|
return table_base
|
|
|
|
|
|
- def create_feishu_bot_obj(self, title, mention, detail):
|
|
|
+ async def create_feishu_bot_obj(self, title, mention, detail, mention_users=None):
|
|
|
"""
|
|
|
create feishu bot object
|
|
|
+
|
|
|
+ :param title: 标题
|
|
|
+ :param mention: 是否 @ 所有人
|
|
|
+ :param detail: 详细内容
|
|
|
+ :param mention_users: 要 @ 的具体用户列表,例如 ["luojunhui"]
|
|
|
"""
|
|
|
+ mention_element = self.not_mention
|
|
|
+ if mention:
|
|
|
+ mention_element = self.mention_all
|
|
|
+ elif mention_users:
|
|
|
+ mention_text = await self.create_mention_text(mention_users)
|
|
|
+ mention_element = {"content": mention_text, "tag": "lark_md"}
|
|
|
+
|
|
|
return {
|
|
|
"elements": [
|
|
|
{
|
|
|
"tag": "div",
|
|
|
- "text": self.mention_all if mention else self.not_mention,
|
|
|
+ "text": mention_element,
|
|
|
},
|
|
|
{
|
|
|
"tag": "div",
|
|
|
@@ -201,8 +285,24 @@ class FeishuBotApi(Feishu):
|
|
|
|
|
|
# bot
|
|
|
async def bot(
|
|
|
- self, title, detail, mention=True, table=False, env="long_articles_task"
|
|
|
+ self,
|
|
|
+ title,
|
|
|
+ detail,
|
|
|
+ mention=True,
|
|
|
+ table=False,
|
|
|
+ env="long_articles_task",
|
|
|
+ mention_users=None,
|
|
|
):
|
|
|
+ """
|
|
|
+ 发送飞书机器人消息
|
|
|
+
|
|
|
+ :param title: 标题
|
|
|
+ :param detail: 详细内容
|
|
|
+ :param mention: 是否 @ 所有人
|
|
|
+ :param table: 是否为表格形式
|
|
|
+ :param env: 环境,决定发送到哪个机器人
|
|
|
+ :param mention_users: 要 @ 的具体用户列表,例如 ["luojunhui"]
|
|
|
+ """
|
|
|
match env:
|
|
|
case "dev":
|
|
|
url = self.long_articles_bot_dev
|
|
|
@@ -223,15 +323,16 @@ class FeishuBotApi(Feishu):
|
|
|
|
|
|
headers = {"Content-Type": "application/json"}
|
|
|
if table:
|
|
|
- card = self.create_feishu_table(
|
|
|
+ card = await self.create_feishu_table(
|
|
|
title=title,
|
|
|
columns=detail["columns"],
|
|
|
rows=detail["rows"],
|
|
|
mention=mention,
|
|
|
+ mention_users=mention_users,
|
|
|
)
|
|
|
else:
|
|
|
- card = self.create_feishu_bot_obj(
|
|
|
- title=title, mention=mention, detail=detail
|
|
|
+ card = await self.create_feishu_bot_obj(
|
|
|
+ title=title, mention=mention, detail=detail, mention_users=mention_users
|
|
|
)
|
|
|
|
|
|
data = {"msg_type": "interactive", "card": card}
|