自定义飞书Webhook机器人api接口

自定义飞书Webhook机器人api接口

使用方法:

在网站目录新建一个名为api.php的文件,将以上代码粘贴进去即可

然后访问域名/api.php?title=洛小柒 - QQ沐编程&content=小柒祝大家新年快乐!&url=www.qqmu.com&type=1

title是标题

content是主体内容,支持html代码

url是点击按钮链接

type是卡片颜色,1是绿色,2是橙色,3是红色,没有值是黑色

效果截图

代码如下

php 复制代码
<?php
    function sendFeishuCardMessage($type,$title,$content) {
    // 飞书机器人的Webhook URL
    $webhookUrl = 'https://open.feishu.cn/open-apis/bot/v2/hook/405a03ae-8355-43ca-9422-a53181215ed6';
    // 示例调用
    $title = $_GET['title'] ?? '测试标题';// 标题
    $content = $_GET['content'] ?? '测试内容';// 内容
    $url = $_GET['url'] ?? '0';//按钮地址
    $type = $_GET['type']; // 可以是 'failure', 'warning', 'success'
    
    // 根据type变量设置卡片颜色和标题
    switch ($type) {
        case '3':
            $color = 'red';//红色
            $cardTitle = $title;
            break;
        case '2':
            $color = 'orange';//橙色
            $cardTitle = $title;
            break;
        case '1':
            $color = 'green';//绿色
            $cardTitle = $title;
            break;
        default:
            $color = 'grey';
            $cardTitle = $title;
            break;
    }

    // 构建卡片消息的JSON数据
    $data = [
        'msg_type' => 'interactive',
        'card' => [
            'config' => [
                'wide_screen_mode' => true,
                'enable_forward' => true
            ],
            'elements' => [
                [
                    'tag' => 'div',
                    'text' => [
                        'content' => $content,
                        'tag' => 'lark_md'
                    ]
                ],
                [
                    'tag' => 'action',
                    'actions' => [
                        [
                            'tag' => 'button',
                            'text' => [
                                'content' => '点击查看详情',//按钮文字
                                'tag' => 'plain_text'
                            ],
                            'type' => 'primary', // 按钮样式:primary(蓝色)、default(灰色)、danger(红色)
                            'url' => $url // 按钮点击后跳转的链接
                        ]
                    ]
                ]
            ],
            'header' => [
                'title' => [
                    'content' => $cardTitle,
                    'tag' => 'plain_text'
                ],
                'template' => $color
            ]
        ]
    ];

    // 发送HTTP POST请求
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $webhookUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

$response = sendFeishuCardMessage($type, $title, $content);
echo $response;

?>
相关推荐
智购科技无人售货机工厂8 分钟前
2026自动售货机热敏打印模块集成:从串口驱动到小票自动裁切的工程实践~YH
android·stm32·单片机·嵌入式硬件·系统架构
A13345551 小时前
苹果安卓手机播放器推荐:2026无广告4K怎么选
android·智能手机
hunterandroid2 小时前
ContentProvider 跨进程数据共享实战
android·前端
天空之城--3 小时前
Android输入法子系统深度解析
android
天空之城--4 小时前
Android全链路性能优化:从原理到实践的终极指南
android·性能优化
Meteors.4 小时前
Android 性能优化:05.CPU优化
android·性能优化
石头猫灯5 小时前
一次 CMS 被黑实录:完整事件思路梳理
android·学习
zhonyu鱼5 小时前
Escrcpy:用电脑键盘鼠标操作安卓手机的跨平台投屏工具
android·计算机外设·电脑
千里马学框架6 小时前
安卓开机性能优化:如何安全高效地裁剪 SystemService
android·智能手机·framework·wms·手机·性能·车载
Android打工仔6 小时前
一次 Android 拍照后卡顿的 Perfetto 定位与优化实践
android·性能优化