下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:8761
代码说明:这个实现包含三个主要模块,XML生成器核心类、常用卡片模板和示例使用代码。核心类提供了构建XML卡片的基础方法,模板类包含常用卡片类型,main.py展示了如何使用这些功能
import xml.etree.ElementTree as ET
from typing import Dict, List, Union
class QQCardGenerator:
"""QQ机器人XML卡片消息生成器"""
def __init__(self):
self.root = ET.Element("msg")
self.root.set("serviceID", "1")
self.root.set("templateID", "1")
self.root.set("action", "web")
self.root.set("brief", "卡片消息")
self.root.set("sourceMsgId", "0")
def add_text(self, text: str, size: int = 15, color: str = "#000000"):
"""添加文本元素"""
item = ET.SubElement(self.root, "item")
item.set("layout", "0")
text_elem = ET.SubElement(item, "text")
text_elem.set("fontSize", str(size))
text_elem.set("color", color)
text_elem.text = text
return self
def add_image(self, url: str, width: int = 200, height: int = 200):
"""添加图片元素"""
item = ET.SubElement(self.root, "item")
item.set("layout", "1")
image = ET.SubElement(item, "image")
image.set("url", url)
image.set("width", str(width))
image.set("height", str(height))
return self
def add_button(self, text: str, action: str, color: str = "#1296DB"):
"""添加按钮元素"""
item = ET.SubElement(self.root, "item")
item.set("layout", "2")
button = ET.SubElement(item, "button")
button.set("text", text)
button.set("action", action)
button.set("color", color)
return self
def add_progress(self, value: int, max_value: int = 100):
"""添加进度条"""
item = ET.SubElement(self.root, "item")
item.set("layout", "3")
progress = ET.SubElement(item, "progress")
progress.set("value", str(value))
progress.set("max", str(max_value))
return self
def generate_xml(self) -> str:
"""生成最终XML字符串"""
tree = ET.ElementTree(self.root)
ET.indent(tree, space=" ")
return ET.tostring(self.root, encoding="unicode")
xml_card_generator import QQCardGenerator
class CardTemplates:
@staticmethod
def create_welcome_card(user: str) -> str:
"""欢迎卡片模板"""
return (
QQCardGenerator()
.add_text(f"欢迎 {user} 加入群聊!", size=20, color="#FF0000")
.add_image("https://example.com/welcome.png")
.add_text("请阅读群规并遵守", size=15)
.add_button("查看群规", "open_url:https://example.com/rules")
.generate_xml()
)
@staticmethod
def create_vote_card(title: str, options: List[str]) -> str:
"""投票卡片模板"""
card = QQCardGenerator()
card.add_text(title, size=18, color="#0000FF")
for i, option in enumerate(options, 1):
card.add_text(f"{i}. {option}")
card.add_button(f"投票选项{i}", f"vote:{i}")
return card.generate_xml()
@staticmethod
def create_weather_card(city: str, data: Dict) -> str:
"""天气卡片模板"""
return (
QQCardGenerator()
.add_text(f"{city}天气预报", size=20)
.add_image(data["icon"])
.add_text(f"温度: {data['temp']}℃")
.add_text(f"天气: {data['weather']}")
.add_text(f"湿度: {data['humidity']}%")
.add_progress(data["aqi"], 500)
.generate_xml()
)
from xml_card_generator import QQCardGenerator
from card_templates import CardTemplates
自定义卡片示例
custom_card = (
QQCardGenerator()
.add_text("自定义卡片标题", size=20, color="#FF00FF")
.add_image("https://example.com/image.jpg", 300, 200)
.add_text("这是卡片内容区域")
.add_button("点击按钮", "open_url:https://example.com")
.add_progress(75)
.generate_xml()
)
print("自定义卡片XML:")
print(custom_card)
使用模板示例
welcome_card = CardTemplates.create_welcome_card("新用户")
vote_card = CardTemplates.create_vote_card(
"今天吃什么?",
["火锅", "烤肉", "日料", "快餐"]
)
print("\n欢迎卡片XML:")
print(welcome_card)
print("\n投票卡片XML:")
print(vote_card)