Skip to main content
Glama
cluic
by cluic

get_history

Retrieve recent chat history from WeChat conversations to provide context for AI assistants. Specify the number of messages to fetch for analysis or reference.

Instructions

获取历史消息。参数: n (获取数量,默认50)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nNo

Implementation Reference

  • Main handler function for the 'get_history' tool. Uses @tool decorator for registration with name 'get_history' and description. Takes optional parameter n (default 50) for number of messages to retrieve. Calls wrapper.get_history_messages(n=n) to fetch messages, then formats them as JSON with sender, content, and type fields.
    @tool(
        name="get_history",
        description="获取历史消息。参数: n (获取数量,默认50)"
    )
    def get_history(n: int = 50) -> str:
        """
        获取历史消息
    
        Args:
            n: 获取消息数量
    
        Returns:
            JSON 格式的消息列表
        """
        wrapper = _get_wrapper()
        if not wrapper:
            return format_result(False, "微信未初始化")
    
        try:
            messages = wrapper.get_history_messages(n=n)
            if not messages:
                return format_result(True, "无历史消息", {"messages": []})
    
            formatted = []
            for msg in messages:
                formatted.append({
                    "sender": msg.sender,
                    "content": truncate_text(msg.content, 200),
                    "type": msg.msg_type.value if isinstance(msg.msg_type, MessageType) else str(msg.msg_type),
                })
    
            return format_result(True, f"获取成功,共 {len(messages)} 条消息", {"messages": formatted})
        except Exception as e:
            logger.error(f"获取历史消息失败: {e}")
            return format_result(False, f"获取历史消息失败: {e}")
  • Helper method get_history_messages in WeChatWrapper class that wraps the wxautox4 GetHistoryMessage API. Decorated with @require_online to ensure WeChat is online. Converts raw messages from wxautox4 into MessageInfo objects with content, sender, msg_type, and attr fields.
    @require_online
    def get_history_messages(self, n: int = 50) -> list[MessageInfo]:
        """
        获取历史消息
    
        Args:
            n: 最大获取消息数量
    
        Returns:
            list[MessageInfo]: 消息列表
        """
        try:
            messages = self.wx.GetHistoryMessage(n=n)
            result = []
            for msg in messages:
                result.append(MessageInfo(
                    content=msg.content,
                    sender=msg.sender,
                    msg_type=MessageType(msg.type) if msg.type in MessageType.__members__ else MessageType.TEXT,
                    attr=MessageAttribute(msg.attr) if msg.attr in MessageAttribute.__members__ else MessageAttribute.FRIEND
                ))
            return result
        except Exception as e:
            logger.error(f"获取历史消息失败: {e}")
            return []
  • Dataclass schema definition for MessageInfo that represents a message object. Contains fields: content, sender, msg_type (MessageType enum), attr (MessageAttribute enum), timestamp, and chat_name. Includes to_dict() method for serialization.
    @dataclass
    class MessageInfo:
        """
        消息信息模型
    
        Attributes:
            content: 消息内容
            sender: 发送者
            msg_type: 消息类型
            attr: 消息属性
            timestamp: 时间戳
            chat_name: 所属聊天名称
        """
        content: str
        sender: str
        msg_type: MessageType
        attr: MessageAttribute
        timestamp: Optional[float] = None
        chat_name: Optional[str] = None
    
        def to_dict(self) -> dict[str, Any]:
            """转换为字典"""
            return {
                "content": self.content,
                "sender": self.sender,
                "type": self.msg_type.value if isinstance(self.msg_type, MessageType) else self.msg_type,
                "attr": self.attr.value if isinstance(self.attr, MessageAttribute) else self.attr,
                "timestamp": self.timestamp,
                "chat_name": self.chat_name
            }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cluic/wxauto-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server