diff --git a/server/tasks/lark/__init__.py b/server/tasks/lark/__init__.py index b7ed7df4..c0675ea1 100644 --- a/server/tasks/lark/__init__.py +++ b/server/tasks/lark/__init__.py @@ -1,2 +1,3 @@ +from .chat import * from .lark import * from .manage import * diff --git a/server/tasks/lark/chat.py b/server/tasks/lark/chat.py new file mode 100644 index 00000000..182ee436 --- /dev/null +++ b/server/tasks/lark/chat.py @@ -0,0 +1,76 @@ +from celery_app import app, celery +from connectai.lark.sdk import Bot +from model.schema import ChatGroup, Repo, Team, db +from sqlalchemy.orm import aliased +from utils.lark.chat_manual import ChatManual +from utils.lark.chat_tip_failed import ChatTipFailed + +from .manage import get_bot_by_application_id + + +@celery.task() +def send_chat_failed_tip(content, app_id, message_id, *args, bot=None, **kwargs): + """send new repo card message to user. + + Args: + app_id: IMApplication.app_id. + message_id: lark message id. + content: error message + """ + if not bot: + bot, _ = get_bot_by_application_id(app_id) + message = ChatTipFailed(content=content) + return bot.reply(message_id, message).json() + + +@celery.task() +def send_chat_manual(app_id, message_id, content, data, *args, **kwargs): + chat_id = data["event"]["message"]["chat_id"] + chat_group = ( + db.session.query(ChatGroup) + .filter( + ChatGroup.chat_id == chat_id, + ChatGroup.status == 0, + ) + .first() + ) + if not chat_group: + return send_chat_failed_tip( + "找不到项目群", app_id, message_id, content, data, *args, **kwargs + ) + repo = ( + db.session.query(Repo) + .filter( + Repo.id == chat_group.repo_id, + Repo.status == 0, + ) + .first() + ) + if not repo: + return send_chat_failed_tip( + "找不到项目群", app_id, message_id, content, data, *args, **kwargs + ) + bot, application = get_bot_by_application_id(app_id) + if not application: + return send_manage_fail_message( + "找不到对应的应用", app_id, message_id, content, data, *args, bot=bot, **kwargs + ) + + team = ( + db.session.query(Team) + .filter( + Team.id == application.team_id, + ) + .first() + ) + if not team: + return send_manage_fail_message( + "找不到对应的项目", app_id, message_id, content, data, *args, bot=bot, **kwargs + ) + + message = ManageManual( + repo_url=f"https://github.com/{team.name}/{repo.name}", + repo_name=repo.name, + actions=[], # TODO 获取actions + ) + return bot.reply(message_id, message).json() diff --git a/server/utils/lark/chat_manual.py b/server/utils/lark/chat_manual.py index e463083b..2a519f96 100644 --- a/server/utils/lark/chat_manual.py +++ b/server/utils/lark/chat_manual.py @@ -25,7 +25,9 @@ def __init__( value={ "key": "value", # TODO }, - ), + ) + if len(actions) > 0 + else None, ), FeishuMessageDiv( content=f"** ⚡️ 前往 GitHub 查看 Repo 主页 **\n*话题下回复「/view」 *", diff --git a/server/utils/lark/parser.py b/server/utils/lark/parser.py index bb41c473..48170db2 100644 --- a/server/utils/lark/parser.py +++ b/server/utils/lark/parser.py @@ -73,7 +73,15 @@ def init_subparsers(self): def on_help(self, param, unkown, *args, **kwargs): logging.info("on_help %r %r", vars(param), unkown) # TODO call task.delay - tasks.send_manage_manual.delay(*args, **kwargs) + try: + raw_message = args[3] + chat_type = raw_message["event"]["message"]["chat_type"] + if "p2p" == chat_type: + tasks.send_manage_manual.delay(*args, **kwargs) + else: + tasks.send_chat_manual.delay(*args, **kwargs) + except Exception as e: + logging.error(e) return "help", param, unkown def on_match(self, param, unkown, *args, **kwargs):