Commit 46ac3d11 by HanSon

完成发送图片,修改撤回

1 parent c532cc2b
......@@ -304,7 +304,7 @@ $robot->server->setCustomHandler(function(){
- [ ] 命令行操作信息发送
- [ ] 消息抽象成每个类
- [ ] 抽象消息
- [ ] 群操作
- [ ] 创建群
......@@ -327,6 +327,11 @@ $robot->server->setCustomHandler(function(){
- [ ] 发送图片
- [ ] 发送表情
- [ ] 消息存储
- [x] 语音
- [x] 图片
- [ ] 视频
- [ ] 消息处理
- [x] 文字
- [x] 图片
......
......@@ -20,7 +20,12 @@ $robot = new Robot([
$robot->server->setMessageHandler(function($message){
/** @var $message Message */
if($message->content === '测试图片'){
// https://wx2.qq.com/cgi-bin/mmwebwx-bin
// 自己发就发送给自己
Image::send($message->username, realpath(__DIR__ . '/./../tmp/jpg/1547651860337387181.jpg'));
}
if($message->type === 'Recall' && $message->rawMsg['FromUserName'] !== myself()->username){
Console::log($message->content);
Message::send($message->content, $message->username);
Image::send($message->username, realpath(__DIR__ . '/./../tmp/jpg/1547651860337387181.jpg'));
}
......
......@@ -19,10 +19,16 @@ $robot = new Robot([
$robot->server->setMessageHandler(function($message){
/** @var $message Message */
// 发送撤回消息 (排除自己)
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);
if($message->type === 'Recall' && $message->rawMsg['FromUserName'] !== myself()->username ){
// Message::send($message->content, $message->username);
$message = message()->get($message->msgId);
$nickname = $message['sender'] ? $message['sender']['NickName'] : account()->getAccount($message['username'])['NickName'];
$content = "{$nickname} 刚撤回了消息 " . $message['type'] === 'Text' ? "\"{$message['content']}\"" : null;
if($message['type'] === 'Image'){
\Hanson\Robot\Message\Image::send($message->username, realpath(__DIR__ . "/./../tmp/jpg/{$message->msgId}.jpg"));
}else{
return $content;
}
}
});
......
......@@ -111,8 +111,9 @@ class Server
$content = http()->get('https://login.weixin.qq.com/jslogin', [
'appid' => 'wx782c26e4c19acffb',
'fun' => 'new',
// 'lang' => 'zh_CN',
'lang' => 'zh_CN',
// '_' => time() * 1000 . random_int(1, 999)
'_' => time()
]);
preg_match('/window.QRLogin.code = (\d+); window.QRLogin.uuid = \"(\S+?)\"/', $content, $matches);
......
......@@ -29,7 +29,6 @@ class Image extends Message
$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'=> [
......@@ -37,13 +36,13 @@ class Image extends Message
'MediaId'=> $mediaId,
'FromUserName'=> myself()->username,
'ToUserName'=> $username,
'LocalID'=> $clientMsgId,
'ClientMsgId'=> $clientMsgId
'LocalID'=> time() * 1e4,
'ClientMsgId'=> time() * 1e4
]
];
$result = http()->post($url,
json_encode($data, JSON_UNESCAPED_UNICODE), true
);
$result = http()->json($url, $data, true);
print_r($result);
if($result['BaseResponse']['Ret'] != 0){
Console::log('发送图片失败');
......
......@@ -96,13 +96,14 @@ class Message
*/
private function setFrom()
{
$this->from = contact()->getContactByUsername($this->rawMsg['FromUserName']);
$this->from = $this->toObject(account()->getAccount($this->rawMsg['FromUserName']));
// $this->from = contact()->getContactByUsername($this->rawMsg['FromUserName']);
$this->username = $this->rawMsg['FromUserName'];
}
private function setTo()
{
$this->to = contact()->getContactByUsername($this->rawMsg['ToUserName']);
$this->to = $this->toObject(contact()->getContactByUsername($this->rawMsg['ToUserName']));
}
private function setFromType()
......@@ -213,10 +214,9 @@ class Message
break;
case 10002:
$this->type = 'Recall'; // 撤回
$msgId = $this->parseMsgId($this->rawMsg['Content']);
$message = message()->get($msgId);
$nickname = $message['sender'] ? $message['sender']['NickName'] : account()->getAccount($message['username'])['NickName'];
$this->content = "{$nickname} 刚撤回了消息 \"{$message['content']}\"";
$this->msgId = $msgId = $this->parseMsgId($this->rawMsg['Content']);
// if($message['type'] === )
break;
default:
$this->type = 'Unknown';
......@@ -231,7 +231,7 @@ class Message
*/
private function handleGroupContent($content)
{
if(!$content && !str_contains($content, '<br/>')){
if(!$content || !str_contains($content, '<br/>')){
return;
}
list($uid, $content) = explode('<br/>', $content, 2);
......@@ -313,4 +313,9 @@ class Message
return true;
}
private function toObject(Array $array)
{
return json_decode(json_encode($array));
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2017/1/13
* Time: 15:48
*/
namespace Hanson\Robot\Message;
class Recall extends Message
{
public $raw;
/**
* @var string 上一条撤回的msgId
*/
public $msgId;
public $msg;
public $content;
public function __construct(array $msg)
{
$this->raw = $msg;
$msgId = $this->parseMsgId($this->rawMsg['Content']);
$this->msg = $message = message()->get($msgId);
$nickname = $message['sender'] ? $message['sender']['NickName'] : account()->getAccount($message['username'])['NickName'];
$this->content = "{$nickname} 刚撤回了消息 " . $message['type'] === 'Text' ? "\"{$message['content']}\"" : null;
}
/**
* 解析message获取msgId
*
* @param $xml
* @return string msgId
*/
private function parseMsgId($xml)
{
preg_match('/<msgid>(\d+)<\/msgid>/', $xml, $matches);
return $matches[1];
}
}
\ No newline at end of file
......@@ -37,7 +37,6 @@ trait UploadAble
'id' => 'WU_FILE_' .static::$mediaCount,
'name' => basename($file),
'type' => $mime,
// 'lastModifieDate' => gmdate('D M d Y H:i:s', filemtime($file) ).' GMT+0800 (CST)',
'lastModifieDate' => gmdate('D M d Y H:i:s TO', filemtime($file)).' (CST)',
'size' => filesize($file),
'mediatype' => $mediaType,
......@@ -55,24 +54,16 @@ trait UploadAble
], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
'webwx_data_ticket' => static::getTicket(),
'pass_ticket' => (server()->passTicket),
// 'filename' => "@{$file};filename=".basename($file),
// 'filename' => file_get_contents($file),
'filename' => fopen($file, 'r'),
// 'filename' => $file
// 'filename' => curl_file_create($file, $mime, basename($file))
];
$data = static::dataToMultipart($data);
// $result = http()->post($url, $data, true);
$result = http()->request($url, 'post', [
'multipart' => $data
]);
$result = json_decode($result, true);
print_r($data);
print_r($result);
if($result['BaseResponse']['Ret'] == 0){
return $result;
}
......@@ -109,7 +100,6 @@ trait UploadAble
];
if($key === 'filename'){
$field['filename'] = basename(static::$file);
// $field['Content-type'] = 'application/octet-stream';
}
$result[] = $field;
}
......
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2017/1/13
* Time: 17:23
*/
namespace Hanson\Robot\Support;
trait ObjectAble
{
public function toObject(Array $array)
{
return json_decode(json_encode($array));
}
}
\ 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!