基于CQHTTP的简易QQ机器人

入口

<?php
require 'fun.php';

error_reporting(0);//禁用错误报告,也就是不显示错误
header("content-type:application/json");
$content = file_get_contents("php://input");
$Data = json_decode($content, true);

//私聊
if ($Data['message_type'] == "private") {
    if ($Data['message']=='帮助'){
        exit('{"reply": "[CQ:image,file=file:///www/wwwroot/api.reaper0s.com/cq/help.png,type=show,id=40004]"}');
        // exit('{"reply": "愚者的机器人\n----------帮助----------\n\n--发送 美女图片--返回图片\n----美女/风景/动漫 图片\n\n--发送 抖音视频--返回视频\n--发送 黄油视频--返回视频\n--发送 绑定.学号.手机号\n----开启通达自动疫情签到\n--发送 解绑.学号.手机号\n----关闭通达自动疫情签到\n------注意用分隔符.隔开   \n--发送其他字符\n----机器人AI聊天\n\n黄油暂不开放"}');https://s1.ax1x.com/2022/04/29/LvnTte.jpg
    }
    //返回图片
    if (strpos($Data['message'], '图片') !== false) {
        if (strpos($Data['message'], '美女') !== false) {
            exit('{"reply": "' . image('meizi') . '"}');
        }
        if (strpos($Data['message'], '动漫') !== false) {
            exit('{"reply": "' . image('dongman') . '"}');
        }
        if (strpos($Data['message'], '风景') !== false) {
            exit('{"reply": "' . image('fengjing') . '"}');
        }
    }
    //返回视频
    if (strpos($Data['message'], '视频') !== false) {
        if (strpos($Data['message'], '抖音') !== false) {
            exit('{"reply": "' . video('douyin') . '"}');
        }
        if ($Data['user_id']=='2656281252') {
            if (strpos($Data['message'], '黄油') !== false) {
                exit('{"reply": "' . video('yello') . '"}');
            }
        }
    }
    // 疫情签到
    if (strpos($Data['message'], '绑定') !== false) {
        $userid=explode(".",$Data['message'])[1];
        $phoneid=explode(".",$Data['message'])[2];
        $QQid=$Data['user_id'];
        exit('{"reply": "'.sign('true',$userid,$phoneid,$QQid).'"}');
    }
    if (strpos($Data['message'], '解绑') !== false) {
        $userid=explode(".",$Data['message'])[1];
        $phoneid=explode(".",$Data['message'])[2];
        $QQid=$Data['user_id'];
        exit('{"reply": "'.sign('false',$userid,$phoneid,$QQid).'"}');
    }
    //歌曲
    if (strpos($Data['message'], '歌曲') !== false) {
        if (strpos($Data['message'], '随机') !== false) {
            exit('{"reply": "[CQ:record,file=https://api.uomg.com/api/rand.music,cache=0,c=8]"}');
        }
        $musictype=explode("歌曲",$Data['message'])[0];
            exit('{"reply": "'.music($musictype).'"}');
    }
    //聊天
    exit('{"reply": "'.chat($Data['message']).'"}');
}




//群聊
if ($Data['message_type'] == "group") {
    if (strpos($Data['message'], 'CQ:at') !== false) {
        $message=str_replace('[CQ:at,qq=3628599071] ', '',$Data['message']);
        //返回帮助文档
        if (strpos($message, '帮助') !== false) {
            exit('{"reply": "[CQ:image,file=file:///www/wwwroot/api.reaper0s.com/cq/help.png,type=show,id=40004]","at_sender":true}');
        }
        //返回图片
        if (strpos($message, '图片') !== false) {
            if (strpos($message, '美女') !== false) {
                exit('{"reply": "' . image('meizi') . '","at_sender":true}');
            }
            if (strpos($message, '动漫') !== false) {
                exit('{"reply": "' . image('dongman') . '","at_sender":true}');
            }
            if (strpos($message, '风景') !== false) {
                exit('{"reply": "' . image('fengjing') . '","at_sender":true}');
            }
        }
        //返回视频
        if (strpos($message, '视频') !== false) {
            if (strpos($message, '抖音') !== false) {
                exit('{"reply": "' . video('douyin') . '"}');
            }
            if ($Data['user_id']=='2656281252') {
                if (strpos($message, '黄油') !== false) {
                    exit('{"reply": "' . video('yello') . '"}');
                }
            }
        }
        // 疫情签到
        if (strpos($message, '绑定') !== false) {
            $userid=explode(".",$message)[1];
            $phoneid=explode(".",$message)[2];
            $QQid=$Data['user_id'];
            exit('{"reply": "'.sign('true',$userid,$phoneid,$QQid).'\n加好友,接收打卡情况通知","at_sender":true}');
        }
        if (strpos($message, '解绑') !== false) {
            $userid=explode(".",$message)[1];
            $phoneid=explode(".",$message)[2];
            $QQid=$Data['user_id'];
            exit('{"reply": "'.sign('false',$userid,$phoneid,$QQid).'","at_sender":true}');
        }
        //歌曲
        if (strpos($message, '歌曲') !== false) {
            if (strpos($message, '随机') !== false) {
                exit('{"reply": "[CQ:record,file=https://api.uomg.com/api/rand.music,cache=0,c=8]"}');
            }
            $musictype=explode("歌曲",$message)[0];
                exit('{"reply": "'.music($musictype).'"}');
        }
        //聊天
        exit('{"reply": "'.chat($message).'","at_sender":true}');
    }
    
}

//加好友 自动同意好友请求
if ($Data['post_type'] == "request") {
    if ($Data['comment'] == "愚者") {
        exit('{"approve": "true"}');
    }
}

封装PHP GET/POST请求

<?php
function httpRequest($url, $method, $postfields = null, $headers = array(), $debug = false) {
    $method = strtoupper($method);
    $ci = curl_init();
    /* Curl settings */
    curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
    curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    switch ($method) {
        case "POST":
            curl_setopt($ci, CURLOPT_POST, true);
            if (!empty($postfields)) {
                $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
                curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
            }
            break;
        default:
            curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
            break;
    }
    $ssl = preg_match('/^https:\/\//i',$url) ? TRUE : FALSE;
    curl_setopt($ci, CURLOPT_URL, $url);
    if($ssl){
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
    }
//curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
    curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
    curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ci, CURLINFO_HEADER_OUT, true);
    /*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
    $response = curl_exec($ci);
    $requestinfo = curl_getinfo($ci);
    $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
    if ($debug) {
        echo "=====post data======\r\n";
        var_dump($postfields);
        echo "=====info===== \r\n";
        print_r($requestinfo);
        echo "=====response=====\r\n";
        print_r($response);
    }
    curl_close($ci);
    return $response;
//return array($http_code, $response,$requestinfo);
}

函数实现

数据库连接使用Medoo框架

<?php
require 'httpRequest.php';
require 'Medoo.php';
use Medoo\Medoo;


//图片
function image($type)
{    // https://raw.sevencdn.com/reaper0s/apicloud/master/img/meinv/$imagename.jpg
    if($type=='meizi'){
        return str_replace(' ', '', "[CQ:image,file=https://api.vvhan.com/api/tao,type=show,cache=0,c=8]");
        
    }
    return str_replace(' ', '', "[CQ:image,file=https://api.btstu.cn/sjbz/api.php?lx={$type}&format=images,type=show,cache=0,c=8]");
}

//视频
function video($type)
{
    // 抖音视频
    if ($type == 'douyin') {
        $min = 1;
        $max = 141;
        $videoname = mt_rand($min, $max);
        return str_replace(' ', '', "[CQ:video,file=https://tucdn.wpon.cn/api-girl/videos/$videoname.mp4,c=3]");
    }
    // 黄色视频
    if ($type == 'yello') {
        // 连接数据库
        $database = new Medoo([
            'database_type' => 'mysql',
            'database_name' => '',
            'server' => 'localhost',
            'username' => '',
            'password' => '',
            'charset' => 'utf8mb4',
            'port' => 3306,
        ]);
        $min = 1;
        $max = 320000;
        $id = mt_rand($min, $max);
        $videodb = $database->get("yellovideo", [
            "videotitle",
            "videourl"
        ], [
            "id" => $id
        ]);
        $videoinfo = $videodb['videotitle'] . '\n' . $videodb['videourl'];
        return str_replace(' ', '', "$videoinfo");
    }
}

//签到 绑定 解绑
function sign($type, $userid, $phoneid,$QQid)
{
    // 连接数据库
    $database = new Medoo([
        'database_type' => 'mysql',
        'database_name' => '',
        'server' => 'localhost',
        'username' => '',
        'password' => '',
        'charset' => 'utf8mb4',
        'port' => 3306,
    ]);
    if ($type == 'true') {
        $database->insert("nytdsign", [
            "userid" => $userid,
            "phoneid" => $phoneid,
            "QQ"=> $QQid]);
        $database->update("nytdsign", [
            "state" => 1,
            "phoneid" => $phoneid,
            "QQ"=> $QQid
        ], [
            "userid" => $userid,
        ]);
        return str_replace(' ', '', "成功绑定,通达自动疫情签到");
    }
    if ($type == 'false') {
        $database->update("nytdsign", [
            "state" => 0,
            "QQ"=>''

        ], [
            "userid" => $userid,
            "phoneid" => $phoneid
        ]);
        return str_replace(' ', '', "成功解绑,通达自动疫情签到");
    }
}

//歌曲
function music($type){
    return str_replace(' ', '', "[CQ:record,file=https://api.uomg.com/api/rand.music?sort=$type,cache=0,c=8]");
}

//聊天
function chat($msg)
{
    return str_replace(' ', '', json_decode(httpRequest("http://api.qingyunke.com/api.php?key=free&appid=0&msg=$msg", 'get'), true)["content"]);
}
最后修改:2022 年 05 月 01 日
如果觉得我的文章对你有用,请随意赞赏