先说明下啊,当前代码是在php Laravel框架下实现的,所以直接复制是无法使用的,但是您可以参考如何实现的,我在几个关键的地方做了注释讲解!喜欢学习的小伙伴可以观摩~
捐赠列表
/**
* 捐赠提示语:私聊|群聊
* @param array $request 接收的消息
* @return array|boolean
*/
public function _donate(array $request)
{
if('wxid_6mkmsto8tyvf52' != $request ['robot_wxid'])
return false;
if (is_array(($request ['msg'])))
return false;
$user_wxid = $request ['final_from_wxid'] ?? ($request ['from_wxid'] ?? null);
if(is_null($user_wxid))
return false;
$key = ['邀请码','邀请','捐赠','注册','群主','遗忘悠剑'];//触发词
$msg = trim($request ['msg']);
$req_event = $request ['event'];
if($req_event == 'EventGroupMsg'){
$event = 'SendGroupMsgAndAt';
$group_wxid = $request ['from_wxid'];
}else{
$event = 'SendTextMsg';
$group_wxid = '';
}
if(Str::contains($msg, $key)){
$msg = "您好,要论坛邀请码吗??\n";
$msg .= "?捐赠>=5元即可获得哦\n";
$msg .= "?点击 https://iderge.com/2.sl 识别二维码捐赠\n";
$msg .= " [打脸]捐赠完成后会自动回复给您邀请码~";
$msg = $this->formatEmoji($msg);
//会话保存10分钟,也就是说触发捐赠后,10分钟内进行捐赠,机器人能正常通知你,否则找不到你了就,可以看下面的处理
Cache::put($user_wxid,$request,600);
// $remote ['type'] = 1;
// $remote ['delay'] = 5;
// $remote ['param'] = $request;
// RemoteEvent::dispatch($remote);
return [
"event" => $event,
"robot_wxid" => $request ['robot_wxid'],
"to_wxid" => $request ['from_wxid'],
"member_wxid" => $request ['final_from_wxid'],
"member_name" => $request ['final_from_name'],
"group_wxid" => $group_wxid,
"msg" => $msg,
];
}
return false;
}
/**
* 收款改订单状态:二维码收款
* @param array $request
* @return array|false
* @throws GuzzleException
*/
public function _cashMoney(array $request)
{
$wx_id = $request ['final_from_wxid'];
$money = $request ['money'];
if(!empty($wx_id) && $money > 0){//代表收款成功
//这里我说明一下,当机器人命中用户发起捐款的关键词时,存一个redis,方便这里得到刚才是谁发起的捐赠,这样才可以知道把结果告诉谁
$param = Cache::pull($wx_id);//获取并删除
//Log::info("Cache:".json_encode($param,JSON_UNESCAPED_UNICODE));
if(is_null($param)){//当然啦,如果实在找不到是谁发起的,那就发给主人好了,然后人工处理下即可
$param ['event'] = 'EventFriendMsg';
$param ['robot_wxid'] = $request ['robot_wxid'];
$param ['from_wxid'] = 'sundreamer';
$param ['final_from_wxid'] = $request ['final_from_wxid'];
$param ['final_from_name'] = $request ['final_from_name'];
}
$req_event = $param ['event'];
if($req_event == 'EventGroupMsg'){
$event = 'SendGroupMsgAndAt';
$group_wxid = $param ['from_wxid'];
}else{
$event = 'SendTextMsg';
$group_wxid = '';
}
$msg = "\n [爱心]感谢您的捐赠";
$msg .= "\n?捐赠人昵称:{$param ['final_from_name']}";
$msg .= "\n?捐赠人微信:{$param ['final_from_wxid']}";
$msg .= "\n [红包]捐赠金额:¥{$money}";
// 这里需要调用获取邀请码的借口,就省略啦,得到返回值$invite_arr
$invite_arr = json_decode($body,true);
if($money >= 5){
$msg .= "\n?论坛邀请码:{$invite_arr ['invite_code']}";
$msg .= "\n?论坛地址:https://www.ikam.cn";
//$msg .= "\n [打脸]未被艾特的同学不要捐赠哦";
}
$msg = $this->formatEmoji($msg);
return [
"event" => $event,
"robot_wxid" => $param ['robot_wxid'],
"to_wxid" => $param ['from_wxid'],
"member_wxid" => $param ['final_from_wxid'],
"member_name" => $param ['final_from_name'],
"group_wxid" => $group_wxid,
"msg" => $msg,
];
}
return false;
}