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 b3bfcfae
authored
Mar 16, 2017
by
HanSon
Committed by
GitHub
Mar 16, 2017
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge pull request #50 from HanSon/dev
🎲
优化群控功能
2 parents
db06d623
cdd3479a
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
269 additions
and
95 deletions
README.md
example/index.php
src/Collections/Contact.php
src/Collections/ContactFactory.php
src/Collections/Group.php
src/Collections/Member.php
src/Core/Http.php
src/Core/MessageFactory.php
src/Core/MessageHandler.php
src/Core/Server.php
src/Core/Sync.php
src/Message/Entity/Emoticon.php
src/Message/Entity/GroupChange.php
src/Message/Entity/Image.php
src/Message/Entity/NewFriend.php
src/Message/Entity/Text.php
src/Message/Entity/Video.php
src/Message/UploadAble.php
src/Support/Console.php
README.md
View file @
b3bfcfa
...
@@ -31,13 +31,22 @@ PS:运行后二维码将保存于设置的缓存目录,命名为qr.png,控
...
@@ -31,13 +31,22 @@ PS:运行后二维码将保存于设置的缓存目录,命名为qr.png,控
**请在terminal运行!请在terminal运行!请在terminal运行!**
**请在terminal运行!请在terminal运行!请在terminal运行!**
## 体验
<img
src=
"https://ws2.sinaimg.cn/large/685b97a1gy1fdordpa0cgj20e80e811z.jpg"
height=
"320"
>
扫码后,验证输入“上山打老虎”即可自动加为好友并且拉入vbot群。
vbot并非24小时执行,有时会因为开发调试等原因暂停功能。如果碰巧遇到关闭情况,可加Q群 492548647 了解开放时间。执行后发送“拉我”即可自动邀请进群。
vbot示例源码为 https://github.com/HanSon/vbot/blob/master/example/index.php
## 文档
## 文档
详细文档在
[
wiki
](
https://github.com/HanSon/vbot/wiki
)
中
详细文档在
[
wiki
](
https://github.com/HanSon/vbot/wiki
)
中
### 例子
### 小DEMO
[
所有类型例子
](
https://github.com/HanSon/vbot/blob/master/example/index.php
)
[
红包提醒
](
https://github.com/HanSon/vbot/blob/master/example/hongbao.php
)
[
红包提醒
](
https://github.com/HanSon/vbot/blob/master/example/hongbao.php
)
...
...
example/index.php
View file @
b3bfcfa
This diff is collapsed.
Click to expand it.
src/Collections/Contact.php
View file @
b3bfcfa
...
@@ -26,7 +26,7 @@ class Contact extends Collection
...
@@ -26,7 +26,7 @@ class Contact extends Collection
*/
*/
public
static
function
getInstance
()
public
static
function
getInstance
()
{
{
if
(
static
::
$instance
===
null
)
{
if
(
static
::
$instance
===
null
)
{
static
::
$instance
=
new
Contact
();
static
::
$instance
=
new
Contact
();
}
}
...
@@ -41,8 +41,8 @@ class Contact extends Collection
...
@@ -41,8 +41,8 @@ class Contact extends Collection
*/
*/
public
function
getContactById
(
$id
)
public
function
getContactById
(
$id
)
{
{
return
$this
->
filter
(
function
(
$item
,
$key
)
use
(
$id
)
{
return
$this
->
filter
(
function
(
$item
,
$key
)
use
(
$id
)
{
if
(
$item
[
'Alias'
]
===
$id
)
{
if
(
$item
[
'Alias'
]
===
$id
)
{
return
true
;
return
true
;
}
}
})
->
first
();
})
->
first
();
...
@@ -56,22 +56,23 @@ class Contact extends Collection
...
@@ -56,22 +56,23 @@ class Contact extends Collection
*/
*/
public
function
getUsernameById
(
$id
)
public
function
getUsernameById
(
$id
)
{
{
return
$this
->
search
(
function
(
$item
,
$key
)
use
(
$id
)
{
return
$this
->
search
(
function
(
$item
,
$key
)
use
(
$id
)
{
if
(
$item
[
'Alias'
]
===
$id
)
{
if
(
$item
[
'Alias'
]
===
$id
)
{
return
true
;
return
true
;
}
}
});
});
}
}
/**
/**
* 根据通讯录中的备注获取通讯对象
* 根据通讯录中的备注获取通讯对象
*
*
* @param $id
* @param $id
* @return mixed
* @return mixed
*/
*/
public
function
getUsernameByRemarkName
(
$id
)
public
function
getUsernameByRemarkName
(
$id
)
{
{
return
$this
->
search
(
function
(
$item
,
$key
)
use
(
$id
)
{
return
$this
->
search
(
function
(
$item
,
$key
)
use
(
$id
)
{
if
(
$item
[
'RemarkName'
]
===
$id
)
{
if
(
$item
[
'RemarkName'
]
===
$id
)
{
return
true
;
return
true
;
}
}
});
});
...
@@ -81,12 +82,15 @@ class Contact extends Collection
...
@@ -81,12 +82,15 @@ class Contact extends Collection
* 根据通讯录中的昵称获取通讯对象
* 根据通讯录中的昵称获取通讯对象
*
*
* @param $nickname
* @param $nickname
* @param bool $blur
* @return mixed
* @return mixed
*/
*/
public
function
getUsernameByNickname
(
$nickname
)
public
function
getUsernameByNickname
(
$nickname
,
$blur
=
false
)
{
{
return
$this
->
search
(
function
(
$item
,
$key
)
use
(
$nickname
){
return
$this
->
search
(
function
(
$item
,
$key
)
use
(
$nickname
,
$blur
)
{
if
(
$item
[
'NickName'
]
===
$nickname
){
if
(
$blur
&&
str_contains
(
$item
[
'NickName'
],
$nickname
))
{
return
true
;
}
elseif
(
!
$blur
&&
$item
[
'NickName'
]
===
$nickname
)
{
return
true
;
return
true
;
}
}
});
});
...
@@ -113,7 +117,7 @@ class Contact extends Collection
...
@@ -113,7 +117,7 @@ class Contact extends Collection
$result
=
http
()
->
json
(
$url
,
[
$result
=
http
()
->
json
(
$url
,
[
'UserName'
=>
$username
,
'UserName'
=>
$username
,
'CmdId'
=>
3
,
'CmdId'
=>
3
,
'OP'
=>
(
int
)
$isStick
,
'OP'
=>
(
int
)
$isStick
,
'BaseRequest'
=>
server
()
->
baseRequest
'BaseRequest'
=>
server
()
->
baseRequest
],
true
);
],
true
);
...
...
src/Collections/ContactFactory.php
View file @
b3bfcfa
...
@@ -34,11 +34,11 @@ class ContactFactory
...
@@ -34,11 +34,11 @@ class ContactFactory
$this
->
makeContactList
();
$this
->
makeContactList
();
$contact
=
contact
()
->
get
(
myself
()
->
username
);
$contact
=
contact
()
->
get
(
myself
()
->
username
);
myself
()
->
alias
=
isset
(
$contact
[
'Alias'
])
?
$contact
[
'Alias'
]
:
myself
()
->
nickname
?
:
myself
()
->
username
;
myself
()
->
alias
=
isset
(
$contact
[
'Alias'
])
?
$contact
[
'Alias'
]
:
myself
()
->
nickname
?:
myself
()
->
username
;
$this
->
getBatchGroupMembers
();
$this
->
getBatchGroupMembers
();
if
(
server
()
->
config
[
'debug'
])
{
if
(
server
()
->
config
[
'debug'
])
{
FileManager
::
download
(
'contact.json'
,
json_encode
(
contact
()
->
all
()));
FileManager
::
download
(
'contact.json'
,
json_encode
(
contact
()
->
all
()));
FileManager
::
download
(
'member.json'
,
json_encode
(
member
()
->
all
()));
FileManager
::
download
(
'member.json'
,
json_encode
(
member
()
->
all
()));
FileManager
::
download
(
'group.json'
,
json_encode
(
group
()
->
all
()));
FileManager
::
download
(
'group.json'
,
json_encode
(
group
()
->
all
()));
...
@@ -56,23 +56,23 @@ class ContactFactory
...
@@ -56,23 +56,23 @@ class ContactFactory
$url
=
sprintf
(
server
()
->
baseUri
.
'/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s&seq=%s'
,
server
()
->
passTicket
,
server
()
->
skey
,
time
(),
$seq
);
$url
=
sprintf
(
server
()
->
baseUri
.
'/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s&seq=%s'
,
server
()
->
passTicket
,
server
()
->
skey
,
time
(),
$seq
);
$result
=
http
()
->
json
(
$url
,
[],
true
);
$result
=
http
()
->
json
(
$url
,
[],
true
);
$memberList
=
$result
[
'MemberList'
];
$seq
=
$result
[
'Seq'
];
if
(
isset
(
$result
[
'MemberList'
])
&&
$result
[
'MemberList'
])
{
foreach
(
$result
[
'MemberList'
]
as
$contact
)
{
foreach
(
$memberList
as
$contact
)
{
if
(
official
()
->
isOfficial
(
$contact
[
'VerifyFlag'
]))
{
#公众号
if
(
official
()
->
isOfficial
(
$contact
[
'VerifyFlag'
])){
#公众号
Official
::
getInstance
()
->
put
(
$contact
[
'UserName'
],
$contact
);
Official
::
getInstance
()
->
put
(
$contact
[
'UserName'
],
$contact
);
}
elseif
(
in_array
(
$contact
[
'UserName'
],
static
::
SPECIAL_USERS
))
{
# 特殊账户
}
elseif
(
in_array
(
$contact
[
'UserName'
],
static
::
SPECIAL_USERS
)){
# 特殊账户
Special
::
getInstance
()
->
put
(
$contact
[
'UserName'
],
$contact
);
Special
::
getInstance
()
->
put
(
$contact
[
'UserName'
],
$contact
);
}
elseif
(
strstr
(
$contact
[
'UserName'
],
'@@'
)
!==
false
)
{
# 群聊
}
elseif
(
strstr
(
$contact
[
'UserName'
],
'@@'
)
!==
false
){
# 群聊
group
()
->
put
(
$contact
[
'UserName'
],
$contact
);
group
()
->
put
(
$contact
[
'UserName'
],
$contact
);
}
else
{
}
else
{
contact
()
->
put
(
$contact
[
'UserName'
],
$contact
);
contact
()
->
put
(
$contact
[
'UserName'
],
$contact
);
}
}
}
}
}
if
(
$seq
!=
0
)
{
if
(
isset
(
$result
[
'Seq'
])
&&
$result
[
'Seq'
]
!=
0
)
{
$this
->
makeContactList
(
$
seq
);
$this
->
makeContactList
(
$
result
[
'Seq'
]
);
}
}
}
}
...
@@ -84,7 +84,7 @@ class ContactFactory
...
@@ -84,7 +84,7 @@ class ContactFactory
$url
=
sprintf
(
server
()
->
baseUri
.
'/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s'
,
time
(),
server
()
->
passTicket
);
$url
=
sprintf
(
server
()
->
baseUri
.
'/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s'
,
time
(),
server
()
->
passTicket
);
$list
=
[];
$list
=
[];
group
()
->
each
(
function
(
$item
,
$key
)
use
(
&
$list
)
{
group
()
->
each
(
function
(
$item
,
$key
)
use
(
&
$list
)
{
$list
[]
=
[
'UserName'
=>
$key
,
'EncryChatRoomId'
=>
''
];
$list
[]
=
[
'UserName'
=>
$key
,
'EncryChatRoomId'
=>
''
];
});
});
...
@@ -104,13 +104,15 @@ class ContactFactory
...
@@ -104,13 +104,15 @@ class ContactFactory
*/
*/
private
function
initGroupMembers
(
$array
)
private
function
initGroupMembers
(
$array
)
{
{
foreach
(
$array
[
'ContactList'
]
as
$group
)
{
if
(
isset
(
$array
[
'ContactList'
])
&&
$array
[
'ContactList'
])
{
$groupAccount
=
group
()
->
get
(
$group
[
'UserName'
]);
foreach
(
$array
[
'ContactList'
]
as
$group
)
{
$groupAccount
[
'MemberList'
]
=
$group
[
'MemberList'
];
$groupAccount
=
group
()
->
get
(
$group
[
'UserName'
]);
$groupAccount
[
'ChatRoomId'
]
=
$group
[
'EncryChatRoomId'
];
$groupAccount
[
'MemberList'
]
=
$group
[
'MemberList'
];
group
()
->
put
(
$group
[
'UserName'
],
$groupAccount
);
$groupAccount
[
'ChatRoomId'
]
=
$group
[
'EncryChatRoomId'
];
foreach
(
$group
[
'MemberList'
]
as
$member
)
{
group
()
->
put
(
$group
[
'UserName'
],
$groupAccount
);
member
()
->
put
(
$member
[
'UserName'
],
$member
);
foreach
(
$group
[
'MemberList'
]
as
$member
)
{
member
()
->
put
(
$member
[
'UserName'
],
$member
);
}
}
}
}
}
...
...
src/Collections/Group.php
View file @
b3bfcfa
...
@@ -18,6 +18,13 @@ class Group extends Collection
...
@@ -18,6 +18,13 @@ class Group extends Collection
static
$instance
=
null
;
static
$instance
=
null
;
/**
/**
* username => id
*
* @var array
*/
public
$map
=
[];
/**
* create a single instance
* create a single instance
*
*
* @return Group
* @return Group
...
@@ -62,6 +69,68 @@ class Group extends Collection
...
@@ -62,6 +69,68 @@ 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 $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
* @param array $contacts
...
...
src/Collections/Member.php
View file @
b3bfcfa
...
@@ -26,7 +26,7 @@ class Member extends Collection
...
@@ -26,7 +26,7 @@ class Member extends Collection
*/
*/
public
static
function
getInstance
()
public
static
function
getInstance
()
{
{
if
(
static
::
$instance
===
null
)
{
if
(
static
::
$instance
===
null
)
{
static
::
$instance
=
new
Member
();
static
::
$instance
=
new
Member
();
}
}
...
...
src/Core/Http.php
View file @
b3bfcfa
...
@@ -50,9 +50,11 @@ class Http
...
@@ -50,9 +50,11 @@ class Http
return
$array
?
json_decode
(
$content
,
true
)
:
$content
;
return
$array
?
json_decode
(
$content
,
true
)
:
$content
;
}
}
public
function
json
(
$url
,
$
options
=
[],
$array
=
false
)
public
function
json
(
$url
,
$
params
=
[],
$array
=
false
,
$extra
=
[]
)
{
{
$content
=
$this
->
request
(
$url
,
'POST'
,
[
'json'
=>
$options
]);
$params
=
array_merge
([
'json'
=>
$params
],
$extra
);
$content
=
$this
->
request
(
$url
,
'POST'
,
$params
);
return
$array
?
json_decode
(
$content
,
true
)
:
$content
;
return
$array
?
json_decode
(
$content
,
true
)
:
$content
;
}
}
...
@@ -86,15 +88,9 @@ class Http
...
@@ -86,15 +88,9 @@ class Http
*/
*/
public
function
request
(
$url
,
$method
=
'GET'
,
$options
=
[])
public
function
request
(
$url
,
$method
=
'GET'
,
$options
=
[])
{
{
try
{
$response
=
$this
->
getClient
()
->
request
(
$method
,
$url
,
$options
);
$response
=
$this
->
getClient
()
->
request
(
$method
,
$url
,
$options
);
return
$response
->
getBody
()
->
getContents
();
}
catch
(
\Exception
$e
){
Console
::
log
(
'http链接失败:'
.
$e
->
getMessage
(),
Console
::
ERROR
);
Console
::
log
(
'错误URL:'
.
$url
,
Console
::
ERROR
);
}
return
null
;
return
$response
->
getBody
()
->
getContents
()
;
}
}
...
...
src/Core/MessageFactory.php
View file @
b3bfcfa
...
@@ -68,7 +68,7 @@ class MessageFactory
...
@@ -68,7 +68,7 @@ class MessageFactory
else
if
(
str_contains
(
$msg
[
'Content'
],
'添加'
)
||
str_contains
(
$msg
[
'Content'
],
'have added'
)
||
str_contains
(
$msg
[
'Content'
],
'打招呼'
)){
else
if
(
str_contains
(
$msg
[
'Content'
],
'添加'
)
||
str_contains
(
$msg
[
'Content'
],
'have added'
)
||
str_contains
(
$msg
[
'Content'
],
'打招呼'
)){
# 添加好友
# 添加好友
return
new
NewFriend
(
$msg
);
return
new
NewFriend
(
$msg
);
}
else
if
(
str_contains
(
$msg
[
'Content'
],
'加入了群聊'
)
||
str_contains
(
$msg
[
'Content'
],
'移出了群聊'
)
||
str_contains
(
$msg
[
'Content'
],
'改群名为'
)){
}
else
if
(
str_contains
(
$msg
[
'Content'
],
'加入了群聊'
)
||
str_contains
(
$msg
[
'Content'
],
'移出了群聊'
)
||
str_contains
(
$msg
[
'Content'
],
'改群名为'
)
||
str_contains
(
$msg
[
'Content'
],
'移出群聊'
)
||
str_contains
(
$msg
[
'Content'
],
'邀请你'
)
){
return
new
GroupChange
(
$msg
);
return
new
GroupChange
(
$msg
);
}
}
break
;
break
;
...
...
src/Core/MessageHandler.php
View file @
b3bfcfa
...
@@ -32,6 +32,8 @@ class MessageHandler
...
@@ -32,6 +32,8 @@ class MessageHandler
private
$exceptionHandler
;
private
$exceptionHandler
;
private
$onceHandler
;
private
$sync
;
private
$sync
;
private
$messageFactory
;
private
$messageFactory
;
...
@@ -49,7 +51,7 @@ class MessageHandler
...
@@ -49,7 +51,7 @@ class MessageHandler
*/
*/
public
static
function
getInstance
()
public
static
function
getInstance
()
{
{
if
(
static
::
$instance
===
null
)
{
if
(
static
::
$instance
===
null
)
{
static
::
$instance
=
new
MessageHandler
();
static
::
$instance
=
new
MessageHandler
();
}
}
...
@@ -64,7 +66,7 @@ class MessageHandler
...
@@ -64,7 +66,7 @@ class MessageHandler
*/
*/
public
function
setMessageHandler
(
Closure
$closure
)
public
function
setMessageHandler
(
Closure
$closure
)
{
{
if
(
!
$closure
instanceof
Closure
)
{
if
(
!
$closure
instanceof
Closure
)
{
throw
new
\Exception
(
'message handler must be a closure!'
);
throw
new
\Exception
(
'message handler must be a closure!'
);
}
}
...
@@ -79,7 +81,7 @@ class MessageHandler
...
@@ -79,7 +81,7 @@ class MessageHandler
*/
*/
public
function
setCustomHandler
(
Closure
$closure
)
public
function
setCustomHandler
(
Closure
$closure
)
{
{
if
(
!
$closure
instanceof
Closure
)
{
if
(
!
$closure
instanceof
Closure
)
{
throw
new
\Exception
(
'custom handler must be a closure!'
);
throw
new
\Exception
(
'custom handler must be a closure!'
);
}
}
...
@@ -94,7 +96,7 @@ class MessageHandler
...
@@ -94,7 +96,7 @@ class MessageHandler
*/
*/
public
function
setExitHandler
(
Closure
$closure
)
public
function
setExitHandler
(
Closure
$closure
)
{
{
if
(
!
$closure
instanceof
Closure
)
{
if
(
!
$closure
instanceof
Closure
)
{
throw
new
\Exception
(
'exit handler must be a closure!'
);
throw
new
\Exception
(
'exit handler must be a closure!'
);
}
}
...
@@ -109,7 +111,7 @@ class MessageHandler
...
@@ -109,7 +111,7 @@ class MessageHandler
*/
*/
public
function
setExceptionHandler
(
Closure
$closure
)
public
function
setExceptionHandler
(
Closure
$closure
)
{
{
if
(
!
$closure
instanceof
Closure
)
{
if
(
!
$closure
instanceof
Closure
)
{
throw
new
\Exception
(
'exit handler must be a closure!'
);
throw
new
\Exception
(
'exit handler must be a closure!'
);
}
}
...
@@ -117,29 +119,48 @@ class MessageHandler
...
@@ -117,29 +119,48 @@ 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接口
* 轮询消息API接口
*/
*/
public
function
listen
()
public
function
listen
()
{
{
while
(
true
){
if
(
$this
->
onceHandler
instanceof
Closure
)
{
if
(
$this
->
customHandler
instanceof
Closure
){
call_user_func_array
(
$this
->
onceHandler
,
[]);
}
while
(
true
)
{
if
(
$this
->
customHandler
instanceof
Closure
)
{
call_user_func_array
(
$this
->
customHandler
,
[]);
call_user_func_array
(
$this
->
customHandler
,
[]);
}
}
$time
=
time
();
$time
=
time
();
list
(
$retCode
,
$selector
)
=
$this
->
sync
->
checkSync
();
list
(
$retCode
,
$selector
)
=
$this
->
sync
->
checkSync
();
if
(
in_array
(
$retCode
,
[
'1100'
,
'1101'
]))
{
# 微信客户端上登出或者其他设备登录
if
(
in_array
(
$retCode
,
[
'1100'
,
'1101'
]))
{
# 微信客户端上登出或者其他设备登录
Console
::
log
(
'微信客户端正常退出'
);
Console
::
log
(
'微信客户端正常退出'
);
if
(
$this
->
exitHandler
)
{
if
(
$this
->
exitHandler
)
{
call_user_func_array
(
$this
->
exitHandler
,
[]);
call_user_func_array
(
$this
->
exitHandler
,
[]);
}
}
break
;
break
;
}
elseif
(
$retCode
==
0
)
{
}
elseif
(
$retCode
==
0
)
{
$this
->
handlerMessage
(
$selector
);
$this
->
handlerMessage
(
$selector
);
}
else
{
}
else
{
Console
::
log
(
'微信客户端异常退出'
);
Console
::
log
(
'微信客户端异常退出'
);
if
(
$this
->
exceptionHandler
)
{
if
(
$this
->
exceptionHandler
)
{
call_user_func_array
(
$this
->
exitHandler
,
[]);
call_user_func_array
(
$this
->
exitHandler
,
[]);
}
}
break
;
break
;
...
@@ -157,27 +178,37 @@ class MessageHandler
...
@@ -157,27 +178,37 @@ class MessageHandler
*/
*/
private
function
handlerMessage
(
$selector
)
private
function
handlerMessage
(
$selector
)
{
{
if
(
$selector
===
0
)
{
if
(
$selector
===
0
)
{
return
;
return
;
}
}
$message
=
$this
->
sync
->
sync
();
$message
=
$this
->
sync
->
sync
();
if
(
$message
[
'AddMsgList'
]){
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'
])
{
foreach
(
$message
[
'AddMsgList'
]
as
$msg
)
{
foreach
(
$message
[
'AddMsgList'
]
as
$msg
)
{
$content
=
$this
->
messageFactory
->
make
(
$msg
);
$content
=
$this
->
messageFactory
->
make
(
$msg
);
if
(
$content
)
{
if
(
$content
)
{
$this
->
addToMessageCollection
(
$content
);
$this
->
addToMessageCollection
(
$content
);
if
(
$this
->
handler
)
{
if
(
$this
->
handler
)
{
$reply
=
call_user_func_array
(
$this
->
handler
,
[
$content
]);
$reply
=
call_user_func_array
(
$this
->
handler
,
[
$content
]);
if
(
$reply
)
{
if
(
$reply
)
{
if
(
$reply
instanceof
Image
)
{
if
(
$reply
instanceof
Image
)
{
Image
::
sendByMsgId
(
$content
->
from
[
'UserName'
],
$reply
->
msg
[
'MsgId'
]);
Image
::
sendByMsgId
(
$content
->
from
[
'UserName'
],
$reply
->
msg
[
'MsgId'
]);
}
elseif
(
$reply
instanceof
Video
)
{
}
elseif
(
$reply
instanceof
Video
)
{
Video
::
sendByMsgId
(
$content
->
from
[
'UserName'
],
$reply
->
msg
[
'MsgId'
]);
Video
::
sendByMsgId
(
$content
->
from
[
'UserName'
],
$reply
->
msg
[
'MsgId'
]);
}
elseif
(
$reply
instanceof
Emoticon
)
{
}
elseif
(
$reply
instanceof
Emoticon
)
{
Emoticon
::
sendByMsgId
(
$content
->
from
[
'UserName'
],
$reply
->
msg
[
'MsgId'
]);
Emoticon
::
sendByMsgId
(
$content
->
from
[
'UserName'
],
$reply
->
msg
[
'MsgId'
]);
}
else
{
}
else
{
Text
::
send
(
$content
->
from
[
'UserName'
],
$reply
);
Text
::
send
(
$content
->
from
[
'UserName'
],
$reply
);
}
}
}
}
...
@@ -194,7 +225,7 @@ class MessageHandler
...
@@ -194,7 +225,7 @@ class MessageHandler
{
{
message
()
->
put
(
$message
->
msg
[
'MsgId'
],
$message
);
message
()
->
put
(
$message
->
msg
[
'MsgId'
],
$message
);
if
(
server
()
->
config
[
'debug'
])
{
if
(
server
()
->
config
[
'debug'
])
{
$file
=
fopen
(
System
::
getPath
()
.
'message.json'
,
'a'
);
$file
=
fopen
(
System
::
getPath
()
.
'message.json'
,
'a'
);
fwrite
(
$file
,
json_encode
(
$message
)
.
PHP_EOL
);
fwrite
(
$file
,
json_encode
(
$message
)
.
PHP_EOL
);
fclose
(
$file
);
fclose
(
$file
);
...
...
src/Core/Server.php
View file @
b3bfcfa
...
@@ -88,8 +88,10 @@ class Server
...
@@ -88,8 +88,10 @@ class Server
$this
->
statusNotify
();
$this
->
statusNotify
();
Console
::
log
(
'开始初始化联系人'
);
Console
::
log
(
'开始初始化联系人'
);
$this
->
initContact
();
$this
->
initContact
();
Console
::
log
(
sprintf
(
"初始化联系人成功
\n
群数量: %d
\n
联系人数量: %d
\n
公众号数量: %d
\n
特殊号数量: %d"
,
group
()
->
count
(),
contact
()
->
count
(),
official
()
->
count
(),
Special
::
getInstance
()
->
count
()));
Console
::
log
(
'初始化联系人成功'
);
Console
::
log
(
sprintf
(
"群数量: %d"
,
group
()
->
count
()));
Console
::
log
(
sprintf
(
"联系人数量: %d"
,
contact
()
->
count
()));
Console
::
log
(
sprintf
(
"公众号数量: %d"
,
official
()
->
count
()));
MessageHandler
::
getInstance
()
->
listen
();
MessageHandler
::
getInstance
()
->
listen
();
}
}
...
@@ -337,4 +339,9 @@ class Server
...
@@ -337,4 +339,9 @@ class Server
{
{
MessageHandler
::
getInstance
()
->
setExceptionHandler
(
$closure
);
MessageHandler
::
getInstance
()
->
setExceptionHandler
(
$closure
);
}
}
public
function
setOnceHandler
(
\Closure
$closure
)
{
MessageHandler
::
getInstance
()
->
setOnceHandler
(
$closure
);
}
}
}
\ No newline at end of file
\ No newline at end of file
src/Core/Sync.php
View file @
b3bfcfa
...
@@ -52,7 +52,7 @@ class Sync
...
@@ -52,7 +52,7 @@ class Sync
'BaseRequest'
=>
server
()
->
baseRequest
,
'BaseRequest'
=>
server
()
->
baseRequest
,
'SyncKey'
=>
server
()
->
syncKey
,
'SyncKey'
=>
server
()
->
syncKey
,
'rr'
=>
~
time
()
'rr'
=>
~
time
()
],
true
);
],
true
,
[
'timeout'
=>
35
]
);
if
(
$result
[
'BaseResponse'
][
'Ret'
]
==
0
){
if
(
$result
[
'BaseResponse'
][
'Ret'
]
==
0
){
$this
->
generateSyncKey
(
$result
);
$this
->
generateSyncKey
(
$result
);
...
@@ -60,7 +60,7 @@ class Sync
...
@@ -60,7 +60,7 @@ class Sync
return
$result
;
return
$result
;
}
catch
(
\Exception
$e
){
}
catch
(
\Exception
$e
){
return
null
;
$this
->
sync
()
;
}
}
}
}
...
@@ -107,7 +107,7 @@ class Sync
...
@@ -107,7 +107,7 @@ class Sync
*/
*/
public
function
debugMessage
(
$retCode
,
$selector
,
$sleep
=
null
)
public
function
debugMessage
(
$retCode
,
$selector
,
$sleep
=
null
)
{
{
Console
::
log
(
'
[DEBUG] retcode:'
.
$retCode
.
' selector:'
.
$selector
);
Console
::
log
(
'
retcode:'
.
$retCode
.
' selector:'
.
$selector
,
Console
::
WARNING
);
if
(
$sleep
){
if
(
$sleep
){
sleep
(
$sleep
);
sleep
(
$sleep
);
...
...
src/Message/Entity/Emoticon.php
View file @
b3bfcfa
...
@@ -35,7 +35,7 @@ class Emoticon extends Message implements MediaInterface, MessageInterface
...
@@ -35,7 +35,7 @@ class Emoticon extends Message implements MediaInterface, MessageInterface
$response
=
static
::
uploadMedia
(
$username
,
$file
);
$response
=
static
::
uploadMedia
(
$username
,
$file
);
if
(
!
$response
)
{
if
(
!
$response
)
{
Console
::
log
(
"表情
{
$file
}
上传失败"
);
Console
::
log
(
"表情
{
$file
}
上传失败"
,
Console
::
WARNING
);
return
false
;
return
false
;
}
}
...
@@ -57,7 +57,7 @@ class Emoticon extends Message implements MediaInterface, MessageInterface
...
@@ -57,7 +57,7 @@ class Emoticon extends Message implements MediaInterface, MessageInterface
$result
=
http
()
->
json
(
$url
,
$data
,
true
);
$result
=
http
()
->
json
(
$url
,
$data
,
true
);
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
)
{
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
)
{
Console
::
log
(
'发送表情失败'
);
Console
::
log
(
'发送表情失败'
,
Console
::
WARNING
);
return
false
;
return
false
;
}
}
...
@@ -79,6 +79,22 @@ class Emoticon extends Message implements MediaInterface, MessageInterface
...
@@ -79,6 +79,22 @@ class Emoticon extends Message implements MediaInterface, MessageInterface
}
}
/**
/**
* 从当前账号的本地表情库随机发送一个
*
* @param $username
*/
public
static
function
sendRandom
(
$username
)
{
$path
=
static
::
getPath
(
static
::
$folder
);
$files
=
scandir
(
$path
);
unset
(
$files
[
0
],
$files
[
1
]);
$msgId
=
$files
[
array_rand
(
$files
)];
static
::
send
(
$username
,
$path
.
'/'
.
$msgId
);
}
/**
* 下载文件
* 下载文件
*
*
* @return mixed
* @return mixed
...
...
src/Message/Entity/GroupChange.php
View file @
b3bfcfa
...
@@ -18,8 +18,27 @@ class GroupChange extends Message implements MessageInterface
...
@@ -18,8 +18,27 @@ class GroupChange extends Message implements MessageInterface
public
$action
;
public
$action
;
/**
* 群名重命名的名称
*
* @var
*/
public
$rename
;
public
$rename
;
/**
* 被踢出群时的群信息
*
* @var
*/
public
$group
;
/**
* 新人进群的昵称(可能单个可能多个)
*
* @var
*/
public
$nickname
;
public
function
__construct
(
$msg
)
public
function
__construct
(
$msg
)
{
{
parent
::
__construct
(
$msg
);
parent
::
__construct
(
$msg
);
...
@@ -29,17 +48,25 @@ class GroupChange extends Message implements MessageInterface
...
@@ -29,17 +48,25 @@ class GroupChange extends Message implements MessageInterface
public
function
make
()
public
function
make
()
{
{
if
(
str_contains
(
$this
->
msg
[
'Content'
],
'加入了群聊'
)){
Console
::
debug
(
$this
->
msg
[
'Content'
]);
if
(
str_contains
(
$this
->
msg
[
'Content'
],
'邀请你'
))
{
$this
->
action
=
'INVITE'
;
}
elseif
(
str_contains
(
$this
->
msg
[
'Content'
],
'加入了群聊'
))
{
preg_match
(
'/.+"(.+)"加入了群聊/'
,
$this
->
msg
[
'Content'
],
$match
);
$this
->
action
=
'ADD'
;
$this
->
action
=
'ADD'
;
Console
::
log
(
"检测到
{
$this
->
from
[
'NickName'
]
}
有新成员,正在刷新群成员列表..."
);
$this
->
nickname
=
$match
[
1
];
Console
::
debug
(
"检测到
{
$this
->
from
[
'NickName'
]
}
有新成员,正在刷新群成员列表..."
);
(
new
ContactFactory
())
->
makeContactList
();
(
new
ContactFactory
())
->
makeContactList
();
Console
::
lo
g
(
'群成员更新成功!'
);
Console
::
debu
g
(
'群成员更新成功!'
);
}
elseif
(
str_contains
(
$this
->
msg
[
'Content'
],
'移出了群聊'
))
{
}
elseif
(
str_contains
(
$this
->
msg
[
'Content'
],
'移出了群聊'
))
{
$this
->
action
=
'REMOVE'
;
$this
->
action
=
'REMOVE'
;
}
elseif
(
str_contains
(
$this
->
msg
[
'Content'
],
'改群名为'
))
{
}
elseif
(
str_contains
(
$this
->
msg
[
'Content'
],
'改群名为'
))
{
$this
->
action
=
'RENAME'
;
$this
->
action
=
'RENAME'
;
preg_match
(
'/改群名为“(.+)”/'
,
$this
->
msg
[
'Content'
],
$match
);
preg_match
(
'/改群名为“(.+)”/'
,
$this
->
msg
[
'Content'
],
$match
);
$this
->
updateGroupName
(
$match
[
1
]);
$this
->
updateGroupName
(
$match
[
1
]);
}
elseif
(
str_contains
(
$this
->
msg
[
'Content'
],
'移出群聊'
))
{
$this
->
action
=
'BE_REMOVE'
;
$this
->
group
=
group
()
->
pull
(
$this
->
from
[
'UserName'
]);
}
}
$this
->
content
=
$this
->
msg
[
'Content'
];
$this
->
content
=
$this
->
msg
[
'Content'
];
...
...
src/Message/Entity/Image.php
View file @
b3bfcfa
...
@@ -42,7 +42,7 @@ class Image extends Message implements MessageInterface, MediaInterface
...
@@ -42,7 +42,7 @@ class Image extends Message implements MessageInterface, MediaInterface
$response
=
static
::
uploadMedia
(
$username
,
$file
);
$response
=
static
::
uploadMedia
(
$username
,
$file
);
if
(
!
$response
)
{
if
(
!
$response
)
{
Console
::
log
(
"文件
{
$file
}
上传失败"
);
Console
::
log
(
"文件
{
$file
}
上传失败"
,
Console
::
WARNING
);
return
false
;
return
false
;
}
}
...
@@ -63,7 +63,7 @@ class Image extends Message implements MessageInterface, MediaInterface
...
@@ -63,7 +63,7 @@ class Image extends Message implements MessageInterface, MediaInterface
$result
=
http
()
->
json
(
$url
,
$data
,
true
);
$result
=
http
()
->
json
(
$url
,
$data
,
true
);
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
)
{
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
)
{
Console
::
log
(
'发送图片失败'
);
Console
::
log
(
'发送图片失败'
,
Console
::
WARNING
);
return
false
;
return
false
;
}
}
...
...
src/Message/Entity/NewFriend.php
View file @
b3bfcfa
...
@@ -24,8 +24,8 @@ class NewFriend extends Message implements MessageInterface
...
@@ -24,8 +24,8 @@ class NewFriend extends Message implements MessageInterface
public
function
make
()
public
function
make
()
{
{
Console
::
lo
g
(
'检测到新加好友,正在刷新好友列表...'
);
Console
::
debu
g
(
'检测到新加好友,正在刷新好友列表...'
);
(
new
ContactFactory
())
->
makeContactList
();
(
new
ContactFactory
())
->
makeContactList
();
Console
::
lo
g
(
'好友更新成功!'
);
Console
::
debu
g
(
'好友更新成功!'
);
}
}
}
}
\ No newline at end of file
\ No newline at end of file
src/Message/Entity/Text.php
View file @
b3bfcfa
...
@@ -58,7 +58,7 @@ class Text extends Message implements MessageInterface
...
@@ -58,7 +58,7 @@ class Text extends Message implements MessageInterface
);
);
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
)
{
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
)
{
Console
::
log
(
'发送消息失败'
);
Console
::
log
(
'发送消息失败'
,
Console
::
WARNING
);
return
false
;
return
false
;
}
}
...
...
src/Message/Entity/Video.php
View file @
b3bfcfa
...
@@ -34,7 +34,7 @@ class Video extends Message implements MessageInterface, MediaInterface
...
@@ -34,7 +34,7 @@ class Video extends Message implements MessageInterface, MediaInterface
$response
=
static
::
uploadMedia
(
$username
,
$file
);
$response
=
static
::
uploadMedia
(
$username
,
$file
);
if
(
!
$response
)
{
if
(
!
$response
)
{
Console
::
log
(
"视频
{
$file
}
上传失败"
);
Console
::
log
(
"视频
{
$file
}
上传失败"
,
Console
::
WARNING
);
return
false
;
return
false
;
}
}
...
@@ -55,7 +55,7 @@ class Video extends Message implements MessageInterface, MediaInterface
...
@@ -55,7 +55,7 @@ class Video extends Message implements MessageInterface, MediaInterface
$result
=
http
()
->
json
(
$url
,
$data
,
true
);
$result
=
http
()
->
json
(
$url
,
$data
,
true
);
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
)
{
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
)
{
Console
::
log
(
'发送视频失败'
);
Console
::
log
(
'发送视频失败'
,
Console
::
WARNING
);
return
false
;
return
false
;
}
}
...
@@ -90,7 +90,8 @@ class Video extends Message implements MessageInterface, MediaInterface
...
@@ -90,7 +90,8 @@ class Video extends Message implements MessageInterface, MediaInterface
]
]
]);
]);
if
(
strlen
(
$content
)
===
0
){
if
(
strlen
(
$content
)
===
0
){
Console
::
log
(
'下载视频失败'
,
Console
::
ERROR
);
Console
::
log
(
'下载视频失败'
,
Console
::
WARNING
);
Console
::
log
(
'url:'
.
$url
);
}
else
{
}
else
{
FileManager
::
download
(
$this
->
msg
[
'MsgId'
]
.
'.mp4'
,
$content
,
static
::
$folder
);
FileManager
::
download
(
$this
->
msg
[
'MsgId'
]
.
'.mp4'
,
$content
,
static
::
$folder
);
}
}
...
...
src/Message/UploadAble.php
View file @
b3bfcfa
...
@@ -79,7 +79,7 @@ trait UploadAble
...
@@ -79,7 +79,7 @@ trait UploadAble
$response
=
static
::
uploadMedia
(
$username
,
$file
);
$response
=
static
::
uploadMedia
(
$username
,
$file
);
if
(
!
$response
){
if
(
!
$response
){
Console
::
log
(
"文件
{
$file
}
上传失败"
);
Console
::
log
(
"文件
{
$file
}
上传失败"
,
Console
::
WARNING
);
return
false
;
return
false
;
}
}
...
@@ -100,7 +100,7 @@ trait UploadAble
...
@@ -100,7 +100,7 @@ trait UploadAble
$result
=
http
()
->
json
(
$url
,
$data
,
true
);
$result
=
http
()
->
json
(
$url
,
$data
,
true
);
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
){
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
){
Console
::
log
(
'发送文件失败'
);
Console
::
log
(
'发送文件失败'
,
Console
::
WARNING
);
return
false
;
return
false
;
}
}
...
...
src/Support/Console.php
View file @
b3bfcfa
...
@@ -34,6 +34,18 @@ class Console
...
@@ -34,6 +34,18 @@ class Console
}
}
/**
/**
* debug 模式下调试输出
*
* @param $str
*/
public
static
function
debug
(
$str
)
{
if
(
server
()
->
config
[
'debug'
])
{
static
::
log
(
$str
,
'DEBUG'
);
}
}
/**
* 初始化二维码style
* 初始化二维码style
*
*
* @param OutputInterface $output
* @param OutputInterface $output
...
...
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