From c9bc52ef3058218171c90afb8944c2f48407e130 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Thu, 28 Dec 2023 20:38:13 +0800 Subject: [PATCH] feat: update --- pdm.lock | 6 ++--- pyproject.toml | 2 +- requirements.txt | 2 +- server/app.py | 58 ++++++++++++++++++++++++++---------------------- server/env.py | 3 +++ server/server.py | 8 ++----- 6 files changed, 41 insertions(+), 38 deletions(-) create mode 100644 server/env.py diff --git a/pdm.lock b/pdm.lock index d6a208cb..7a464ca2 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default"] strategy = ["cross_platform"] lock_version = "4.4" -content_hash = "sha256:e804c28637907a7db21acfb4cf9cd6516d4de53e9d951b6b8e5a106d82d01871" +content_hash = "sha256:c6d5001ad8007f670b048d2f4d38cf3bdcfc2b5188d7e7bb794c36e214176bcf" [[package]] name = "anyio" @@ -35,7 +35,7 @@ files = [ [[package]] name = "ca-lark-oauth" -version = "0.0.4" +version = "0.0.5" requires_python = ">=3.8" summary = "lark(feishu) oauth webhook" dependencies = [ @@ -43,7 +43,7 @@ dependencies = [ "flask", ] files = [ - {file = "ca-lark-oauth-0.0.4.tar.gz", hash = "sha256:71926c700da858960143640b102d1c0f74777e087107b4648cf079f74e34c208"}, + {file = "ca-lark-oauth-0.0.5.tar.gz", hash = "sha256:dc6b7c583642b3139716b2fd430d53b777d71a867483687c3f128f5992bd802f"}, ] [[package]] diff --git a/pyproject.toml b/pyproject.toml index 55c3340a..2b2909f9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ authors = [ ] dependencies = [ "python-dotenv>=1.0.0", - "ca-lark-oauth>=0.0.4", + "ca-lark-oauth==0.0.5", "ca-lark-webhook>=0.0.3", ] requires-python = ">=3.10" diff --git a/requirements.txt b/requirements.txt index 0ff0cc12..b2e5575a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ anyio==4.2.0 blinker==1.7.0 -ca-lark-oauth==0.0.4 +ca-lark-oauth==0.0.5 ca-lark-sdk==0.0.7 ca-lark-webhook==0.0.3 certifi==2023.11.17 diff --git a/server/app.py b/server/app.py index c61c8179..3a6286c7 100644 --- a/server/app.py +++ b/server/app.py @@ -1,32 +1,36 @@ import os + from connectai.lark.oauth import Server as OauthServer from connectai.lark.sdk import Bot, MarketBot from connectai.lark.webhook import LarkServer -from connectai.storage import ExpiredDictStorage - - -def get_app(): - hook = LarkServer() - oauth = OauthServer() - bot = Bot( - app_id=os.environ.get("APP_ID"), - app_secret=os.environ.get("APP_SECRET"), - encrypt_key=os.environ.get("ENCRYPT_KEY"), - verification_token=os.environ.get("VERIFICATION_TOKEN") - ) - @hook.on_bot_message(message_type="text", bot=bot) - def on_text_message(bot, message_id, content, *args, **kwargs): - text = content["text"] - print("reply_text", message_id, text) - bot.reply_text(message_id, "reply: " + text) - - @oauth.on_bot_event(event_type="oauth:user_info", bot=bot) - def on_oauth_user_info(bot, event_id, user_info, *args, **kwargs): - # oauth user_info - print("oauth", user_info) - return user_info - - app = oauth.get_app() - app.register_blueprint(hook.get_blueprint()) - return app +from flask import Flask + +app = Flask(__name__) + +hook = LarkServer(prefix="/api/feishu/hook") +oauth = OauthServer(prefix="/api/feishu/oauth") + +bot = Bot( + app_id=os.environ.get("APP_ID"), + app_secret=os.environ.get("APP_SECRET"), + encrypt_key=os.environ.get("ENCRYPT_KEY"), + verification_token=os.environ.get("VERIFICATION_TOKEN"), +) + + +@hook.on_bot_message(message_type="text", bot=bot) +def on_text_message(bot, message_id, content, *args, **kwargs): + text = content["text"] + print("reply_text", message_id, text) + bot.reply_text(message_id, "reply: " + text) + + +@oauth.on_bot_event(event_type="oauth:user_info", bot=bot) +def on_oauth_user_info(bot, event_id, user_info, *args, **kwargs): + # oauth user_info + print("oauth", user_info) + return user_info + +app.register_blueprint(oauth.get_blueprint()) +app.register_blueprint(hook.get_blueprint()) diff --git a/server/env.py b/server/env.py new file mode 100644 index 00000000..3d5905ba --- /dev/null +++ b/server/env.py @@ -0,0 +1,3 @@ +from dotenv import find_dotenv, load_dotenv + +load_dotenv(find_dotenv()) diff --git a/server/server.py b/server/server.py index 76eaab77..b9f1902e 100644 --- a/server/server.py +++ b/server/server.py @@ -1,12 +1,8 @@ import os -from dotenv import find_dotenv, load_dotenv -from app import get_app - -load_dotenv(find_dotenv()) -app = get_app() +import env +from app import app if __name__ == "__main__": # gunicorn -w 1 -b :8888 "server:app" app.run(host=os.environ.get("HOST", "0.0.0.0"), port=os.environ.get("PORT", 8888)) -