Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
hfpp2012
/
vbot
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit d4a8ea8b
authored
Mar 15, 2017
by
HanSon
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
增加 onceHandler
1 parent
1bf3ea16
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
2 deletions
example/index.php
src/Collections/Group.php
src/Core/MessageHandler.php
src/Core/Server.php
example/index.php
View file @
d4a8ea8
...
...
@@ -44,6 +44,19 @@ function reply($str)
}
$groupMap
=
[
'vbot 测试群'
=>
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'
]];
}
return
$group
;
})
->
toArray
());
});
$robot
->
server
->
setMessageHandler
(
function
(
$message
)
use
(
$path
)
{
/** @var $message Message */
...
...
@@ -63,9 +76,11 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
// 群组@我回复
}
elseif
(
$message
->
fromType
===
'Group'
)
{
if
(
str_contains
(
$message
->
content
,
'设置群名称'
)
&&
$message
->
from
[
'Alias'
]
===
'hanson1994'
)
{
if
(
str_contains
(
$message
->
content
,
'设置群名称'
))
{
if
(
isset
(
$message
->
sender
[
'Alias'
])
&&
$message
->
sender
[
'Alias'
]
===
'hanson1994'
)
{
group
()
->
setGroupName
(
$message
->
from
[
'UserName'
],
str_replace
(
'设置群名称'
,
''
,
$message
->
content
));
}
}
if
(
$message
->
isAt
)
{
return
reply
(
$message
->
content
);
...
...
@@ -197,7 +212,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
(
$message
->
rename
!==
'vbot 测试群'
)
{
if
(
isset
(
$message
->
from
[
'id'
])
&&
$message
->
from
[
'id'
]
==
1
&&
$message
->
rename
!==
'vbot 测试群'
)
{
group
()
->
setGroupName
(
$message
->
from
[
'UserName'
],
'vbot 测试群'
);
return
'行不改名,坐不改姓!'
;
}
...
...
src/Collections/Group.php
View file @
d4a8ea8
...
...
@@ -32,6 +32,16 @@ class Group extends Collection
}
/**
* 重置数据
*
* @param $array
*/
public
function
reset
(
$array
)
{
static
::
$instance
=
new
Group
(
$array
);
}
/**
* 判断是否群组
*
* @param $userName
...
...
@@ -62,6 +72,21 @@ class Group extends Collection
}
/**
* 根据通讯录中的昵称获取通讯对象
*
* @param $nickname
* @return mixed
*/
public
function
getUsernameByNickname
(
$nickname
)
{
return
$this
->
search
(
function
(
$item
,
$key
)
use
(
$nickname
){
if
(
$item
[
'NickName'
]
===
$nickname
){
return
true
;
}
});
}
/**
* 创建群聊天
*
* @param array $contacts
...
...
src/Core/MessageHandler.php
View file @
d4a8ea8
...
...
@@ -32,6 +32,8 @@ class MessageHandler
private
$exceptionHandler
;
private
$onceHandler
;
private
$sync
;
private
$messageFactory
;
...
...
@@ -117,10 +119,29 @@ class MessageHandler
}
/**
* 执行一次的处理器
*
* @param Closure $closure
* @throws \Exception
*/
public
function
setOnceHandler
(
Closure
$closure
)
{
if
(
!
$closure
instanceof
Closure
){
throw
new
\Exception
(
'exit handler must be a closure!'
);
}
$this
->
onceHandler
=
$closure
;
}
/**
* 轮询消息API接口
*/
public
function
listen
()
{
if
(
$this
->
onceHandler
instanceof
Closure
){
call_user_func_array
(
$this
->
onceHandler
,
[]);
}
while
(
true
){
if
(
$this
->
customHandler
instanceof
Closure
){
call_user_func_array
(
$this
->
customHandler
,
[]);
...
...
src/Core/Server.php
View file @
d4a8ea8
...
...
@@ -339,4 +339,9 @@ class Server
{
MessageHandler
::
getInstance
()
->
setExceptionHandler
(
$closure
);
}
public
function
setOnceHandler
(
\Closure
$closure
)
{
MessageHandler
::
getInstance
()
->
setOnceHandler
(
$closure
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment