572
文章
·
32808
阅读
572
文章
·
32808
阅读

有51人阅读过 使用php中转webhook消息
发布于2024/10/24 更新于2025/01/10
[ 教程仅保证更新时有效,请自行测试。]

由于memos的webhook接口参数和企业微信bot的格式不一致,可使用此php简单转发post变动通知。

memos的webhook中填写此php的地址,往这个php上发请求即可

<?php

// 设置 Content-Type 为 JSON
header('Content-Type: application/json');

// 获取 POST 请求的原始内容(即 JSON 数据)
$rawData = file_get_contents('php://input');

// 解码 JSON 数据
$eventData = json_decode($rawData, true);

// 获取当前时间
$timestamp = date('Y-m-d H:i:s');

// Webhook URL
$webhookUrl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=a2235246-xxxx-xxxx-ba92-06532435ff1e';

// 检查 JSON 是否有效
if ($eventData === null) {
    http_response_code(400); // 如果 JSON 数据解析失败,返回 400 错误
    echo json_encode(['error' => 'Invalid JSON']);
    exit;
}

// 将 Webhook 数据写入日志文件
$logMessage = $timestamp . " - Received Webhook:\n" . print_r($eventData, true) . "\n\n";

// 将日志写入 webhook.log 文件
//file_put_contents('webhook.log', $logMessage, FILE_APPEND);

// 根据事件类型进行处理
if (isset($eventData['activityType'])) {
    $creator = isset($eventData['memo']['creator']) ? $eventData['memo']['creator'] : '未知';  // 如果没有 creator,使用默认值
    // 创建映射关系数组
    $creatorMapping = [
        'users/2' => '张东炎',
        'users/3' => '寇瑜',
        'users/6' => '洪燕群',
        'users/7' => '陆振华',
        'users/8' => '李旭玉',
        'users/9' => '匡慧玲',
        'users/10' => '赵锡彬',        // 添加更多的映射关系
        // 如果需要更多的用户映射,可以继续添加
    ];

    // 判断 creator 的值,并进行相应的映射
    if (isset($creatorMapping[$creator])) {
        $creator = $creatorMapping[$creator];  // 如果有匹配,替换为映射的名字
    } else {
        $creator = '未知';  // 如果没有匹配的值,使用默认值
    }
   
    $content = isset($eventData['memo']['content']) ? $eventData['memo']['content'] : '未知';  // 如果没有 creator,使用默认值
    $uid = isset($eventData['memo']['uid']) ? $eventData['memo']['uid'] : '未知';  // 如果没有 uid,使用默认值
    switch ($eventData['activityType']) {
        case 'memos.memo.updated':
            // 你可以在这里将文章信息保存到数据库,或者做其他处理
            $webhookData = [
                'msgtype' => 'text',
                'text' => [
                    'content' => "有订单被 [修改] !请及时查看并处理!\n- 业务员:$creator \n- 关键词:$content\n\n查看详细信息:\nhttps://ljs.fun:5231/m/$uid"  // 这里加上换行符
                ]
            ];
           
            // 初始化 cURL
            $ch = curl_init($webhookUrl);

            // 设置 cURL 选项
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($webhookData));

            // 执行 cURL 请求并获取响应
            $response = curl_exec($ch);

            // 检查是否有错误
            if (curl_errno($ch)) {
                $logMessage = "cURL Error: " . curl_error($ch);
                file_put_contents('webhook.log', $logMessage, FILE_APPEND);
            }
           
            break;

        case 'memos.memo.created':
            // 处理文章创建事件
            // 你可以按需要处理文章创建事件
            // Webhook 数据
            $webhookData = [
                'msgtype' => 'text',
                'text' => [
                    'content' => "有 [新订单] ,请及时查看并处理!\n- 业务员:$creator \n- 关键词:$content\n\n查看详细信息:\nhttps://ljs.fun:5231/m/$uid"  // 这里加上换行符
                ]
            ];

            // 初始化 cURL
            $ch = curl_init($webhookUrl);

            // 设置 cURL 选项
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($webhookData));

            // 执行 cURL 请求并获取响应
            $response = curl_exec($ch);

            // 检查是否有错误
            if (curl_errno($ch)) {
                $logMessage = "cURL Error: " . curl_error($ch);
                file_put_contents('webhook.log', $logMessage, FILE_APPEND);
            }
           
            break;

        case 'memos.memo.deleted':
            // 处理文章删除事件
            // 你可以按需要处理文章删除事件
            // Webhook 数据
            $webhookData = [
                'msgtype' => 'text',
                'text' => [
                    'content' => "有订单被 [删除] !请及时查看并处理!\n- 业务员:$creator \n- 关键词:$content\n\n查看详细信息:\nhttps://ljs.fun:5231/m/$uid"  // 这里加上换行符
                ]
            ];

            // 初始化 cURL
            $ch = curl_init($webhookUrl);

            // 设置 cURL 选项
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($webhookData));

            // 执行 cURL 请求并获取响应
            $response = curl_exec($ch);

            // 检查是否有错误
            if (curl_errno($ch)) {
                $logMessage = "cURL Error: " . curl_error($ch);
                file_put_contents('webhook.log', $logMessage, FILE_APPEND);
            }
           
            break;

        default:
            // 未处理的事件类型
            echo json_encode(['status' => 'error', 'message' => 'Unknown event type']);
            break;
    }
} else {
    // 如果没有收到事件类型
    http_response_code(400);
    echo json_encode(['error' => 'No event type found']);
}

?>


文章对你有帮助吗?
  • 一般[0]
  • 很赞[0]
  • 没用[0]
  • 垃圾[0]
  • 无语[0]
扫一扫,手机浏览手机访问本站