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 288f7b2a
authored
Jan 04, 2017
by
hanccc
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
增加example
1 parent
0871aa18
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
120 additions
and
20 deletions
src/Collections/Account.php
src/Core/Http.php
src/Core/MessageHandler.php
src/Core/Server.php
src/Message/Message.php
test/forward.php
test/from_type.php
src/Collections/Account.php
View file @
288f7b2
...
...
@@ -104,7 +104,7 @@ class Account extends Collection
* @param $id
* @return array
*/
public
function
getContact
(
$id
)
public
function
getContact
ByUsername
(
$id
)
{
$target
=
static
::
$instance
->
get
(
static
::
NORMAL_MEMBER
);
...
...
@@ -118,4 +118,51 @@ class Account extends Collection
return
$target
[
$id
]
??
null
;
}
/**
* 获取联系人列表
*
* @return Collection
*/
public
function
getNormalMembers
()
{
$target
=
static
::
$instance
->
get
(
static
::
NORMAL_MEMBER
);
return
collect
(
$target
);
}
/**
* 根据微信号获取联系人
*
* @param $id
* @return mixed
*/
public
function
getContactById
(
$id
)
{
$contact
=
$this
->
getNormalMembers
()
->
filter
(
function
(
$item
,
$key
)
use
(
$id
){
if
(
$item
[
'info'
][
'Alias'
]
===
$id
){
return
true
;
}
})
->
first
();
return
$contact
;
}
/**
* 根据微信号获取联系username
*
* @param $id
* @return mixed
*/
public
function
getUsernameById
(
$id
)
{
$contact
=
$this
->
getNormalMembers
()
->
search
(
function
(
$item
,
$key
)
use
(
$id
){
if
(
$item
[
'info'
][
'Alias'
]
===
$id
){
return
true
;
}
});
return
$contact
;
}
}
\ No newline at end of file
src/Core/Http.php
View file @
288f7b2
...
...
@@ -23,7 +23,6 @@ class Http
public
static
function
getInstance
()
{
if
(
!
static
::
$instance
){
echo
'http is null'
;
static
::
$instance
=
new
Http
();
}
...
...
src/Core/MessageHandler.php
View file @
288f7b2
...
...
@@ -21,6 +21,8 @@ class MessageHandler
private
$handler
;
private
$customHandler
;
static
$instance
=
null
;
/**
...
...
@@ -46,6 +48,15 @@ class MessageHandler
$this
->
handler
=
$closure
;
}
public
function
setCustomHandler
(
Closure
$closure
)
{
if
(
!
$closure
instanceof
Closure
){
throw
new
\Exception
(
'message handler must be a closure!'
);
}
$this
->
customHandler
=
$closure
;
}
/**
* listen the chat api
*/
...
...
@@ -54,6 +65,11 @@ class MessageHandler
$this
->
preCheckSync
();
while
(
true
){
if
(
$this
->
customHandler
){
call_user_func_array
(
$this
->
customHandler
,
[]);
}
$time
=
time
();
list
(
$retCode
,
$selector
)
=
$this
->
checkSync
();
...
...
src/Core/Server.php
View file @
288f7b2
...
...
@@ -292,6 +292,15 @@ class Server
MessageHandler
::
getInstance
()
->
setMessageHandler
(
$closure
);
}
public
function
setCustomerHandler
(
\Closure
$closure
)
{
if
(
!
is_callable
(
$closure
)){
throw
new
\Exception
(
'[ERROR] message handler must be a closure!'
);
}
MessageHandler
::
getInstance
()
->
setCustomHandler
(
$closure
);
}
public
function
debug
(
$debug
=
true
)
{
$this
->
debug
=
$debug
;
...
...
src/Message/Message.php
View file @
288f7b2
...
...
@@ -29,6 +29,8 @@ class Message
*/
public
$sender
;
public
$username
;
public
$to
;
public
$content
;
...
...
@@ -38,7 +40,7 @@ class Message
/**
* @var string 消息发送者类型
*/
public
$
F
romType
;
public
$
f
romType
;
/**
* @var string 消息类型
...
...
@@ -84,34 +86,35 @@ class Message
*/
private
function
setFrom
()
{
$this
->
from
=
Account
::
getInstance
()
->
getContact
(
$this
->
rawMsg
[
'FromUserName'
]);
$this
->
from
=
Account
::
getInstance
()
->
getContactByUsername
(
$this
->
rawMsg
[
'FromUserName'
]);
$this
->
username
=
$this
->
rawMsg
[
'FromUserName'
];
}
private
function
setTo
()
{
$this
->
to
=
Account
::
getInstance
()
->
getContact
(
$this
->
rawMsg
[
'ToUserName'
]);
$this
->
to
=
Account
::
getInstance
()
->
getContact
ByUsername
(
$this
->
rawMsg
[
'ToUserName'
]);
}
private
function
setFromType
()
{
if
(
$this
->
rawMsg
[
'MsgType'
]
==
51
)
{
$this
->
F
romType
=
'System'
;
$this
->
f
romType
=
'System'
;
}
elseif
(
$this
->
rawMsg
[
'MsgType'
]
==
37
)
{
$this
->
F
romType
=
'FriendRequest'
;
$this
->
f
romType
=
'FriendRequest'
;
}
elseif
(
$this
->
rawMsg
[
'FromUserName'
]
===
myself
()
->
userName
)
{
$this
->
F
romType
=
'Self'
;
$this
->
f
romType
=
'Self'
;
}
elseif
(
$this
->
rawMsg
[
'ToUserName'
]
===
'filehelper'
)
{
$this
->
F
romType
=
'FileHelper'
;
$this
->
f
romType
=
'FileHelper'
;
}
elseif
(
substr
(
$this
->
rawMsg
[
'FromUserName'
],
0
,
2
)
===
'@@'
)
{
# group
$this
->
F
romType
=
'Group'
;
$this
->
f
romType
=
'Group'
;
}
elseif
(
ContactAccount
::
getInstance
()
->
isContact
(
$this
->
rawMsg
[
'FromUserName'
]))
{
$this
->
F
romType
=
'Contact'
;
$this
->
f
romType
=
'Contact'
;
}
elseif
(
OfficialAccount
::
getInstance
()
->
isPublic
(
$this
->
rawMsg
[
'FromUserName'
]))
{
$this
->
F
romType
=
'Public'
;
$this
->
f
romType
=
'Public'
;
}
elseif
(
SpecialAccount
::
getInstance
()
->
get
(
$this
->
rawMsg
[
'FromUserName'
],
false
))
{
$this
->
F
romType
=
'Special'
;
$this
->
f
romType
=
'Special'
;
}
else
{
$this
->
F
romType
=
'Unknown'
;
$this
->
f
romType
=
'Unknown'
;
}
}
...
...
@@ -129,12 +132,12 @@ class Message
*/
private
function
setTypeByFrom
()
{
if
(
$this
->
F
romType
===
'System'
){
if
(
$this
->
f
romType
===
'System'
){
$this
->
type
=
'Empty'
;
}
elseif
(
$this
->
F
romType
===
'FileHelper'
){
# File Helper
}
elseif
(
$this
->
f
romType
===
'FileHelper'
){
# File Helper
$this
->
type
=
'Text'
;
$this
->
content
->
msg
=
$this
->
formatContent
(
$this
->
rawMsg
[
'Content'
]);
}
elseif
(
$this
->
F
romType
===
'Group'
){
}
elseif
(
$this
->
f
romType
===
'Group'
){
$this
->
handleGroupContent
(
$this
->
rawMsg
[
'Content'
]);
}
}
...
...
@@ -203,7 +206,7 @@ class Message
*/
private
function
setLocationMessage
()
{
$this
->
F
romType
=
'Location'
;
$this
->
f
romType
=
'Location'
;
// $this->url = $this->rawMsg['Url'];
$this
->
content
->
msg
=
Location
::
getLocationText
(
$this
->
rawMsg
[
'Content'
]);
}
...
...
test/forward.php
View file @
288f7b2
...
...
@@ -17,9 +17,11 @@ $client = new \GuzzleHttp\Client();
$robot
->
server
->
setMessageHandler
(
function
(
$message
)
use
(
$client
,
$robot
){
if
(
$message
->
type
===
'Text'
){
// print_r($message);
// echo $message->rawMsg['FromUserName'];
$contact
=
\Hanson\Robot\Collections\Account
::
getInstance
()
->
getUsernameById
(
'hanson1994'
);
\Hanson\Robot\Message\Message
::
send
(
'hi'
,
$message
->
rawMsg
[
'FromUserName'
]);
\Hanson\Robot\Message\Message
::
send
(
$message
->
content
,
$contact
);
}
});
$robot
->
server
->
setCustomerHandler
(
function
(){
});
$robot
->
server
->
run
();
test/from_type.php
0 → 100644
View file @
288f7b2
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2016/12/7
* Time: 16:33
*/
require_once
__DIR__
.
'./../vendor/autoload.php'
;
$robot
=
new
\Hanson\Robot\Foundation\Robot
([
'tmp'
=>
realpath
(
'./tmp'
)
.
'/'
,
'debug'
=>
true
,
]);
$client
=
new
\GuzzleHttp\Client
();
$robot
->
server
->
setMessageHandler
(
function
(
$message
)
use
(
$client
,
$robot
){
/** @var $message \Hanson\Robot\Message\Message */
if
(
$message
->
type
===
'Text'
){
\Hanson\Robot\Message\Message
::
send
(
$message
->
fromType
,
$message
->
username
);
}
});
$robot
->
server
->
run
();
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