<div id="post-toc" class="post-content markdown-body">
<h1 id="tip0">起因</h1>
今天,得知qq群有个机器人hook,消息推送机器人,hook机器人,hookrobot。
然后才出,网上都没有一个实例。权那他就此粗糙的写了一个php类。
先写一个php的,后续有时间写java的。
<h1 id="tip1">类</h1>

<span class="hljs-meta"><?php</span>

<span class="hljs-comment">/**
 * Class HOOK_ROBOT
 * author 权那他
 * date 2020/04/09
 * update 2020/04/12
 */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HOOK_ROBOT</span>
</span>{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> $_instance;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> $httpApi;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> $message;

    <span class="hljs-comment">/**
     * <span class="hljs-doctag">@param</span> $array
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setApi</span><span class="hljs-params">($array)</span>
    </span>{
        <span class="hljs-comment">//hookrobot_api build</span>
        <span class="hljs-keyword">self</span>::$httpApi = $array[<span class="hljs-string">"api"</span>] . <span class="hljs-string">"?"</span> . http_build_query($array[<span class="hljs-string">"query"</span>]);
        <span class="hljs-keyword">self</span>::init();
    }

    <span class="hljs-comment">/**
     * 初始化
     * 看腾讯hookrobot后续会新加啥
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">init</span><span class="hljs-params">()</span>
    </span>{
        <span class="hljs-comment">//这样是为了兼容后续腾讯hookrobot会增加参数</span>
        <span class="hljs-keyword">self</span>::$message = <span class="hljs-keyword">array</span>(
            <span class="hljs-string">"content"</span> => <span class="hljs-keyword">array</span>()
        );
    }

    <span class="hljs-comment">/**
     * 设置单实例
     * <span class="hljs-doctag">@param</span> HOOK_ROBOT $hook
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">set</span><span class="hljs-params">(HOOK_ROBOT $hook)</span>
    </span>{
        <span class="hljs-keyword">self</span>::$_instance = $hook;
    }

    <span class="hljs-comment">/**
     * 获取单实例
     * <span class="hljs-doctag">@return</span> HOOK_ROBOT
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">get</span><span class="hljs-params">()</span>
    </span>{
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">empty</span>(<span class="hljs-keyword">self</span>::$_instance)) {
            <span class="hljs-keyword">die</span>(<span class="hljs-string">"Exception: Single instance is not set"</span>);
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">self</span>::$_instance;
    }

    <span class="hljs-comment">/**
     * <span class="hljs-doctag">@param</span> $msg
     * <span class="hljs-doctag">@param</span> int $type 目前是0,可能后续腾讯hookrobot会有新增加
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setMsg</span><span class="hljs-params">($msg, $type = <span class="hljs-number">0</span>)</span>
    </span>{
        <span class="hljs-keyword">self</span>::$message[<span class="hljs-string">"content"</span>][] = <span class="hljs-keyword">array</span>(
            <span class="hljs-string">"type"</span> => $type,
            <span class="hljs-string">"data"</span> => $msg
        );
    }

    <span class="hljs-comment">/**
     * 发送
     * 第一个是http代号,测试出一直是500
     * 第二个是api返回的内容,现在一直是null
     * <span class="hljs-doctag">@return</span> array
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">send</span><span class="hljs-params">()</span>
    </span>{
        $json = json_encode(<span class="hljs-keyword">self</span>::$message);
        <span class="hljs-comment">// encode 后,就初始化msg</span>
        <span class="hljs-keyword">self</span>::init();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, <span class="hljs-number">1</span>);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="hljs-number">1</span>);
        curl_setopt($ch, CURLOPT_URL, <span class="hljs-keyword">self</span>::$httpApi);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_HEADER, <span class="hljs-number">0</span>);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            <span class="hljs-keyword">array</span>(
                <span class="hljs-string">'Content-Type: application/json; charset=utf-8'</span>,
                <span class="hljs-string">'Content-Length: '</span> . strlen($json)
            )
        );
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(
            curl_getinfo($ch, CURLINFO_HTTP_CODE),
            $return_content
        );
    }
}

<span class="hljs-comment">// hookrobot  new 对象,set 单实例</span>
$hook = <span class="hljs-keyword">new</span> HOOK_ROBOT();
$hook->setApi(
    <span class="hljs-keyword">array</span>(
        <span class="hljs-comment">//hookrobot_api</span>
        <span class="hljs-string">"api"</span> => <span class="hljs-string">"https://app.qun.qq.com/cgi-bin/api/hookrobot_send"</span>,
        <span class="hljs-comment">//这样做是为了后续腾讯hookrobot更新的新参数</span>
        <span class="hljs-string">"query"</span> => <span class="hljs-keyword">array</span>(
            <span class="hljs-comment">// 这里是 key,自己改成自己群hook的key</span>
            <span class="hljs-string">"key"</span> => <span class="hljs-string">"13256478748454846856496768"</span>
        )
    )
);
HOOK_ROBOT::set($hook);

<span class="hljs-comment">//调用方法 1</span>

<span class="hljs-comment">//这里添加消息内容  可以多次调用</span>
HOOK_ROBOT::setMsg(<span class="hljs-string">"test 1"</span>);
HOOK_ROBOT::setMsg(<span class="hljs-string">"test 2"</span>);

<span class="hljs-comment">// 最后,添加消息完后,发送</span>
HOOK_ROBOT::send();

<span class="hljs-comment">//或则 打印返回的内容</span>
var_dump(HOOK_ROBOT::send());


<span class="hljs-comment">//调用方法 2</span>
<span class="hljs-comment">// 可以获取hookrobot单实例,类比上面的set</span>
$hook = HOOK_ROBOT::get();
$hook->setMsg(<span class="hljs-string">"get test 1"</span>);
$hook->setMsg(<span class="hljs-string">"get test 2"</span>);
$hook->send();

代码中的key,改成自己群hook的key

搬自https://krait.cn/major/2025.html

</div>

最后修改:2020 年 10 月 01 日 09 : 49 AM
如果觉得我的文章对你有用,请随意赞赏