-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
255 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,16 @@ | ||
import os | ||
|
||
from connectai.lark.oauth import Server as OauthServer | ||
from connectai.lark.sdk import Bot, MarketBot | ||
from connectai.lark.webhook import LarkServer | ||
from flask import Flask, session | ||
from flask_cors import CORS | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
app = Flask(__name__) | ||
app.secret_key = (os.environ.get("SECRET_KEY"),) | ||
|
||
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"), | ||
app.secret_key = os.environ.get("SECRET_KEY") | ||
db = SQLAlchemy(app, engine_options={"isolation_level": "AUTOCOMMIT"}) | ||
CORS( | ||
app, allow_headers=["Authorization", "X-Requested-With"], supports_credentials=True | ||
) | ||
|
||
|
||
@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) | ||
# TODO | ||
session["user_id"] = user_info["union_id"] | ||
return user_info | ||
|
||
|
||
app.register_blueprint(oauth.get_blueprint()) | ||
app.register_blueprint(hook.get_blueprint()) | ||
gunicorn_logger = logging.getLogger("gunicorn.error") | ||
app.logger.handlers = gunicorn_logger.handlers | ||
app.logger.setLevel(gunicorn_logger.level) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import json | ||
import logging | ||
from datetime import datetime | ||
|
||
import bson | ||
from app import db | ||
from sqlalchemy import BINARY, String, text | ||
|
||
|
||
class ObjID(BINARY): | ||
"""基于bson.ObjectId用于mysql主键的自定义类型""" | ||
|
||
def bind_processor(self, dialect): | ||
def processor(value): | ||
return ( | ||
bson.ObjectId(value).binary if bson.ObjectId.is_valid(value) else value | ||
) | ||
|
||
return processor | ||
|
||
def result_processor(self, dialect, coltype): | ||
def processor(value): | ||
if not isinstance(value, bytes): | ||
value = bytes(value) | ||
return str(bson.ObjectId(value)) if bson.ObjectId.is_valid(value) else value | ||
|
||
return processor | ||
|
||
@staticmethod | ||
def new_id(): | ||
return str(bson.ObjectId()) | ||
|
||
@staticmethod | ||
def is_valid(value): | ||
return bson.ObjectId.is_valid(value) | ||
|
||
|
||
class JSONStr(String): | ||
"""自动转换 str 和 dict 的自定义类型""" | ||
|
||
def bind_processor(self, dialect): | ||
def processor(value): | ||
try: | ||
if isinstance(value, str) and (value[0] == "%" or value[-1] == "%"): | ||
# 使用like筛选的情况 | ||
return value | ||
return json.dumps(value, ensure_ascii=False) | ||
except Exception as e: | ||
logging.exception(e) | ||
return value | ||
|
||
return processor | ||
|
||
def result_processor(self, dialect, coltype): | ||
def processor(value): | ||
try: | ||
return json.loads(value) | ||
except Exception as e: | ||
logging.exception(e) | ||
return value | ||
|
||
return processor | ||
|
||
@staticmethod | ||
def is_valid(value): | ||
try: | ||
json.loads(value) | ||
return True | ||
except Exception as e: | ||
logging.exception(e) | ||
return False | ||
|
||
|
||
class User(db.Model): | ||
__tablename__ = "user" | ||
id = db.Column(ObjID(12), primary_key=True) | ||
openid = db.Column(db.String(128), nullable=True, comment="外部用户ID") | ||
name = db.Column(db.String(128), nullable=True, comment="用户名") | ||
extra = db.Column( | ||
JSONStr(1024), nullable=True, server_default=text("'{}'"), comment="用户其他字段" | ||
) | ||
status = db.Column(db.Integer, nullable=True, default=0, server_default=text("0")) | ||
created = db.Column(db.TIMESTAMP, nullable=False, default=datetime.utcnow) | ||
modified = db.Column( | ||
db.TIMESTAMP, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .lark import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import os | ||
|
||
from app import app | ||
from connectai.lark.oauth import Server as OauthServerBase | ||
from connectai.lark.sdk import Bot, MarketBot | ||
from connectai.lark.webhook import LarkServer as LarkServerBase | ||
|
||
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"), | ||
) | ||
|
||
|
||
class LarkServer(LarkServerBase): | ||
def get_bot(self, app_id): | ||
# TODO search in database and create Bot() | ||
return bot | ||
|
||
|
||
class OauthServer(OauthServerBase): | ||
def get_bot(self, app_id): | ||
# TODO search in database and create Bot() | ||
return bot | ||
|
||
|
||
hook = LarkServer(prefix="/api/feishu/hook") | ||
oauth = OauthServer(prefix="/api/feishu/oauth") | ||
|
||
|
||
@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) | ||
# TODO | ||
session["user_id"] = user_info["union_id"] | ||
return user_info | ||
|
||
|
||
app.register_blueprint(oauth.get_blueprint()) | ||
app.register_blueprint(hook.get_blueprint()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import os | ||
|
||
import env | ||
import routes | ||
from app import app | ||
|
||
if __name__ == "__main__": | ||
|