Commit a39fe166 by HanSon

增加Image类

1 parent e71f4e1c
<?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();
...@@ -18,7 +18,8 @@ $robot = new Robot([ ...@@ -18,7 +18,8 @@ $robot = new Robot([
$robot->server->setMessageHandler(function($message){ $robot->server->setMessageHandler(function($message){
/** @var $message 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){ if($message->type === 'Recall' && $message->rawMsg['FromUserName'] !== myself()->username){
Console::log($message->content); Console::log($message->content);
Message::send($message->content, $message->username); Message::send($message->content, $message->username);
......
...@@ -18,6 +18,8 @@ class Http ...@@ -18,6 +18,8 @@ class Http
protected $client; protected $client;
public $cookies;
/** /**
* @return Http * @return Http
*/ */
......
...@@ -209,7 +209,7 @@ class Server ...@@ -209,7 +209,7 @@ class Server
$this->deviceId = 'e' . strval(random_int(100000000000000, 999999999999999)); $this->deviceId = 'e' . strval(random_int(100000000000000, 999999999999999));
$this->baseRequest = [ $this->baseRequest = [
'Uin' => $this->uin, 'Uin' => intval($this->uin),
'Sid' => $this->sid, 'Sid' => $this->sid,
'Skey' => $this->skey, 'Skey' => $this->skey,
'DeviceID' => $this->deviceId 'DeviceID' => $this->deviceId
...@@ -220,7 +220,7 @@ class Server ...@@ -220,7 +220,7 @@ class Server
protected function init($first = true) 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, [ $content = http()->json($url, [
'BaseRequest' => $this->baseRequest 'BaseRequest' => $this->baseRequest
......
<?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 \ No newline at end of file
...@@ -75,7 +75,7 @@ class Message ...@@ -75,7 +75,7 @@ class Message
public $rawMsg; public $rawMsg;
private $mediaCount = -1; static $mediaCount = -1;
public function make($selector, $msg) public function make($selector, $msg)
{ {
...@@ -231,7 +231,7 @@ class Message ...@@ -231,7 +231,7 @@ class Message
*/ */
private function handleGroupContent($content) private function handleGroupContent($content)
{ {
if(!$content || $this->rawMsg['MsgType'] == 10002){ if(!$content && !str_contains($content, '<br/>')){
return; return;
} }
list($uid, $content) = explode('<br/>', $content, 2); list($uid, $content) = explode('<br/>', $content, 2);
...@@ -313,31 +313,4 @@ class Message ...@@ -313,31 +313,4 @@ class Message
return true; 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 \ No newline at end of file
...@@ -13,7 +13,7 @@ namespace Hanson\Robot\Message; ...@@ -13,7 +13,7 @@ namespace Hanson\Robot\Message;
* Class UploadAble * Class UploadAble
* @package Hanson\Robot\Message\ * @package Hanson\Robot\Message\
* *
* @property string $mediaCount * @property string static $mediaCount
*/ */
trait UploadAble trait UploadAble
{ {
...@@ -21,17 +21,18 @@ trait UploadAble ...@@ -21,17 +21,18 @@ trait UploadAble
/** /**
* @param $username * @param $username
* @param $file * @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'; $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)); $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, [ $data = [
'id' => 'WU_FILE_' .$this->mediaCount, 'id' => 'WU_FILE_' .static::$mediaCount,
'name' => $file, 'name' => $file,
'type' => $mime, 'type' => $mime,
'lastModifieDate' => $lastModifyDate, 'lastModifieDate' => $lastModifyDate,
...@@ -39,23 +40,34 @@ trait UploadAble ...@@ -39,23 +40,34 @@ trait UploadAble
'mediatype' => $mediaType, 'mediatype' => $mediaType,
'uploadmediarequest' => json_encode([ 'uploadmediarequest' => json_encode([
'BaseRequest' => server()->baseRequest, 'BaseRequest' => server()->baseRequest,
'ClientMediaId' => (time() * 1000).mt_rand(10000,99999), 'ClientMediaId' => time(),
'TotalLen' => filesize($file), 'TotalLen' => filesize($file),
'StartPos' => 0, 'StartPos' => 0,
'DataLen' => filesize($file), 'DataLen' => filesize($file),
'MediaType' => 4, 'MediaType' => 4,
'UploadType' => 2, // 'UploadType' => 2,
'FromUserName' => myself()->username, // 'FromUserName' => myself()->username,
'ToUserName' => $username, // 'ToUserName' => $username,
'FileMd5' => md5_file($file) // 'FileMd5' => md5_file($file)
],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), ], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
'webwx_data_ticket' => http()->getClient()->get, 'webwx_data_ticket' => static::getTicket(),
'pass_ticket' => $pass_ticket, 'pass_ticket' => server()->passTicket,
'filename' => '@'.$file_name '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); $info = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($info, $file); $mime = finfo_file($info, $file);
...@@ -63,4 +75,13 @@ trait UploadAble ...@@ -63,4 +75,13 @@ trait UploadAble
return [$mime, explode('/', $mime)[0] === 'image' ? 'pic' : 'doc']; 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 \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!