php对接飞书自定义机器人发送消息功能
warning:
这篇文章距离上次修改已过201天,其中的内容可能已经有所变动。
<?php
// 发送消息到飞书自定义机器人
function sendMessageToLark($webhookUrl, $message){
$payload = json_encode(array('msg_type' => 'text', 'content' => $message));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $webhookUrl);
curl_setopt($ch_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
if(curl_errno($ch)){
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $response;
}
// 使用方法
$webhookUrl = 'https://open.feishu.cn/open-apis/bot/v2/hook/your_bot_token';
$message = '这是一条测试消息';
$response = sendMessageToLark($webhookUrl, $message);
echo $response;
在这个代码示例中,我们定义了一个sendMessageToLark
函数,它接受飞书自定义机器人的Webhook URL和要发送的消息内容。然后使用cURL库来发送POST请求,消息内容以JSON格式传递。如果请求成功,它会返回响应内容,否则会打印错误信息。使用时需要替换$webhookUrl
中的your_bot_token
为实际的机器人令牌。
评论已关闭