Skip to main content

KaiheilaCardBuilder

一个构造开黑啦卡片的工具

使用方法

from khl_card import *

# 新建卡片
# 这里构建了一个卡片,带有一个模块
card = Card(Section(Kmarkdown('测试卡片')))

# 给卡片添加模块,这里是分割线
card.append(Divider())

# 拥有的模块列表,具体用法参考开黑啦官方文档
['Header', 'Section', 'ImageGroup', 'Container', 'Context', 'ActionGroup', 'File', 'Audio', 'Video', 'Divider',
 'Invite', 'Countdown']

# 拥有的元素列表,具体用法参考开黑啦官方文档
['PlainText', 'Kmarkdown', 'Paragraph', 'Image', 'Button', '_BaseAccessory']

# 倒计时模块的简便创建
card.append(Countdown.new_day_countdown('2022-07-05 08:00:00'))
card.append(Countdown.new_hour_countdown('2022-07-05 08:00:00'))
card.append(Countdown.new_second_countdown('2022-07-05 08:00:00'))

# 现在支持链式调用
card.append(Section(Kmarkdown('调用1'))).append(Section(Kmarkdown('调用2'))).append(Section(Kmarkdown('调用3')))

# 构建卡片,返回的卡片的字典
card.build()

# 构建卡片,返回官方编辑器可以用的 json 文本
print(card.build_to_json())

# 输出:
{
    "type": "card",
    "theme": "primary",
    "size": "lg",
    "modules": [
        {
            "type": "section",
            "mode": "right",
            "text": {
                "type": "kmarkdown",
                "content": "测试卡片"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "countdown",
            "mode": "day",
            "endTime": 1656979200000,
            "startTime": 1655827836854
        },
        {
            "type": "countdown",
            "mode": "hour",
            "endTime": 1656979200000,
            "startTime": 1655827836854
        },
        {
            "type": "countdown",
            "mode": "second",
            "endTime": 1656979200000,
            "startTime": 1655827836891
        },
        {
            "type": "section",
            "mode": "right",
            "text": {
                "type": "kmarkdown",
                "content": "调用1"
            }
        },
        {
            "type": "section",
            "mode": "right",
            "text": {
                "type": "kmarkdown",
                "content": "调用2"
            }
        },
        {
            "type": "section",
            "mode": "right",
            "text": {
                "type": "kmarkdown",
                "content": "调用3"
            }
        }
    ]
}

# 现在可以使用 CardMessageBuilder 来构造卡片消息
cm = CardMessageBuilder().card(
    CardBuilder()
    .image_group(
        ImageGroupBuilder()
        .add(Image('http://img.sdadad'))
        .build()
    )
    .context(
        ContextBuilder()
        .add(Kmarkdown.bold('Test context: ') + Kmarkdown('abcde'))  # 现在 KMD 也能够使用 + 拼接了
        .build()
    )
    .divider()
    .invite('asfws66')
    .container(
        ContainerBuilder()
        .add(Image('http://img.sdadad'))
        .add(Image('http://img.sdadad'))
        .build()
    )
    .header('This is a header')
    .section(Kmarkdown('This is a section'))
    .file('file_url', 'title')
    .audio('src', 'title', 'cover')
    .build()
).build()

print(cm.build_to_json())


# 输出:

[
    {
        "type": "card",
        "theme": "primary",
        "size": "lg",
        "modules": [
            {
                "type": "image-group",
                "elements": [
                    {
                        "type": "image",
                        "src": "http://img.sdadad",
                        "alt": "",
                        "size": "lg",
                        "circle": false
                    }
                ]
            },
            {
                "type": "context",
                "elements": [
                    {
                        "type": "kmarkdown",
                        "content": "**Test context: **abcde"
                    }
                ]
            },
            {
                "type": "divider"
            },
            {
                "type": "invite",
                "code": "asfws66"
            },
            {
                "type": "container",
                "elements": [
                    {
                        "type": "image",
                        "src": "http://img.sdadad",
                        "alt": "",
                        "size": "lg",
                        "circle": false
                    },
                    {
                        "type": "image",
                        "src": "http://img.sdadad",
                        "alt": "",
                        "size": "lg",
                        "circle": false
                    }
                ]
            },
            {
                "type": "header",
                "text": {
                    "type": "plain-text",
                    "content": "This is a header"
                }
            },
            {
                "type": "section",
                "mode": "right",
                "text": {
                    "type": "kmarkdown",
                    "content": "This is a section"
                }
            },
            {
                "type": "file",
                "src": "file_url",
                "title": "title"
            },
            {
                "type": "audio",
                "src": "src",
                "title": "title",
                "cover": "cover"
            }
        ]
    }
]

更新日志

1.3.0

添加 CardMessageBuilder CardBuilder ImageGroupBuilder ContainerBuilder ContextBuilder ActionGroupBuilder 来快捷的构造卡片

添加KMD的特殊用法快捷,KMD能够使用 + 拼接。例:Kmarkdown.bold('Test context: ') + Kmarkdown('abcde')

1.2.5

添加 CardMessage, Button 可以使用 ThemeTypes

1.2.4

Section 能够使用结构体

1.2.3

修改结构,Paragraph 不应该属于文字类元素

1.2.2

添加 Card 的链式调用

1.2.1

现在 Card 构建使用可变参数

1.1.1

卡片按钮现在使用 PlainText 或 Kmarkdown

1.1.0

添加 Card 的一些方法

修复 Button 的构建 bug

修复 Color 中 __str__ 的问题

为倒计时模块添加快速创建方法

from khl_card.modules import *

Countdown.new_day_countdown('2022-05-05 08:00:00')
Countdown.new_hour_countdown('2022-05-05 08:00:00')
Countdown.new_second_countdown('2022-05-05 08:00:00')

Metadata

Release files for kaiheilacardbuilder-m 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kaiheilacardbuilder-m 1.3.0
File Size Uploaded
kaiheilacardbuilder_m-1.3.0.tar.gz 12.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kaiheilacardbuilder-m 1.3.0
File Interpreter ABI Platform
kaiheilacardbuilder_m-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.7 kB

Release files / kaiheilacardbuilder_m-1.3.0.tar.gz

Download URL kaiheilacardbuilder_m-1.3.0.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ecb3b2d7f4a52bfe084c97d4cee1fb305852084f4566df233d83351b648186d3
BLAKE2b-256 checksum
How to use checksums
c8e0e1c69fd5bba8d4f981a0ca5c07b67723515aea14757eacdb3fc11b6e86c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.7.4 CPython/3.11.4

Release files / kaiheilacardbuilder_m-1.3.0-py3-none-any.whl

Download URL kaiheilacardbuilder_m-1.3.0-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bdf6f9d8acfa1677c68935d911c80208696695fbecaa36528be3938f0b2eccea
BLAKE2b-256 checksum
How to use checksums
d7bb66492167783e4297c0555e24b22413222146690303d882c94b9131e8f9b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.7.4 CPython/3.11.4

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page