自定义飞书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;

?>
相关推荐
奔跑吧 android35 分钟前
【android bluetooth 协议分析 18】【PBAP详解 2】【车机为何不显示电话号码为空的联系人信息】
android·蓝牙电话·hfp·pbap·电话簿
深盾科技35 分钟前
安卓二次打包技术深度拆解:从逆向篡改到防护逻辑
android
4Forsee36 分钟前
【Android】消息机制
android·java·前端
2501_915921432 小时前
iOS 虚拟位置设置实战,多工具协同打造精准调试与场景模拟环境
android·ios·小程序·https·uni-app·iphone·webview
龚礼鹏2 小时前
Android 图像显示框架三——演示demo以及解析
android·交互
QuantumLeap丶3 小时前
《Flutter全栈开发实战指南:从零到高级》- 11 -状态管理Provider
android·flutter·ios
百锦再3 小时前
第6章 结构体与方法
android·java·c++·python·rust·go
gustt3 小时前
用小程序搭建博客首页:从数据驱动到界面展示
android·前端·微信小程序
金鸿客3 小时前
Compose从相册和系统相机拍照获取照片
android
IT乐手4 小时前
Android 获取定位信息工具类
android