接收到用户消息后可以根据消息内容自动回复消息,graceWeChat 提供了丰富的消息回复函数来实现此功能。
// 初始化消息回复对象 // 类 \phpGrace\tools\graceWeChat\replyMessage // 构造函数参数 : graceWeChat 操作对象 $replyMessageObj = new \phpGrace\tools\graceWeChat\replyMessage( $this->graceWeChat);
使用微信公众号官方提供的调试接口可以方便地进行接口调试 :
官网 : https://mp.weixin.qq.com/debug/
/** * 功能 : 回复文本消息 * @param string $msg // 需要回复的文本 */
示例
<?php class indexController extends grace{ public $graceWeChat; public function __construct(){ parent::__construct(); $this->graceWeChat = new \phpGrace\tools\graceWeChat(); } public function index(){ // 接收消息 $this->graceWeChat->deBug = true; $this->graceWeChat->getMsg(); // 初始化消息回复对象 $replyMessageObj = new \phpGrace\tools\graceWeChat\replyMessage( $this->graceWeChat); // 回复文本消息 $replyMessageObj->text('hi ...'); } }
/** 功能 : 回复图文消息 * $msg格式 * $msg = array( * array('项目标题', '描述', '图片地址', '点击项目打开的Url'), * array('项目标题', '描述', '图片地址', '点击项目打开的Url')...... * ); */
注意事项
当用户发送文本、图片、语音、视频、图文、地理位置这六种消息时,开发者只能回复1条图文消息;其余场景最多可回复8条图文消息。
示例
<?php class indexController extends grace{ public $graceWeChat; public function __construct(){ parent::__construct(); $this->graceWeChat = new \phpGrace\tools\graceWeChat(); } public function index(){ // 接收消息 $this->graceWeChat->deBug = true; $this->graceWeChat->getMsg(); if($this->graceWeChat->msgContent == '图文'){ // 初始化消息回复对象 $replyMessageObj = new \phpGrace\tools\graceWeChat\replyMessage( $this->graceWeChat); // 回复图文消息 $msg = array( array( '标题信息', '描述信息', 'https://img2.baidu.com/it/u=1263895256,4135038943&fm=26&fmt=auto&gp=0.jpg', 'http://www.baidu.com' ), array( '标题信息', '描述信息', 'https://img2.baidu.com/it/u=3191132181,1261503774&fm=26&fmt=auto&gp=0.jpg', 'http://www.phpgarce.com' ) ); $replyMessageObj->imgAndTextList($msg); } } }
/** * 回复图片消息 * @param string $media_id // 图片素材的 media_id 通过公众号后台或者公众号管理系统获取 */
示例
<?php class indexController extends grace{ public $graceWeChat; public function __construct(){ parent::__construct(); $this->graceWeChat = new \phpGrace\tools\graceWeChat(); } public function index(){ // 接收消息 $this->graceWeChat->deBug = true; $this->graceWeChat->getMsg(); if($this->graceWeChat->msgContent == 't'){ // 初始化消息回复对象 $replyMessageObj = new \phpGrace\tools\graceWeChat\replyMessage( $this->graceWeChat); // 回复图文消息 $replyMessageObj->image('7G6PblwGzzSQ-pszqg0lY6dMoDmTYHTTeMHdmmkMQFc'); } } }
/** * 功能 : 回复语音消息 * @param string $media_id // 语音素材的 media_id */
/** * 功能 : 回复视频消息 * @param object $msg // 视频消息对象 */ // 视频消息对象格式 : $msg = new stdClass(); $msg->media_id = 'media_id'; $msg->Title = '视频标题'; $msg->Description = '视频描述';
/** * 功能 : 回复音乐消息 * @param object $msg // 视频消息对象 */ // 音乐消息对象格式 : $msg = new stdClass(); $msg->Title = '音乐标题'; $msg->Description = '音乐描述'; $msg->MusicURL = '音乐链接'; $msg->HQMusicUrl = '高质量音乐链接,WIFI环境优先使用该链接播放音乐'; $msg->ThumbMediaId = '缩略图的媒体id,通过素材管理中的接口上传多媒体文件,得到的id';