本次版本发布主要解决了同步模式中文乱码问题,虽然直接中文不再乱码,但是依然鼓励大家使用 unicode模式的json进行交互!
至于下载,请移步码云仓库吧!
下面,贴一下php-sdk的小爱聊天代码吧。(当然 你可以想去码云仓库下载完整版),如果不想自己部署服务端,可以直接在你插件里配置官方demo链接:http://www.uera.cn/robot.php
public function xiaoAi($request)
{
if ($request ['type'] != 1)
return false;
$api = 'http://jiuli.xiaoapi.cn';
$must = false;
$req_event = $request ['event'];
$msg = $request ['msg'];
if ($req_event == 'EventGroupMsg') {
$partnner = '!\ [@at,nickname=(.*?),wxid=(.*?)\]!';
preg_match_all($partnner, $request ['msg'], $match);
$at_nick_name = $at_wx_id = [];
if (!empty($match [0])) {//说明被艾特了~
foreach ($match [0] as $k => $at) {
$msg = trim(str_replace($at, '', $request ['msg']));
$at_nick_name [$k] = $match [1] [$k];
$at_wx_id [$k] = $match [2] [$k];
}
if (in_array($request ['robot_wxid'], $at_wx_id))
$must = true;
$event = 'SendGroupMsgAndAt';
} else {
$event = 'SendTextMsg';
}
$group_wxid = $request ['from_wxid'];
} else {
$must = true;
$event = 'SendTextMsg';
$group_wxid = '';
}
if ($must || rand(1, 5) == 3) {//如果被艾特或者私聊必回,否则 20%的几率回复
$api = 'http://jiuli.xiaoapi.cn/i/xiaoai_tts.php';
$resp = file_get_contents("{$api}?msg={$msg}");
$resps = json_decode($resp, true) ?? ['text' => ''];
if (empty(trim($resps ['text'])))
return false;
$msg = $resps ['text'];
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;
}