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 a39fe166
authored
Jan 10, 2017
by
HanSon
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
增加Image类
1 parent
e71f4e1c
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
129 additions
and
49 deletions
example/image.php
example/recall.php
src/Core/Http.php
src/Core/Server.php
src/Message/Image.php
src/Message/Message.php
src/Message/UploadAble.php
example/image.php
0 → 100644
View file @
a39fe16
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2016/12/7
* Time: 16:33
*/
require_once
__DIR__
.
'./../vendor/autoload.php'
;
use
Hanson\Robot\Foundation\Robot
;
use
Hanson\Robot\Message\Message
;
use
Hanson\Robot\Message\Image
;
use
Hanson\Robot\Support\Console
;
$robot
=
new
Robot
([
'tmp'
=>
__DIR__
.
'/./../tmp/'
,
]);
$robot
->
server
->
setMessageHandler
(
function
(
$message
){
/** @var $message Message */
if
(
$message
->
content
===
'测试图片'
){
Image
::
send
(
$message
->
username
,
realpath
(
__DIR__
.
'/./../tmp/jpg/1483791274935140574833210401.jpg'
));
}
});
$robot
->
server
->
run
();
example/recall.php
View file @
a39fe16
...
...
@@ -18,7 +18,8 @@ $robot = new Robot([
$robot
->
server
->
setMessageHandler
(
function
(
$message
){
/** @var $message Message */
Console
::
log
(
'我的username'
.
myself
()
->
username
);
// 发送撤回消息 (排除自己)
print_r
(
http
()
->
getClient
()
->
getConfig
(
'cookies'
)
->
toArray
());
if
(
$message
->
type
===
'Recall'
&&
$message
->
rawMsg
[
'FromUserName'
]
!==
myself
()
->
username
){
Console
::
log
(
$message
->
content
);
Message
::
send
(
$message
->
content
,
$message
->
username
);
...
...
src/Core/Http.php
View file @
a39fe16
...
...
@@ -18,6 +18,8 @@ class Http
protected
$client
;
public
$cookies
;
/**
* @return Http
*/
...
...
src/Core/Server.php
View file @
a39fe16
...
...
@@ -209,7 +209,7 @@ class Server
$this
->
deviceId
=
'e'
.
strval
(
random_int
(
100000000000000
,
999999999999999
));
$this
->
baseRequest
=
[
'Uin'
=>
$this
->
uin
,
'Uin'
=>
intval
(
$this
->
uin
)
,
'Sid'
=>
$this
->
sid
,
'Skey'
=>
$this
->
skey
,
'DeviceID'
=>
$this
->
deviceId
...
...
@@ -220,7 +220,7 @@ class Server
protected
function
init
(
$first
=
true
)
{
$url
=
sprintf
(
self
::
BASE_URI
.
'/webwxinit?r=%i&lang=en_US&pass_ticket=%s
'
,
time
(),
$this
->
passTicket
);
$url
=
sprintf
(
self
::
BASE_URI
.
'/webwxinit?r=%i&lang=en_US&pass_ticket=%s
&skey=%s'
,
time
(),
$this
->
passTicket
,
$this
->
skey
);
$content
=
http
()
->
json
(
$url
,
[
'BaseRequest'
=>
$this
->
baseRequest
...
...
src/Message/Image.php
0 → 100644
View file @
a39fe16
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2017/1/10
* Time: 16:51
*/
namespace
Hanson\Robot\Message
;
use
Hanson\Robot\Core\Server
;
use
Hanson\Robot\Support\Console
;
class
Image
extends
Message
{
use
UploadAble
;
public
static
function
send
(
$username
,
$file
)
{
$response
=
static
::
uploadMedia
(
$username
,
$file
);
if
(
!
$response
){
Console
::
log
(
"文件
{
$file
}
发送失败"
);
return
false
;
}
$mediaId
=
$response
[
'MediaId'
];
$url
=
sprintf
(
Server
::
BASE_URI
.
'/webwxsendmsgimg?fun=async&f=json&pass_ticket=%s'
,
server
()
->
passTicket
);
$clientMsgId
=
(
time
()
*
1000
)
.
substr
(
uniqid
(),
0
,
5
);
$data
=
[
'BaseRequest'
=>
server
()
->
baseRequest
,
'Msg'
=>
[
'Type'
=>
3
,
'MediaId'
=>
$mediaId
,
'FromUserName'
=>
myself
()
->
username
,
'ToUserName'
=>
$username
,
'LocalID'
=>
$clientMsgId
,
'ClientMsgId'
=>
$clientMsgId
]
];
$result
=
http
()
->
post
(
$url
,
json_encode
(
$data
,
JSON_UNESCAPED_UNICODE
),
true
);
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
){
Console
::
log
(
'发送图片失败'
);
return
false
;
}
return
true
;
}
}
\ No newline at end of file
src/Message/Message.php
View file @
a39fe16
...
...
@@ -75,7 +75,7 @@ class Message
public
$rawMsg
;
private
$mediaCount
=
-
1
;
static
$mediaCount
=
-
1
;
public
function
make
(
$selector
,
$msg
)
{
...
...
@@ -231,7 +231,7 @@ class Message
*/
private
function
handleGroupContent
(
$content
)
{
if
(
!
$content
||
$this
->
rawMsg
[
'MsgType'
]
==
10002
){
if
(
!
$content
&&
!
str_contains
(
$content
,
'<br/>'
)
){
return
;
}
list
(
$uid
,
$content
)
=
explode
(
'<br/>'
,
$content
,
2
);
...
...
@@ -313,31 +313,4 @@ class Message
return
true
;
}
public
static
function
sendImg
(
$username
,
$mediaId
)
{
$url
=
sprintf
(
Server
::
BASE_URI
.
'/webwxsendmsgimg?fun=async&f=json&pass_ticket=%s'
,
server
()
->
passTicket
);
$clientMsgId
=
(
time
()
*
1000
)
.
substr
(
uniqid
(),
0
,
5
);
$data
=
[
'BaseRequest'
=>
server
()
->
baseRequest
,
'Msg'
=>
[
'Type'
=>
3
,
'MediaId'
=>
$mediaId
,
'FromUserName'
=>
myself
()
->
username
,
'ToUserName'
=>
$username
,
'LocalID'
=>
$clientMsgId
,
'ClientMsgId'
=>
$clientMsgId
]
];
$result
=
http
()
->
post
(
$url
,
json_encode
(
$data
,
JSON_UNESCAPED_UNICODE
),
true
);
if
(
$result
[
'BaseResponse'
][
'Ret'
]
!=
0
){
Console
::
log
(
'发送图片失败'
);
return
false
;
}
return
true
;
}
}
\ No newline at end of file
src/Message/UploadAble.php
View file @
a39fe16
...
...
@@ -13,7 +13,7 @@ namespace Hanson\Robot\Message;
* Class UploadAble
* @package Hanson\Robot\Message\
*
* @property
string
$mediaCount
* @property
string static
$mediaCount
*/
trait
UploadAble
{
...
...
@@ -21,17 +21,18 @@ trait UploadAble
/**
* @param $username
* @param $file
* @return bool|mixed|string
*/
public
function
uploadMedia
(
$username
,
$file
)
public
static
function
uploadMedia
(
$username
,
$file
)
{
$url
=
'https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json'
;
$this
->
mediaCount
=
++
$this
->
mediaCount
;
static
::
$mediaCount
=
++
static
::
$
mediaCount
;
$lastModifyDate
=
gmdate
(
'D M d Y H:i:s TO'
,
filemtime
(
$file
));
list
(
$mime
,
$mediaType
)
=
$this
->
getMediaType
(
$file
);
list
(
$mime
,
$mediaType
)
=
static
::
getMediaType
(
$file
);
http
()
->
post
(
$url
,
[
'id'
=>
'WU_FILE_'
.
$this
->
mediaCount
,
$data
=
[
'id'
=>
'WU_FILE_'
.
static
::
$
mediaCount
,
'name'
=>
$file
,
'type'
=>
$mime
,
'lastModifieDate'
=>
$lastModifyDate
,
...
...
@@ -39,23 +40,34 @@ trait UploadAble
'mediatype'
=>
$mediaType
,
'uploadmediarequest'
=>
json_encode
([
'BaseRequest'
=>
server
()
->
baseRequest
,
'ClientMediaId'
=>
(
time
()
*
1000
)
.
mt_rand
(
10000
,
99999
),
'ClientMediaId'
=>
time
(
),
'TotalLen'
=>
filesize
(
$file
),
'StartPos'
=>
0
,
'DataLen'
=>
filesize
(
$file
),
'MediaType'
=>
4
,
'UploadType'
=>
2
,
'FromUserName'
=>
myself
()
->
username
,
'ToUserName'
=>
$username
,
'FileMd5'
=>
md5_file
(
$file
)
],
JSON_UNESCAPED_UNICODE
|
JSON_UNESCAPED_SLASHES
),
'webwx_data_ticket'
=>
http
()
->
getClient
()
->
get
,
'pass_ticket'
=>
$pass_ticket
,
'filename'
=>
'@'
.
$file_name
]);
// 'UploadType' => 2,
// 'FromUserName' => myself()->username,
// 'ToUserName' => $username,
// 'FileMd5' => md5_file($file)
],
JSON_UNESCAPED_UNICODE
|
JSON_UNESCAPED_SLASHES
),
'webwx_data_ticket'
=>
static
::
getTicket
(),
'pass_ticket'
=>
server
()
->
passTicket
,
'filename'
=>
'@'
.
$file
];
$result
=
http
()
->
post
(
$url
,
$data
,
true
);
print_r
(
$data
);
print_r
(
$result
);
if
(
$result
[
'BaseResponse'
][
'Ret'
]
==
0
){
return
$result
;
}
return
false
;
}
private
function
getMediaType
(
$file
)
private
static
function
getMediaType
(
$file
)
{
$info
=
finfo_open
(
FILEINFO_MIME_TYPE
);
$mime
=
finfo_file
(
$info
,
$file
);
...
...
@@ -63,4 +75,13 @@ trait UploadAble
return
[
$mime
,
explode
(
'/'
,
$mime
)[
0
]
===
'image'
?
'pic'
:
'doc'
];
}
private
static
function
getTicket
()
{
$cookies
=
http
()
->
getClient
()
->
getConfig
(
'cookies'
)
->
toArray
();
$key
=
array_search
(
'webwx_data_ticket'
,
array_column
(
$cookies
,
'Name'
));
return
$cookies
[
$key
][
'Value'
];
}
}
\ 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