Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a logic error during sign verifying card message & card message model version update & sample code update #67

Open
wants to merge 3 commits into
base: v2_main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,22 +320,38 @@ app = Flask(__name__)
def do_interactive_card(data: lark.Card) -> Any:
print(lark.JSON.marshal(data))
content = {
"header": {
"title": {
"tag": "plain_text",
"content": "更新卡片成功"
},
"template": "green"
"toast":{
"type":"info",
"content":"卡片交互成功",
"i18n":{
"zh_cn":"卡片交互成功",
"en_us":"card action success"
}
},
"elements": [
{
"tag": "div",
"text": {
"tag": "lark_md",
"content": "**Success!\n成功啦😄**"
"card":{
"type":"raw",
"data":{
"config":{
"enable_forward":True
},
"elements":[
{
"tag":"div",
"text":{
"content":"This is the plain text",
"tag":"plain_text"
}
}
],
"header":{
"template":"blue",
"title":{
"content":"This is the title",
"tag":"plain_text"
}
}
},
]
}
}
}
return content

Expand Down
4 changes: 2 additions & 2 deletions lark_oapi/card/action_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def _verify_sign(self, request: RawRequest) -> None:
timestamp = request.headers.get(LARK_REQUEST_TIMESTAMP)
nonce = request.headers.get(LARK_REQUEST_NONCE)
signature = request.headers.get(LARK_REQUEST_SIGNATURE)
bs = (timestamp + nonce + self._verification_token).encode(UTF_8) + request.body
h = hashlib.sha1(bs)
bs = (timestamp + nonce + self._encrypt_key).encode(UTF_8) + request.body
h = hashlib.sha256(bs)
if signature != h.hexdigest():
raise AccessDeniedException("signature verification failed")

Expand Down
71 changes: 61 additions & 10 deletions lark_oapi/card/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,82 @@
from lark_oapi.core.model import RawRequest


class Action(object):
class EventOperator(object):
_types = {}

def __init__(self, d=None) -> None:
self.tenant_key: Optional[str] = None
self.user_id: Optional[str] = None
self.open_id: Optional[str] = None
init(self, d, self._types)


class EventAction(object):
_types = {}

def __init__(self, d=None) -> None:
self.value: Dict[str, Any] = {}
self.tag: Optional[str] = None
self.option: Optional[str] = None
self.form_value: Dict[str, Any] = {}
self.timezone: Optional[str] = None
self.name: Optional[str] = None
self.tag: Optional[str] = None
self.option: Dict[str, Any] = {}
init(self, d, self._types)


class EventContext(object):
_types = {}

def __init__(self, d=None) -> None:
self.url: Optional[str] = None
self.preview_token: Optional[str] = None
self.open_message_id: Optional[str] = None
self.open_chat_id: Optional[str] = None
init(self, d, self._types)


class Header(object):
_types = {}

def __init__(self, d=None) -> None:
self.event_id: Optional[str] = None
self.token: Optional[str] = None
self.create_time: Optional[str] = None
self.event_type: Optional[str] = None
self.tenant_key: Optional[str] = None
self.app_id: Optional[str] = None
init(self, d, self._types)


class Event(object):
_types = {
"operator": EventOperator,
"action": EventAction,
"context": EventContext
}

def __init__(self, d=None) -> None:
self.operator: Optional[EventOperator] = None
self.token: Optional[str] = None
self.action: Optional[EventAction] = None
self.host: Optional[str] = None
self.delivery_type: Optional[str] = None
self.context: Optional[EventContext] = None
init(self, d, self._types)


class Card(object):
_types = {
"action": Action
"header": Header,
"event": Event
}

def __init__(self, d=None) -> None:
self.open_id: Optional[str] = None
self.user_id: Optional[str] = None
self.tenant_key: Optional[str] = None
self.open_message_id: Optional[str] = None
self.open_chat_id: Optional[str] = None
self.schema: Optional[str] = None
self.header: Optional[Header] = None
self.event: Optional[Event] = None
self.token: Optional[str] = None
self.challenge: Optional[str] = None
self.type: Optional[str] = None
self.action: Optional[Action] = None
self.raw: Optional[RawRequest] = None
init(self, d, self._types)
44 changes: 30 additions & 14 deletions samples/card/flask_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,38 @@
def do_interactive_card(data: lark.Card) -> Any:
print(lark.JSON.marshal(data))
content = {
"header": {
"title": {
"tag": "plain_text",
"content": "更新卡片成功"
},
"template": "green"
"toast":{
"type":"info",
"content":"卡片交互成功",
"i18n":{
"zh_cn":"卡片交互成功",
"en_us":"card action success"
}
},
"elements": [
{
"tag": "div",
"text": {
"tag": "lark_md",
"content": "**Success!\n成功啦😄**"
"card":{
"type":"raw",
"data":{
"config":{
"enable_forward":True
},
"elements":[
{
"tag":"div",
"text":{
"content":"This is the plain text",
"tag":"plain_text"
}
}
],
"header":{
"template":"blue",
"title":{
"content":"This is the title",
"tag":"plain_text"
}
}
},
]
}
}
}
return content

Expand Down