微擎系统微信公众号关键字触发回复多条消息实现

使用客服消息实现发送多条图文消息

第一步

首先找到微擎关键字触发发送消息的PHP文件在
framework->builtin->core->processor.php文件里面
framework->builtin->core->processor.php--MingGyGy

第二步、修改respond方法

public function respond() {
     $result = $this->msg_respond();
     return $this->respText($result);
}

第三步、写入我们自己实现发送图文的方法

msg_respond()方法

private function msg_respond() {
        $rids = !is_array($this->rule) ? explode(',', $this->rule) : $this->rule;
        //数据库中获取发送文字的信息
        $reply = table('basic_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
        //图片
        $img_reply = table('images_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
        //图文
        //$news_reply = table('news_reply')->where(array('rid IN' => $rids,'parent_id ==' => -1))->orderby('id')->getAll();
        $news_reply = table('news_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
        //音乐
        $music_reply = table('music_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
        //语音
        $voice_reply = table('voice_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
        //视频
        $video_reply = table('video_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
//父级找子级
        /*foreach ($news_reply as &$value){
            //if ($value['parent'] != -1) {
                $value['parent_data'] = table('news_reply')->where(['parent_id' => $value['id']])->orderby('id')->getAll();
            //}
        }*/

        //判断是否为空,如果都为空返回false
        if (empty($reply)&&empty($img_reply)&&empty($news_reply)&&empty($music_reply)&&empty($voice_reply)&&empty($video_reply)) {
            return false;
        }
        $access_token=$this->getToken();
        $postStr=file_get_contents('php://input');
        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        if (count($reply)+count($img_reply)+count($news_reply)+count($music_reply)+count($voice_reply)+count($video_reply)==1){
            if($reply!=null){
                $reply[0]['content'] = htmlspecialchars_decode($reply[0]['content']);
                $reply[0]['content'] = str_replace(array('<br>', '&nbsp;'), array("\n", ' '), $reply[0]['content']);
                $reply[0]['content'] = strip_tags($reply[0]['content'], '<a>');
                return $reply[0]['content'];
            }elseif ($img_reply!=null){
                for ($y=0;$y<count($img_reply);$y++){
                    $this->imageReply($postObj->FromUserName,$access_token,$img_reply[$y]['mediaid']);
                }
            }elseif ($news_reply!=null){
                //$this->judgeType('news');
                for ($j=0;$j<count($news_reply);$j++){

                    //$this->newsReply($postObj->FromUserName,$access_token,$news_reply[$j]['url'],$news_reply[$j]['thumb'],$news_reply[$j]['title'],$news_reply[$j]['description'],$news_reply[$j]['parent_data']);
                    $this->newsReply($postObj->FromUserName,$access_token,$news_reply[$j]['url'],$news_reply[$j]['thumb'],$news_reply[$j]['title'],$news_reply[$j]['description'],$news_reply[$j]['media_id']);
                }
            }elseif ($music_reply!=null){
                $result = $this->music_respond();
                return $this->respMusic(array(
                    'Title' => $result['title'],
                    'Description' => $result['description'],
                    'MusicUrl' => $result['url'],
                    'HQMusicUrl' => $result['hqurl'],
                ));
            }elseif ($voice_reply!=null){
                for ($s=0;$s<count($voice_reply);$s++){
                    $this->voiceReply($postObj->FromUserName,$access_token,$voice_reply[$s]['mediaid']);
                }
            }elseif ($video_reply!=null){
                for ($d=0;$d<count($video_reply);$d++){
                    $this->videoReply($postObj->FromUserName,$access_token,$video_reply[$d]['mediaid'],$video_reply[$d]['title'],$video_reply[$d]['description']);
                }
            }else{
                return "数据错误!!!";
            }
        }else{
            //循环发送图片
            for ($y=0;$y<count($img_reply);$y++){
                $this->imageReply($postObj->FromUserName,$access_token,$img_reply[$y]['mediaid']);
            }
            //循环发送图文
            for ($j=0;$j<count($news_reply);$j++){
                $this->newsReply($postObj->FromUserName,$access_token,$news_reply[$j]['url'],$news_reply[$j]['thumb'],$news_reply[$j]['title'],$news_reply[$j]['description'],$news_reply[$j]['media_id']);
            }
            //视频
            for ($d=0;$d<count($video_reply);$d++){
                $this->videoReply($postObj->FromUserName,$access_token,$video_reply[$d]['mediaid'],$video_reply[$d]['title'],$video_reply[$d]['description']);
            }
            //语音
            for ($s=0;$s<count($voice_reply);$s++){
                $this->voiceReply($postObj->FromUserName,$access_token,$voice_reply[$s]['mediaid']);
            }
            //循环发送文字
            for($i=0;$i<count($reply);$i++){
                if($i==count($reply)-1){
                    return $reply[$i]['content'];
                }else{
                    $this->replymsg($postObj->FromUserName,$access_token,trim($reply[$i]['content']));
                }
            }
        }
        return 0;
    }

第四步、获取access token信息

我们自己写一个getToken()方法,需要修改 a p p i d 和 appid和 appidsecret参数,修改为你自己公众号上的appid和secret

private    function getToken(){
        $appid = "你自己的appid";
        $secret = "你自己的secret";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
        $ch = curl_init();//初始化
        curl_setopt($ch, CURLOPT_URL, $url);//与url建立对话
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //进行配置
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //进行配置
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//进行配置
        $output = curl_exec($ch);//获取Access Token
        curl_close($ch);//关闭会话
        $jsoninfo = json_decode($output, true);
        $access_token =$jsoninfo["access_token"];
        echo $access_token;
        return $access_token;
    }

第五步、写发送的方法

//发送文字的方法
    private function replymsg($fromUsername,$access_token,$content){

        $data = '{
            "touser":"'.$fromUsername.'",
            "msgtype":"text",
            "text":
            {
                 "content":"'.$content.'"
            }
        }';

        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
        //$this->https_post($url,$data);
        $result = $this->https_post($url,$data);
        $final = json_decode($result);
        return $final;
    }
    //发送图片的方法
    private function imageReply($fromUsername,$access_token,$mediaId){
        $data='{
            "touser":"'.$fromUsername.'",
            "msgtype":"image",
            "image":
                {
                    "media_id":"'.$mediaId.'"
                }
        }';
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
        //$this->https_post($url,$data);
        $result = $this->https_post($url,$data);
        $final = json_decode($result);
        return $final;
    }
    //发送图文的方法
    private function newsReply($fromUsername,$access_token,$url,$picUrl,$title,$description,$mediaid){
        $data = '{
            "touser":"'.$fromUsername.'",
            "msgtype":"mpnews",
            "mpnews":{
                "media_id":"'.$mediaid.'"
            }
        }';
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
        //$this->https_post($url,$data);
        $result = $this->https_post($url,$data);
        $final = json_decode($result);
        return $final;
    }
    //发送音乐的方法
    private function musicReply($fromUsername,$access_token,$title,$description,$url,$hqurl){
        $data = '{
            "touser":"'.$fromUsername.'",
            "msgtype":"music",
            "music":
             {
                "title":"'.$title.'",
                "description":"'.$description.'",
                "musicurl":"'.$url.'",
                "hqmusicurl":"'.$hqurl.'"
             }
        }';
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
        //$this->https_post($url,$data);
        $result = $this->https_post($url,$data);
        $final = json_decode($result);
        return $final;
    }
    //语音
    private function voiceReply($fromUsername,$access_token,$mediaid){
        $data='
            {
    "touser":"'.$fromUsername.'",
    "msgtype":"voice",
    "voice":
    {
      "media_id":"'.$mediaid.'"
    }
}
        ';
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
        //$this->https_post($url,$data);
        $result = $this->https_post($url,$data);
        $final = json_decode($result);
        return $final;
    }
    //视频
    private function videoReply($fromUsername,$access_token,$mediaid,$title,$description){
        $data = '
            {
    "touser":"'.$fromUsername.'",
    "msgtype":"video",
    "video":
    {
      "media_id":"'.$mediaid.'",
      "title":"'.$title.'",
      "description":"'.$description.'"
    }
}
        ';
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
        //$this->https_post($url,$data);
        $result = $this->https_post($url,$data);
        $final = json_decode($result);
        return $final;
    }

第六步、https_post方法

private function https_post($url,$data)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        if (curl_errno($curl)) {
            return 'Errno'.curl_error($curl);
        }
        curl_close($curl);
        return $result;
    }

七、展示一下效果吧

在这里插入图片描述

八、觉得本文章对你有帮助的麻烦动下手指点个赞呗

----hello word开始,hello word结束!

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值