微信中的自动回复

微信中的自动回复即被动回复用户消息,是指:当用户发送消息给公众号时(或某些特定的用户操作引发的事件推送时),会产生一个POST请求,开发者可以在响应包(Get)中返回特定XML结构,来对该消息进行响应(现支持回复文本、图片、图文、语音、视频、音乐)。严格来说,发送被动响应消息其实并不是一种接口,而是对微信服务器发过来消息的一次回复。如下为怎样回复文本、图片以及图文:

首先都要先获取当前正在使用的公众号

1.回复文本消息,相对比较简单:先用post方法获取前台输入的内容,添加到mp_replay_text表中,$ret即为reply_id,如果得到得到了reply_id,将获取到的关键字,类型,reply_id等添加到mp_rule表中,代码如下:

<?php
namespace Home\Controller;
use Think\Controller;
use LaneWeChat\Core\Curl;
class AutoreplyController extends Controller{
	private $mp;
	public function _initialize(){
            $mp = getCurrentMp();
		if(empty($mp)){
			$this->error('当前无使用公众号',U('mp/index'));
			exit;
		}else{
			$this->mp = $mp;
		}
	}

// 新增文本
	public function addtext(){
	    $content = I('post.content');
	    $reply = M('mp_reply_text');
	    $data['content'] = $content;
	    $ret = $reply->add($data);
	    // echo $ret;
	    // exit;
	    if(isset($ret)){
	    	$mp = $this->mp;
	    	$keyword = I('post.keyword');
                $arr['mpid'] = $mp['id'];
                $arr['keyword'] = $keyword;
                $arr['type'] = 'text';
                $arr['reply_id'] = $ret;
                $rule = M('mp_rule');
	        $rule->add($arr);
	        $this->ajaxReturn(array('msg'=>'添加成功'));
	    }else{
	    	$this->ajaxReturn(array('msg'=>$ret));
	    }
	}

2.新增图片:首先用post方法获取到前台输入的关键字(keyword),图片的相对路径(url),以及media_id,将相对路径转化为绝对路径(realpath('.' . $url)),判断media_id是否为空,若为空则先获取图片的media_id,获取到media_id后将media_id和url添加到mp_replay_image表中,并根据$img(即replay_id)将关键字,类型,等内容添加到rule表中

public function addimage(){
	$keyword = I('post.keyword');
        $url = I('post.url');
        $media_id = I('post.media_id');
        $file = realpath('.' . $url);

        if(empty($media_id)){
	        $accessToken = getAccess_token();
	        include APP_PATH . 'LaneWeChat/lanewechat.php';
	        $api = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
	        $data['media']=Curl::addFile($file);
	        $ret =Curl::callWebServer($api,$data,'post',true,false);
        }

        if($ret['media_id']){
	        $reply = M('mp_reply_image');
	        $data['url'] = $ret['url'];
	        $data['media_id'] = $ret['media_id'];
	        $img = $reply->add($data);
            
	        if(isset($img)){
	            $mp = $this->mp;
	            $arr['mpid'] = $mp['id'];
	            $arr['keyword'] = $keyword;
	            $arr['type'] = 'image';
	            $arr['reply_id'] = $img;
	            $reimg = M('mp_rule');
	            $reimg->add($arr);
	            $this->ajaxReturn(array('status'=>0,'msg'=>'上传成功'));
            }else{
            	$this->ajaxReturn(array('status'=>1,'msg'=>$img));
            }
        }else{
        	$this->ajaxReturn(array('msg'=>$ret));
        }
	}

3.新增图文:首先用post方法获取到前台中输入的封面路径$picurl,添加的链接$url,描述$description,关键字$keyword,将$picurl转化为绝对路径(realpath('.' . $picurl)),用上传永久图片的api获取到封面图片的url,获取到$ret['url']后将题目title,描述description,封面路径picurl添加到mp_replay_news表中,然后再根据图文的reply_id(即$res)

//新增图文
	public function addnews(){
		$mp = $this->mp;
		$picurl = I('post.url');
		$url = I('post.content_source_url');
		$title = I('post.title');
		$description = I('post.content');
		$keyword = I('post.keyword');
		$file = realpath('.' . $picurl);//将图片的路径转换为绝对路径
        
                $accessToken = getAccess_token();//获取当前公众号的access_token
		include APP_PATH . 'LaneWeChat/lanewechat.php';
		//上传永久图片api
		$api = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
		$data['media'] = '@' . $file;
                $ret =Curl::callWebServer($api,$data,'post',true,false);
    	        if(isset($ret['url'])){
			$arr['title'] = $title;
			$arr['description'] = $description;
                        $arr['url'] = $url;
                        $arr['picurl'] = $ret['url'];
    		        $res = M('mp_reply_news')->add($arr);

    		        if($res){
    			    $arr['mpid'] = $mp['id'];
    		            $arr['type'] = 'news';
    			    $arr['keyword'] = $keyword;
    			    $arr['reply_id'] = $res;
    			    M('mp_rule')->add($arr);
    			    $this->ajaxReturn(array('status'=>0,'msg'=>'图文上传成功'));
    		        }else{
    		            $this->ajaxReturn(array('status'=>1,'msg'=>'失败'));
    	                }    
    	        }else{
    		    $this->ajaxReturn(array('msg'=>$ret));
    	        }
	}

上传文件的方法是必不可少的,因此一定要注意写上传文件的方法:

//上传文件
	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));
		}
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值