Skip to content

🐛 Lark API send_message may need receive_id_type parameter fix #7

Description

@IvyGain

Description

The send_message method in lark_client.py may not correctly handle different receive_id types (chat_id vs open_id vs user_id).

Problem

In lark_client.py lines 119-139:

async def send_message(
    self, 
    chat_id: str, 
    message: str, 
    message_type: str = "text"
) -> Dict[str, Any]:
    """Send a message to a chat or user."""
    content = {"text": message} if message_type == "text" else {"content": message}
    
    data = {
        "receive_id": chat_id,
        "msg_type": message_type,
        "content": json.dumps(content)
    }
    
    return await self._make_request(
        "POST",
        "/im/v1/messages",
        data=data,
        params={"receive_id_type": "chat_id"}  # Always hardcoded as chat_id
    )

Issues:

  1. The receive_id_type is hardcoded to "chat_id", but the receive_id from webhook events might be:

    • chat_id (group chats)
    • open_id (1-on-1 chats with users)
  2. For 1-on-1 bot chats, the correct receive_id_type might need to be "open_id" or the method needs to auto-detect the type.

Impact

When users send messages in 1-on-1 chats, the bot may fail to respond because it's using the wrong receive_id_type.

Solution

async def send_message(
    self, 
    receive_id: str, 
    message: str, 
    message_type: str = "text",
    receive_id_type: str = "chat_id"  # Allow caller to specify
) -> Dict[str, Any]:
    """Send a message to a chat or user."""
    content = {"text": message} if message_type == "text" else {"content": message}
    
    data = {
        "receive_id": receive_id,
        "msg_type": message_type,
        "content": json.dumps(content)
    }
    
    return await self._make_request(
        "POST",
        "/im/v1/messages",
        data=data,
        params={"receive_id_type": receive_id_type}
    )

Or auto-detect based on ID prefix patterns:

  • oc_* → chat_id
  • ou_* → open_id
  • on_* → union_id

Priority

MEDIUM - May cause message delivery failures in certain chat types

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions