如何实现微信公众号自动回复消息(回复文本,图片或者消息)

首先,创建四张数据表   rh_mp_reply_text(用来存放回复的文本)
                       rh_mp_reply_image(用来存放回复的图片)
                       rh_mp_reply_news(用来存放回复的消息)
                       rh_mp_rule(用来存放关键字,回复的类型等)


应先写三种方法(replytext,replyimage,replynews),将一些图文素材上传到数据库




当然再次在前还需要写 upload方法


public function upload(){
          $upload = new \Think\Upload();// 实例化上传类
          $upload->maxSize = 3145728 ;// 设置附件上传大小
          $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
          $upload->rootPath = './Uploads/'; // 设置附件上传根目录
          $upload->savePath = ''; // 设置附件上传(子)目录
        // 上传文件
        $info = $upload->uploadOne($_FILES['file']);
      if(!$info) {// 上传错误提示错误信息
       // $this->error($upload->getError());
     $this->ajaxReturn(array('code'=>1,'msg'=>$upload->getError()));
       }else{// 上传成功
       // $this->success('上传成功!');
        $file = '/Uploads/'.$info['savepath'].$info['savename'];
        $this->ajaxReturn(array('code'=>0,'msg'=>'上传成功','url'=>$file));
     }


下面是回复文本,图片以及消息的方法,一起来看一下吧!


//回复文本信息
 public function replytext(){


  if(IS_GET){
             $this->display();
            }else{
             $content=I('post.content');
             $keyword=I('post.keyword');
             $model=M('mp_reply_text');
             $data['content']=$content;
  $reply_id=$model->add($data);
          if(isset($reply_id)){


  $mp=getCurrentMp();


  $data['mp_id']=$mp['id'];


  $data['type']='text';


  $data['keyword']=$keyword;


  $data['reply_id']=$reply_id;


  M('mp_rule')->add($data);
                $this->ajaxReturn(array('msg'=>'添加成功!'));
            }else{
               $this->ajaxReturn(array('msg'=>'添加失败!'));
             }


  }


}
//选择本地图片,需上传到数据库
public function replyimage(){
 if(IS_GET){


  $this->display();
          }else{
   $url = I('post.url');//图片在本服务器上传路径
   $file = realpath('.'.$url); //相对路径->绝对路径
   $accessToken = getAccess_token();
            include APP_PATH .'LaneWeChat/lanewechat.php';
            //永久素材
             $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
             $data['media'] = Curl::addFile($file);
             $ret = Curl::callWebServer($url,$data,'post',true,false);
  if(isset($ret['media_id'])){
               $mp=getCurrentMp();
                $data['url']=$url;
                $data['media_id']=$ret['media_id'];
                $model=M('mp_reply_image');
                $reply_id=$model->add($data);
                $keyword=I('post.keyword');
              if(isset($reply_id)){
              $data['mp_id']=$mp['id'];
                $data['type']='image';
                $data['keyword']=$keyword;
                $data['reply_id']=$reply_id;
                M('mp_rule')->add($data);
                 $this->ajaxReturn(array('msg'=>'添加成功!'));
              }
               else{
                $this->ajaxReturn(array('msg'=>'添加失败!'));
    }
    }else{
     
      $this->ajaxReturn(array('msg'=>'添加失败!'));
    }
         $this->ajaxReturn($ret);
   }

}

 public function replynews(){
       if(IS_GET){
          $this->display();
          }else{
           $picurl = I('post.url');//图片在本服务器上传路径
           $file = realpath('.'.$picurl); //相对路径->绝对路径
           // echo $file;
           // exit;
            $accessToken = getAccess_token();
            include APP_PATH .'LaneWeChat/lanewechat.php';
            //永久素材
             $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";


             $data['media'] = Curl::addFile($file);
             $ret = Curl::callWebServer($url,$data,'post',true,false);


              if(isset($ret['media_id'])){
                $mp=getCurrentMp();


                $data['title']=I('post.title');
                $data['description']=I('post.content');
                $data['picurl']=$url;
                $data['url']=I('post.content_source_url');
                $data['media_id']=$ret['media_id'];
                $model=M('mp_reply_news');


                $reply_id=$model->add($data);
                $keyword=I('post.keyword');


              if(isset($reply_id)){
                $data['mp_id']=$mp['id'];
                $data['type']='news';
                $data['keyword']=$keyword;
                $data['reply_id']=$reply_id;
                M('mp_rule')->add($data);
                 $this->ajaxReturn(array('msg'=>'添加成功!'));
              }
               else{
                $this->ajaxReturn(array('msg'=>'添加失败!'));
         }
       }else{
          
           $this->ajaxReturn(array('msg'=>'添加失败!'));
        }
            $this->ajaxReturn($ret);
      }
          }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值