微信公众平台开发——自定义菜单+自动回复


<?php

//加载微信JDK
include "include/wechat.class.php";
//加载配置文件
include "include/bootstrap.php";

$options = array(
        //填写你设定的key
        'token'=>'',
        //填写高级调用功能的app id
        'appid'=>'',
        //填写高级调用功能的密钥
        'appsecret'=>'',
    );

//实例化
$weObj = new Wechat($options);

$weObj->valid();
//获取接收消息的类型
$type = $weObj->getRev()->getRevType();
//获取接收消息内容正文
$keyword =$weObj->getRev()->getRevContent();
//获取接收事件推送
$form = $weObj->getRev()->getRevEvent();
$form_Event =$form['event'];
$form_Key = $form['key'];

switch($type) {
    //text
    case Wechat::MSGTYPE_TEXT:
        switch ($keyword){
            case 1:
                $newsdata = array(
                    array(
                        'Title'=>'xx教育',
                        'Description'=>'hold on ...',
                        'PicUrl'=>SYSTEMURL .'/image/logo.png',
                        'Url'=>'http://www.xxx.com/online/'
                    ),
                   );
                 $weObj->news($newsdata)->reply();
                break;
            case 2:
                $newsdata = array(
                    array(
                        'Title'=>'xx图书',
                        'Description'=>'hold on ...',
                        'PicUrl'=>SYSTEMURL .'/image/logo.png',
                        'Url'=>'http://book.xxx.com/'
                    ),
                   );
                 $weObj->news($newsdata)->reply();
                break;
            case 3:
                $newsdata = array(
                    array(
                        'Title'=>'xx论坛',
                        'Description'=>'hold on ...',
                        'PicUrl'=>SYSTEMURL .'/image/logo.png',
                        'Url'=>'http://bbs.xxx.com/'
                    ),
                   );
                 $weObj->news($newsdata)->reply();
                break;
            case 4:
                $newsdata = array(
                    0=>array(
                        'Title'=>'xx资讯',
                        'Description'=>'hold on ...',
                        'PicUrl'=>SYSTEMURL .'/image/logo.png',
                        'Url'=>'http://news.xxx.com/'
                    ),
                   );
                 $weObj->news($newsdata)->reply();
                break;
            default:
                $weObj->text("可以输入help或者直接给我们留言")->reply();
        }
        break;
    //event
    case Wechat::MSGTYPE_EVENT:
         //判断事件类型
            if ('subscribe' == $form_Event)
            {
                $weObj->text("
你可以输入对应数字查找
                1、xx教育
                2、xx图书
                3、xx论坛
                4、xx资讯
                ")->reply();
            }

            //判断按钮事件
            if ('CLICK' == $form_Event)
            {

                if (OPEN_COURSE == $form_Key)
                {
                    $newsdata = array(
                        0=>array(
                            'Title'=>'公开课',
                            'Description'=>'hold on ...',
                            'PicUrl'=>SYSTEMURL .'/image/logo.png',
                            'Url'=>'http://www.xxx.com'
                        ),
                       );
                    $t= $weObj->news($newsdata)->reply();
                }

                if (LIKE_ME == $form_Key)
                {
                    $weObj->text("你的赞,会让我们更加努力")->reply();
                }
            }
        break;
    //image
    case Wechat::MSGTYPE_IMAGE:
        $weObj->text("你的图和你一样美")->reply();
        break;
    //voice
    case Wechat::MSGTYPE_VOICE:
        $weObj->text("你对我说的情话")->reply();
        break;
    //video
    case Wechat::MSGTYPE_VIDEO:
        $weObj->text("你的视频很美")->reply();
        break;
    //location
    case Wechat::MSGTYPE_LOCATION:
        $weObj->text("你的位置我们会关注")->reply();
        break;
    //link
    case Wechat::MSGTYPE_LINK:
        $weObj->text("你发送了什么链接")->reply();
        break;

    default:
        $weObj->text("你说的我都不懂...")->reply();

}
//获取菜单操作:
$menu = $weObj->getMenu();
//设置菜单
$newmenu =  array(
    "button"=>
        array(
            array('type'=>'click','name'=>'公开课','key'=>'OPEN_COURSE'),
            array('type'=>'view','name'=>'yy','url'=>'http://www.yy.com'),
            array(
                'name'=>'关于我们',
                'sub_button'=>array(
                    array('type'=>'view','name'=>'企业文化','url'=>'http://www.xxx.com/'),
                    array('type'=>'click','name'=>'求赞','key'=>'LIKE_ME'),
                )
            )
            )
        );
$result = $weObj->createMenu($newmenu);

你可能感兴趣的:(自定义菜单、自动回复)