Commit a7d85f3a by HanSon

contact的getUsernameByNickname支持模糊搜索

更改了member的结构,更加层次性、整洁
group增加了map对应ID,方便管理
增加了getMembersByNickname,支持模糊匹配
增加了getUsernameById,根据已设置的map获取username
增加了setMap设置对应ID
member增加了getMembersByNickname,支持模糊匹配成员
1 parent b052cf3a
......@@ -44,17 +44,40 @@ function reply($str)
}
// 设置管理员
function isAdmin($message)
{
$adminAlias = 'hanson1994';
if (in_array($message->fromType, ['Contact', 'Group'])) {
if ($message->fromType === 'Contact') {
return $message->from['Alias'] === $adminAlias;
} else {
return isset($message->sender['Alias']) && $message->sender['Alias'] === $adminAlias;
}
}
return false;
}
$groupMap = [
'vbot 测试群' => 1,
[
'nickname' => 'vbot 测试群',
'id' => 1
]
];
$robot->server->setOnceHandler(function () use ($groupMap) {
group()->reset(group()->map(function ($group, $key) use ($groupMap) {
if (isset($groupMap[$group['NickName']])){
$group['id'] = $groupMap[$group['NickName']];
group()->each(function ($group, $key) use ($groupMap) {
foreach ($groupMap as $map) {
if ($group['NickName'] === $map['nickname']) {
$group['id'] = $map['id'];
$groupMap[$key] = $map['id'];
group()->setMap($key, $map['id']);
}
}
return $group;
})->toArray());
});
});
$robot->server->setMessageHandler(function ($message) use ($path) {
......@@ -70,16 +93,52 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
// 文字信息
if ($message instanceof Text) {
/** @var $message Text */
if (str_contains($message->content, 'vbot') && !$message->isAt) {
return "你好,我叫vbot,我爸是HanSon\n我的项目地址是 https://github.com/HanSon/vbot \n欢迎来给我star!";
}
// 联系人自动回复
if ($message->fromType === 'Contact') {
if ($message->content === '拉我') {
$username = group()->getUsernameById(1);
group()->addMember($username, $message->from['UserName']);
}
if($message->content === '测试'){
$username = group()->getUsernameById(1);
print_r($username);
print_r(group()->get($username));
}
return reply($message->content);
// 群组@我回复
} elseif ($message->fromType === 'Group') {
if (str_contains($message->content, '设置群名称')) {
if (isset($message->sender['Alias']) && $message->sender['Alias'] === 'hanson1994') {
if (str_contains($message->content, '设置群名称') && isAdmin($message)) {
group()->setGroupName($message->from['UserName'], str_replace('设置群名称', '', $message->content));
}
if (str_contains($message->content, '搜人') && isAdmin($message)) {
$nickname = str_replace('搜人', '', $message->content);
$members = group()->getMembersByNickname($message->from['UserName'], $nickname, true);
$result = '搜索结果 数量:' . count($members) . "\n";
foreach ($members as $member) {
$result .= $member['NickName'] . ' ' . $member['UserName'] . "\n";
}
return $result;
}
if (str_contains($message->content, '踢人') && isAdmin($message)) {
$username = str_replace('踢人', '', $message->content);
group()->deleteMember($message->from['UserName'], $username);
}
if (str_contains($message->content, '踢我') && $message->isAt) {
Text::send($message->from['UserName'], '拜拜 ' . $message->sender['NickName']);
group()->deleteMember($message->from['UserName'], $message->sender['UserName']);
return 'vbot 从未见过这么犯贱的人';
}
if ($message->isAt) {
......@@ -198,7 +257,10 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
// 新增好友
if ($message instanceof \Hanson\Vbot\Message\Entity\NewFriend) {
\Hanson\Vbot\Support\Console::log('新加好友:' . $message->from['NickName']);
\Hanson\Vbot\Support\Console::debug('新加好友:' . $message->from['NickName']);
Text::send($message->from['UserName'], "客官,等你很久了!感谢跟 vbot 交朋友,如果可以帮我点个star,谢谢了!https://github.com/HanSon/vbot");
group()->addMember(group()->getUsernameById(1), $message->from['UserName']);
return '现在拉你进去vbot的测试群,进去后为了避免轰炸记得设置免骚扰哦!如果被不小心踢出群,跟我说声“拉我”我就会拉你进群的了。';
}
// 群组变动
......@@ -212,7 +274,7 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
return $message->content;
} elseif ($message->action === 'RENAME') {
// \Hanson\Vbot\Support\Console::log($message->from['NickName'] . ' 改名为 ' . $message->rename);
if (isset($message->from['id']) && $message->from['id'] == 1 && $message->rename !== 'vbot 测试群') {
if (group()->getUsernameById(1) == $message->from['UserName'] && $message->rename !== 'vbot 测试群') {
group()->setGroupName($message->from['UserName'], 'vbot 测试群');
return '行不改名,坐不改姓!';
}
......
......@@ -26,7 +26,7 @@ class Contact extends Collection
*/
public static function getInstance()
{
if(static::$instance === null){
if (static::$instance === null) {
static::$instance = new Contact();
}
......@@ -41,8 +41,8 @@ class Contact extends Collection
*/
public function getContactById($id)
{
return $this->filter(function($item, $key) use ($id){
if($item['Alias'] === $id){
return $this->filter(function ($item, $key) use ($id) {
if ($item['Alias'] === $id) {
return true;
}
})->first();
......@@ -56,22 +56,23 @@ class Contact extends Collection
*/
public function getUsernameById($id)
{
return $this->search(function($item, $key) use ($id){
if($item['Alias'] === $id){
return $this->search(function ($item, $key) use ($id) {
if ($item['Alias'] === $id) {
return true;
}
});
}
/**
* 根据通讯录中的备注获取通讯对象
*
* @param $id
* @return mixed
*/
public function getUsernameByRemarkName( $id)
public function getUsernameByRemarkName($id)
{
return $this->search(function($item, $key) use ($id){
if($item['RemarkName'] === $id){
return $this->search(function ($item, $key) use ($id) {
if ($item['RemarkName'] === $id) {
return true;
}
});
......@@ -81,12 +82,15 @@ class Contact extends Collection
* 根据通讯录中的昵称获取通讯对象
*
* @param $nickname
* @param bool $blur
* @return mixed
*/
public function getUsernameByNickname($nickname)
public function getUsernameByNickname($nickname, $blur = false)
{
return $this->search(function($item, $key) use ($nickname){
if($item['NickName'] === $nickname){
return $this->search(function ($item, $key) use ($nickname, $blur) {
if ($blur && str_contains($item['NickName'], $nickname)) {
return true;
} elseif (!$blur && $item['NickName'] === $nickname) {
return true;
}
});
......@@ -113,7 +117,7 @@ class Contact extends Collection
$result = http()->json($url, [
'UserName' => $username,
'CmdId' => 3,
'OP' => (int) $isStick,
'OP' => (int)$isStick,
'BaseRequest' => server()->baseRequest
], true);
......
......@@ -110,9 +110,11 @@ class ContactFactory
$groupAccount['MemberList'] = $group['MemberList'];
$groupAccount['ChatRoomId'] = $group['EncryChatRoomId'];
group()->put($group['UserName'], $groupAccount);
$members = [];
foreach ($group['MemberList'] as $member) {
member()->put($member['UserName'], $member);
$members[$member['UserName']] = $member;
}
member()->put($group['UserName'], $members);
}
}
......
......@@ -18,6 +18,13 @@ class Group extends Collection
static $instance = null;
/**
* username => id
*
* @var array
*/
public $map = [];
/**
* create a single instance
*
* @return Group
......@@ -32,16 +39,6 @@ class Group extends Collection
}
/**
* 重置数据
*
* @param $array
*/
public function reset($array)
{
static::$instance = new Group($array);
}
/**
* 判断是否群组
*
* @param $userName
......@@ -87,6 +84,53 @@ class Group extends Collection
}
/**
* 根据昵称搜索群成员
*
* @param $groupUsername
* @param $memberNickname
* @param bool $blur
* @return array
*/
public function getMembersByNickname($groupUsername, $memberNickname, $blur = false)
{
$members = $this->get($groupUsername);
$result = [];
foreach ($members['MemberList'] as $member) {
if ($blur && str_contains($member['NickName'], $memberNickname)) {
$result[] = $member;
} elseif (!$blur && $member['NickName'] === $memberNickname) {
$result[] = $member;
}
}
return $result;
}
/**
* 根据ID获取群username
*
* @param $id
* @return mixed
*/
public function getUsernameById($id)
{
return array_search($id, $this->map);
}
/**
* 设置map
*
* @param $username
* @param $id
*/
public function setMap($username, $id)
{
$this->map[$username] = $id;
}
/**
* 创建群聊天
*
* @param array $contacts
......
......@@ -26,11 +26,36 @@ class Member extends Collection
*/
public static function getInstance()
{
if(static::$instance === null){
if (static::$instance === null) {
static::$instance = new Member();
}
return static::$instance;
}
/**
* 根据昵称获取群成员
*
* @param $groupUsername
* @param $memberNickname
* @param bool $blur
* @return array'
*/
public function getMembersByNickname($groupUsername, $memberNickname, $blur = false)
{
$members = $this->get($groupUsername);
$result = [];
foreach ($members as $username => $member) {
if ($blur && str_contains($member['NickName'], $memberNickname)) {
$result[] = $member;
} elseif (!$blur && $member['NickName'] === $memberNickname) {
$result[] = $member;
}
}
return $result;
}
}
\ No newline at end of file
......@@ -51,7 +51,7 @@ class MessageHandler
*/
public static function getInstance()
{
if(static::$instance === null){
if (static::$instance === null) {
static::$instance = new MessageHandler();
}
......@@ -66,7 +66,7 @@ class MessageHandler
*/
public function setMessageHandler(Closure $closure)
{
if(!$closure instanceof Closure){
if (!$closure instanceof Closure) {
throw new \Exception('message handler must be a closure!');
}
......@@ -81,7 +81,7 @@ class MessageHandler
*/
public function setCustomHandler(Closure $closure)
{
if(!$closure instanceof Closure){
if (!$closure instanceof Closure) {
throw new \Exception('custom handler must be a closure!');
}
......@@ -96,7 +96,7 @@ class MessageHandler
*/
public function setExitHandler(Closure $closure)
{
if(!$closure instanceof Closure){
if (!$closure instanceof Closure) {
throw new \Exception('exit handler must be a closure!');
}
......@@ -111,7 +111,7 @@ class MessageHandler
*/
public function setExceptionHandler(Closure $closure)
{
if(!$closure instanceof Closure){
if (!$closure instanceof Closure) {
throw new \Exception('exit handler must be a closure!');
}
......@@ -126,7 +126,7 @@ class MessageHandler
*/
public function setOnceHandler(Closure $closure)
{
if(!$closure instanceof Closure){
if (!$closure instanceof Closure) {
throw new \Exception('exit handler must be a closure!');
}
......@@ -138,29 +138,29 @@ class MessageHandler
*/
public function listen()
{
if($this->onceHandler instanceof Closure){
if ($this->onceHandler instanceof Closure) {
call_user_func_array($this->onceHandler, []);
}
while (true){
if($this->customHandler instanceof Closure){
while (true) {
if ($this->customHandler instanceof Closure) {
call_user_func_array($this->customHandler, []);
}
$time = time();
list($retCode, $selector) = $this->sync->checkSync();
if(in_array($retCode, ['1100', '1101'])){ # 微信客户端上登出或者其他设备登录
if (in_array($retCode, ['1100', '1101'])) { # 微信客户端上登出或者其他设备登录
Console::log('微信客户端正常退出');
if($this->exitHandler){
if ($this->exitHandler) {
call_user_func_array($this->exitHandler, []);
}
break;
}elseif ($retCode == 0){
} elseif ($retCode == 0) {
$this->handlerMessage($selector);
}else{
} else {
Console::log('微信客户端异常退出');
if($this->exceptionHandler){
if ($this->exceptionHandler) {
call_user_func_array($this->exitHandler, []);
}
break;
......@@ -178,33 +178,37 @@ class MessageHandler
*/
private function handlerMessage($selector)
{
if($selector === 0){
if ($selector === 0) {
return;
}
$message = $this->sync->sync();
if(count($message['ModContactList']) > 0){
if (count($message['ModContactList']) > 0) {
foreach ($message['ModContactList'] as $contact) {
if (str_contains($contact['UserName'], '@@')) {
group()->put($contact['UserName'], $contact);
} else {
contact()->put($contact['UserName'], $contact);
}
}
}
if($message['AddMsgList']){
if ($message['AddMsgList']) {
foreach ($message['AddMsgList'] as $msg) {
$content = $this->messageFactory->make($msg);
if($content){
if ($content) {
$this->addToMessageCollection($content);
if($this->handler){
if ($this->handler) {
$reply = call_user_func_array($this->handler, [$content]);
if($reply){
if($reply instanceof Image){
if ($reply) {
if ($reply instanceof Image) {
Image::sendByMsgId($content->from['UserName'], $reply->msg['MsgId']);
}elseif($reply instanceof Video){
} elseif ($reply instanceof Video) {
Video::sendByMsgId($content->from['UserName'], $reply->msg['MsgId']);
}elseif($reply instanceof Emoticon){
} elseif ($reply instanceof Emoticon) {
Emoticon::sendByMsgId($content->from['UserName'], $reply->msg['MsgId']);
}else{
} else {
Text::send($content->from['UserName'], $reply);
}
}
......@@ -221,7 +225,7 @@ class MessageHandler
{
message()->put($message->msg['MsgId'], $message);
if(server()->config['debug']) {
if (server()->config['debug']) {
$file = fopen(System::getPath() . 'message.json', 'a');
fwrite($file, json_encode($message) . PHP_EOL);
fclose($file);
......
......@@ -52,7 +52,7 @@ class GroupChange extends Message implements MessageInterface
if (str_contains($this->msg['Content'], '邀请你')) {
$this->action = 'INVITE';
} elseif (str_contains($this->msg['Content'], '加入了群聊')) {
preg_match('/".+".+"(.+)"加入了群聊/', $this->msg['Content'], $match);
preg_match('/.+"(.+)"加入了群聊/', $this->msg['Content'], $match);
$this->action = 'ADD';
$this->nickname = $match[1];
Console::debug("检测到 {$this->from['NickName']} 有新成员,正在刷新群成员列表...");
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!