ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC=1,4] ||***processor.php**应用于当此模块设置过“[[dev:v0.6:module:module|回复规则列表]]”,用户发送关键字后,触发到模块中向用户回复信息的功能。 ||**processor.php** 具体定义如下: * we7_demo为模块标识,类名的定义遵循“模块标识ModuleProcessor”规则 * 此类必须继承 WeModuleProcessor 类 ``` <?php /** * 官方示例模块处理程序 * * @author 微擎团队 * @url http://bbs.we7.cc/ */ defined('IN_IA') or exit('Access Denied'); class We7_demoModuleProcessor extends WeModuleProcessor { public function respond() { //$this->message变量包含了用户信息和关键字信息 $content = $this->message['content']; //这里定义此模块进行消息处理时的具体过程, 请查看微擎文档来编写你的代码 //回复用户一句话 return $this->respText('您触发了we7_demo模块'); } } ``` ####一些类属性的介绍#### **1、$this->message** 粉丝发送的消息结构 ``` $this->message = array( 'from' => 'fromUser', //来源openid 'to' => 'toUser', //公众号标识 'time' => '1448694116', 'type' => 'text', //消息类型 'event' => '', 'tousername' => 'toUser', //同from 'fromusername' => 'fromUser', //同to 'createtime' => '1448694116', 'msgtype' => 'text', 'content' => '官方示例', //关键字 'msgid' => '1234567890123456', 'redirection' => '', //是否有消息重定向,例如扫描二维码后触发某一个关键字,这样的消息就属于重定向消息 'source' => '', //重定向消息原消息类型 ); ``` **2、$this->module['config']** 设计模块时勾选“模块全局配置项”后,用户配置的选项通过此变量获取。 ####生成访问site.php方法的链接 processor.php文件中,回复用户图文信息时,可以设置跳转链接到site.php文件中的doMobileIndex()函数,可以使用$this->createMobileUrl('index');生成访问URL 访问doWebIndex()函数时,可以使用 $this->createWebUrl('index');来生成 ####构造回复消息的相关函数 * $this->respText(); 回复一段文本文字 ``` <?php defined('IN_IA') or exit('Access Denied'); class We7_demoModuleProcessor extends WeModuleProcessor { public function respond() { //回复用户一句话 return $this->respText('您触发了we7_demo模块'); } } ``` * $this->respNews(array $news); 回复一个图文消息 ``` <?php defined('IN_IA') or exit('Access Denied'); class We7_demoModuleProcessor extends WeModuleProcessor { public function respond() { //多图文即传入一个二维的下面的数组即可 return $this->respNews(array( 'Title' => '单图文的标题', 'Description' => '单图文的描述', 'PicUrl' => tomedia('封面图片地址'), 'Url' => $this->createMobileUrl('introduce', array('id' => $rid)), //创建一个带openid信息的访问模块introduce方法的地址,这里也可以直接写http://we7.cc )); } } ``` * $this->respMusic(array $music); 回复一个音乐消息 ``` <?php defined('IN_IA') or exit('Access Denied'); class We7_demoModuleProcessor extends WeModuleProcessor { public function respond() { return $this->respMusic(array( 'Title' => '音乐回复标题', 'Description' => '音乐回复描述', 'MusicUrl' => tomedia('音乐链接'), 'HQMusicUrl' => '高清音乐链接,可不填', )); } } ``` * $this->respVideo(array $video); 回复一个视频消息,视频需要上传到微信的素材中。见:[上传素材](http://www.kancloud.cn/donknap/we7/245194) ``` <?php defined('IN_IA') or exit('Access Denied'); class We7_demoModuleProcessor extends WeModuleProcessor { public function respond() { return $this->respVideo(array( 'MediaId' => '素材的Id', 'Title' => '视频标题', 'Description' => '视频的描述' )); } } ``` * $this->respVoice($mid); 回复一个语音消息,语音需要上传到微信的素材中。见:[上传素材](http://www.kancloud.cn/donknap/we7/245194) ``` <?php defined('IN_IA') or exit('Access Denied'); class We7_demoModuleProcessor extends WeModuleProcessor { public function respond() { return $this->respVoice('素材id'); } } ``` * $this->respImage($mid) 回复一个图片消息,图片需要上传到微信的素材中。见:[上传素材](http://www.kancloud.cn/donknap/we7/245194) ``` <?php defined('IN_IA') or exit('Access Denied'); class We7_demoModuleProcessor extends WeModuleProcessor { public function respond() { return $this->respImage('素材id'); } } ```