关于微信公众号自动回复文本消息

当用户向微信发送一条消息后,微信将会被动回复数据库对应的文字信息。

首先我们需要新建两个数据库,mp_rlue,reply_text。


除了数据库之外还需要在控制器中写相应的方法replytext,代码如下:
<?php
//回复文本
	public function replytext($type=''){
		if (empty($type)) {
    		$type='image';
    	}
		$keyword = I('post.keyword');
		$content = I('post.content');
		$data['content']=$content;
		$ret = M('mp_reply_text')->add($data);
		if ($ret) {
			//获取当前使用的公众号的id
			$mp = getCurrentMp();
			$mpid = $mp['id'];
			$arr['mpid']=$mpid;
			$arr['reply_id'] = $ret;
			$arr['keyword']=$keyword;
			$arr['type']='text';
			$result = M('mp_rule')->add($arr);
			if ($result) {
				$this->ajaxReturn(array('msg'=>'添加成功'));
			}	
		}
		
	}
?>
此外还需要把LaneWeChat中的core里面的wechatrequest.lib.php中的text方法做些改动
当用户发送关键字时,或被动回复相应的文本,若没有则会回复‘出错了’使用success.
<?php
public static function text(&$request){
        $mpid = $_GET['id'];
        $content = $request['content'];
        $where['mpid'] = $mpid;
        $where['keyword'] = $content;
        $data = M('mp_rule')->where($where)->find();
        if ($data) {
            //发送关键字
            $reply_id = $data['reply_id'];
            $type = $data['type'];
            switch ($type) {
                case 'text':
                    $reply = M('mp_reply_text')->find($reply_id);
                    if ($reply) {
                        $reply_text = $reply['content'];
                    }else{
                        $reply_text = "出错了";
                    }
                    return ResponsePassive::text($request['fromusername'],$request['tousername'],$reply_text);
                    break;
                default:
                    return "success";
                    break;

            }
        }else{
             return "success";
        }
    }
?>
这样微信号回复文本消息就成功啦。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值