
看源码,在微信回复的类里定义了函数回复的方法

<?php
/**
* 1.第三方回复加密消息给公众平台;
* 2.第三方收到公众平台发送的消息,验证消息的安全性,并对消息进行解密。
*/
class WEIXIN_Reply{
/*
省略前面代码
...
...
*/
}elseif($type == 'video'){ // 视频回复
$this->response = 'custom-video';
$raw_items = explode("n", $reply);
$MediaId = $raw_items[0];
$title = $raw_items[1] ?? '';
$description= $raw_items[2] ?? '';
$this->video_reply($MediaId, $title, $description);
}elseif($type == 'function'){ // 函数回复
if(is_callable($reply)){
call_user_func($reply, $keyword, $this);
}else{
echo ' ';
}
return true;
}elseif($type == 'wxcard'){
$this->response = 'wxcard';
仔细看了代码和试错,终于搞明白其逻辑
/**
* @param $reply 自定义的函数名
* @param $keyword 用户在微信回复的关键词
* @param $this 实例化的对象自身
*/
call_user_func($reply, $keyword, $this);
那么这就好办了,比如我想把默认的回复换成百度站内搜索
后续的一列处理需要调用的成员方法
@func get_keyword() 访问器方法,获取关键词
@func news_reply($item) 加密输出完整的xml数据
具体看其在类里的定义
<?php
//统一方法名
function zke_weixin_reply($keyword, $wxObj){
return zke_default_wx_reply($keyword, $wxObj);
}
//默认回复百度站内搜索
function zke_default_wx_reply($keyword, $wxObj){
$keyword = $wxObj -> get_keyword();
$message = '如果没找到内容,你就更换一下关键字,可能就有结果了哦 :-)';
$url = 'https://zhannei.baidu.com/cse/site?q='.$keyword .'&ie=utf-8&cc=zkcoi.com';
$picUrl ='https://www.zkcoi.com/wp-content/uploads/2023/06/zk_wx_reply.png';
$item = '<item>
<Title><![CDATA[找到些关于【'.$keyword.'】的内容]]></Title>
<Description><![CDATA['.$message.']]></Description>
<PicUrl><![CDATA['.$picUrl.']]></PicUrl>
<Url><![CDATA['.$url .']]></Url>
</item>';
$wxObj->news_reply($item);
return $item;
}
再比如我想根据关键词从woocomerce输出产品信息
<?php
function zke_weixin_reply($keyword, $wxObj){
$keyword = $wxObj -> get_keyword();
$data = zke_get_shop_for_wx($keyword);
return $wxObj->news_reply(zke_shop_json_to_wx($data, $keyword)) ;
}
//从商城接口获取查询的json数据
function zke_get_shop_for_wx($keyword){
// 先从缓存中尝试获取相关数据
$cache_key = 'wx_shop_' . md5($keyword);
$cache_data = wp_cache_get($cache_key);
if (false !== $cache_data) {
return $cache_data;
}
$siteUrl = 'zkcoi.com';//修改成自己的url
$url = 'https://'.$siteUrl.'/wp-json/wc/v3/products'; // 替换成你的站点 WooCommerce REST API 地址,这是woocomerce内置的REST API
$consumer_key = 'xxxxxxxxx'; // 替换成你的站点 WooCommerce REST API 访问密钥,在后台生成
$consumer_secret = 'xxxxxxxxx'; // 替换成你的站点 WooCommerce REST API 访问密钥,在后台生成
$params = array(
'search' => $keyword, // 搜索关键字
'per_page' => 1, // 每页显示数量
'page' => 1 // 第几页
);
$headers = array(
'Authorization' => 'Basic ' . base64_encode( $consumer_key . ':' . $consumer_secret ),
'User-Agent' => $siteUrl
);
$response = wp_remote_get( add_query_arg( $params, $url ), array(
'headers' => $headers
));
if ( is_wp_error( $response ) ) {
// 输出 API 请求的错误信息
echo 'API 请求返回错误:' . $response->get_error_message();
return '请求失败,请稍后重试。';
} else {
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body );
// 将新获取到的数据存储到缓存中,有效期为 5 分钟
wp_cache_set($cache_key, $data, '', 300);
return $data;
}
}
//遍历json数据输出xml
function zke_shop_json_to_wx($data,$keyword){
$items ='';
foreach ($data as $product) {
// $thumb = weixin_robot_get_material($news_item['thumb_media_id'], 'thumb');
// $thumb = is_wp_error($thumb)?'':$thumb;
$title = $product->name;
$description = $product->short_description;
$pic_url = $product->images[0]->src;
$url = $product->get_permalink();
$description = mb_substr( wp_strip_all_tags( str_replace( array( "r", "n", " " ), '', $description ), false ), 0, 200, 'UTF-8' );
if(!$description) $description = $title;
$items .= '<item>
<Title><![CDATA['.$title.']]></Title>
<Description><![CDATA['.$description.']]></Description>
<PicUrl><![CDATA['.$pic_url.']]></PicUrl>
<Url><![CDATA['.$url.']]></Url>
</item>';
}
return $items;
}
也可以根据条件返回这两种回复的其中一种。
<?php
//或者都写到一起
function zke_weixin_reply($keyword, $wxObj){
if(!zke_get_shop_for_wx($keyword) ){
return zke_default_wx_reply($keyword, $wxObj);
}else{
$data = zke_get_shop_for_wx($keyword);
return $wxObj->news_reply(zke_shop_json_to_wx($data, $keyword)) ;
}
}
最后将在微信机器人的后台将函数名填进去就行了,有兴趣的可以尝试下,或者关注我的公众号试试看。
公众号已经注销了……

本文作者:ZKCOI
文章名称:WPJAM微信机器人如何使用自定义函数回复?
文章链接:https://www.zkcoi.com/365up/program/2686.html
本站资源仅供个人学习和交流,如若转载,请注明出处,详见《免责声明》。
微信扫一扫
支付宝扫一扫
