Skip to content

Commit

Permalink
feat: 增加chat菜单
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Jan 6, 2024
1 parent ae55235 commit 5b47602
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions server/tasks/lark/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .chat import *
from .lark import *
from .manage import *
76 changes: 76 additions & 0 deletions server/tasks/lark/chat.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 3 additions & 1 deletion server/utils/lark/chat_manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def __init__(
value={
"key": "value", # TODO
},
),
)
if len(actions) > 0
else None,
),
FeishuMessageDiv(
content=f"** ⚡️ 前往 GitHub 查看 Repo 主页 **\n*话题下回复「/view」 *",
Expand Down
10 changes: 9 additions & 1 deletion server/utils/lark/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 5b47602

Please sign in to comment.