Commit 97dbd725 by HanSon

重新定制格式

1 parent a9347535
......@@ -34,28 +34,33 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
/** @var $message Message */
print_r($message);
// 位置信息 返回位置文字
if ($message instanceof Location) {
return $message;
if($message){
print_r($message->msg);
}
// 文字信息
if ($message instanceof Text) {
// 联系人自动回复
if ($message->fromType === 'Contact') {
// 位置信息 返回位置文字
// if ($message instanceof Location) {
// Text::send('地图链接:'.$message->from['UserName'], $message->url);
// return '位置:'.$message;
// }
return http()->post('http://www.tuling123.com/openapi/api', [
'key' => '1dce02aef026258eff69635a06b0ab7d',
'info' => $message->content
], true)['text'];
// 群组@我回复
} elseif ($message->fromType === 'Group' && $message->isAt) {
return http()->post('http://www.tuling123.com/openapi/api', [
'key' => '1dce02aef026258eff69635a06b0ab7d',
'info' => $message->content
], true)['text'];
}
}
// 文字信息
// if ($message instanceof Text) {
// // 联系人自动回复
// if ($message->fromType === 'Contact') {
//
// return http()->post('http://www.tuling123.com/openapi/api', [
// 'key' => '1dce02aef026258eff69635a06b0ab7d',
// 'info' => $message->content
// ], true)['text'];
// // 群组@我回复
// } elseif ($message->fromType === 'Group' && $message->isAt) {
// return http()->post('http://www.tuling123.com/openapi/api', [
// 'key' => '1dce02aef026258eff69635a06b0ab7d',
// 'info' => $message->content
// ], true)['text'];
// }
// }
// 图片信息 返回接收到的图片
// if ($message instanceof Image) {
......@@ -79,78 +84,78 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
// }
// 撤回信息
if ($message instanceof Recall && $message->msg['FromUserName'] !== myself()->username) {
/** @var $message Recall */
if($message->origin instanceof Image){
Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一张照片");
Image::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']);
}elseif($message->origin instanceof Emoticon){
Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一个表情");
Emoticon::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']);
}elseif($message->origin instanceof Video){
Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一个视频");
Video::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']);
}elseif($message->origin instanceof Voice){
Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一条语音");
}else{
Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一条信息 \"{$message->origin->msg['Content']}\"");
}
// return $message;
}
// 红包信息
if($message instanceof RedPacket){
// do something to notify if you want ...
return $message->content . ' 来自 ' .$message->from['NickName'];
}
// 转账信息
if($message instanceof Transfer){
/** @var $message Transfer */
return $message->content . ' 收到金额 ' . $message->fee;
}
// 推荐名片信息
if($message instanceof Recommend){
/** @var $message Recommend */
if($message->isOfficial){
return $message->from['NickName'] . ' 向你推荐了公众号 ' . $message->province . $message->city .
" {$message->info['NickName']} 公众号信息: {$message->description}";
}else{
return $message->from['NickName'] . ' 向你推荐了 ' . $message->province . $message->city .
" {$message->info['NickName']} 头像链接: {$message->bigAvatar}";
}
}
// 请求添加信息
if($message instanceof RequestFriend){
/** @var $message RequestFriend */
$groupUsername = group()->getGroupsByNickname('芬芬', true)->first()['UserName'];
Text::send($groupUsername, "{$message->province}{$message->city}{$message->info['NickName']} 请求添加好友 \"{$message->info['Content']}\"");
if($message->info['Content'] === '上山打老虎'){
Text::send($groupUsername, '暗号正确');
$message->verifyUser($message::VIA);
}else{
Text::send($groupUsername, '暗号错误');
}
}
// 分享信息
if($message instanceof Share){
/** @var $message Share */
$reply = "收到分享\n标题:{$message->title}\n描述:{$message->description}\n链接:{$message->url}";
if($message->app){
$reply .= "\n来源APP:{$message->app}";
}
return $reply;
}
// 手机点击聊天事件
if($message instanceof Touch){
Text::send($message->to['UserName'], "我点击了此群");
}
// if ($message instanceof Recall && $message->msg['FromUserName'] !== myself()->username) {
// /** @var $message Recall */
// if($message->origin instanceof Image){
// Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一张照片");
// Image::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']);
// }elseif($message->origin instanceof Emoticon){
// Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一个表情");
// Emoticon::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']);
// }elseif($message->origin instanceof Video){
// Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一个视频");
// Video::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']);
// }elseif($message->origin instanceof Voice){
// Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一条语音");
// }else{
// Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一条信息 \"{$message->origin->msg['Content']}\"");
// }
// }
//
// // 红包信息
// if($message instanceof RedPacket){
// // do something to notify if you want ...
// return $message->content . ' 来自 ' .$message->from['NickName'];
// }
//
// // 转账信息
// if($message instanceof Transfer){
// /** @var $message Transfer */
// return $message->content . ' 收到金额 ' . $message->fee;
// }
//
// // 推荐名片信息
// if($message instanceof Recommend){
// /** @var $message Recommend */
// if($message->isOfficial){
// return $message->from['NickName'] . ' 向你推荐了公众号 ' . $message->province . $message->city .
// " {$message->info['NickName']} 公众号信息: {$message->description}";
// }else{
// return $message->from['NickName'] . ' 向你推荐了 ' . $message->province . $message->city .
// " {$message->info['NickName']} 头像链接: {$message->bigAvatar}";
// }
// }
//
// // 请求添加信息
// if($message instanceof RequestFriend){
// /** @var $message RequestFriend */
// $groupUsername = group()->getGroupsByNickname('芬芬', true)->first()['UserName'];
//
// Text::send($groupUsername, "{$message->province}{$message->city} 的 {$message->info['NickName']} 请求添加好友 \"{$message->info['Content']}\"");
//
// if($message->info['Content'] === '上山打老虎'){
// Text::send($groupUsername, '暗号正确');
// $message->verifyUser($message::VIA);
// }else{
// Text::send($groupUsername, '暗号错误');
// }
// }
//
// // 分享信息
// if($message instanceof Share){
// /** @var $message Share */
// $reply = "收到分享\n标题:{$message->title}\n描述:{$message->description}\n链接:{$message->url}";
// if($message->app){
// $reply .= "\n来源APP:{$message->app}";
// }
// return $reply;
// }
//
// // 手机点击聊天事件
// if($message instanceof Touch){
// print_r($message);
// Text::send($message->to['UserName'], "我点击了此群");
// }
return false;
......
......@@ -53,9 +53,7 @@ class Account
*/
public function getAccount($username)
{
$account = $username === myself()->username ? myself() : null;
$account = $account ? : static::$group->get($username, null);
$account = static::$group->get($username, null);
$account = $account ? : static::$contact->get($username, null);
......
......@@ -39,7 +39,6 @@ class MessageFactory
*/
private function handleMessageByType($msg)
{
Console::log($msg['MsgType']);
switch($msg['MsgType']){
case 1: //文本消息
if(Location::isLocation($msg)){
......
......@@ -125,8 +125,6 @@ class MessageHandler
$content = $this->messageFactory->make($selector, $msg);
if($content){
$this->addToMessageCollection($content);
}
if($this->handler instanceof Closure){
$reply = call_user_func_array($this->handler, [$content]);
if($reply){
if($reply instanceof Image){
......
......@@ -9,6 +9,8 @@
namespace Hanson\Vbot\Core;
use Hanson\Vbot\Collections\Contact;
class Myself
{
......@@ -33,6 +35,7 @@ class Myself
public function init($user)
{
contact()->put($user['UserName'], $user);
$this->nickname = $user['NickName'];
$this->username = $user['UserName'];
$this->sex = $user['Sex'];
......
......@@ -34,29 +34,29 @@ class Emoticon extends Message implements MediaInterface, MessageInterface
{
$response = static::uploadMedia($username, $file);
if(!$response){
if (!$response) {
Console::log("表情 {$file} 上传失败");
return false;
}
$mediaId = $response['MediaId'];
$url = sprintf(server()->baseUri . '/webwxsendemoticon?fun=sys&f=json&pass_ticket=%s' , server()->passTicket);
$url = sprintf(server()->baseUri . '/webwxsendemoticon?fun=sys&f=json&pass_ticket=%s', server()->passTicket);
$data = [
'BaseRequest'=> server()->baseRequest,
'Msg'=> [
'Type'=> 47,
"EmojiFlag"=> 2,
'MediaId'=> $mediaId,
'FromUserName'=> myself()->username,
'ToUserName'=> $username,
'LocalID'=> time() * 1e4,
'ClientMsgId'=> time() * 1e4
'BaseRequest' => server()->baseRequest,
'Msg' => [
'Type' => 47,
"EmojiFlag" => 2,
'MediaId' => $mediaId,
'FromUserName' => myself()->username,
'ToUserName' => $username,
'LocalID' => time() * 1e4,
'ClientMsgId' => time() * 1e4
]
];
$result = http()->json($url, $data, true);
if($result['BaseResponse']['Ret'] != 0){
if ($result['BaseResponse']['Ret'] != 0) {
Console::log('发送表情失败');
return false;
}
......@@ -87,7 +87,7 @@ class Emoticon extends Message implements MediaInterface, MessageInterface
{
$url = server()->baseUri . sprintf('/webwxgetmsgimg?MsgID=%s&skey=%s', $this->msg['MsgId'], server()->skey);
$content = http()->get($url);
FileManager::download($this->msg['MsgId'].'.gif', $content, static::$folder);
FileManager::download($this->msg['MsgId'] . '.gif', $content, static::$folder);
}
public function make()
......
......@@ -41,28 +41,28 @@ class Image extends Message implements MessageInterface, MediaInterface
{
$response = static::uploadMedia($username, $file);
if(!$response){
if (!$response) {
Console::log("文件 {$file} 上传失败");
return false;
}
$mediaId = $response['MediaId'];
$url = sprintf(server()->baseUri . '/webwxsendmsgimg?fun=async&f=json&pass_ticket=%s' , server()->passTicket);
$url = sprintf(server()->baseUri . '/webwxsendmsgimg?fun=async&f=json&pass_ticket=%s', server()->passTicket);
$data = [
'BaseRequest'=> server()->baseRequest,
'Msg'=> [
'Type'=> 3,
'MediaId'=> $mediaId,
'FromUserName'=> myself()->username,
'ToUserName'=> $username,
'LocalID'=> time() * 1e4,
'ClientMsgId'=> time() * 1e4
'BaseRequest' => server()->baseRequest,
'Msg' => [
'Type' => 3,
'MediaId' => $mediaId,
'FromUserName' => myself()->username,
'ToUserName' => $username,
'LocalID' => time() * 1e4,
'ClientMsgId' => time() * 1e4
]
];
$result = http()->json($url, $data, true);
if($result['BaseResponse']['Ret'] != 0){
if ($result['BaseResponse']['Ret'] != 0) {
Console::log('发送图片失败');
return false;
}
......@@ -79,6 +79,6 @@ class Image extends Message implements MessageInterface, MediaInterface
{
$url = server()->baseUri . sprintf('/webwxgetmsgimg?MsgID=%s&skey=%s', $this->msg['MsgId'], server()->skey);
$content = http()->get($url);
FileManager::download($this->msg['MsgId'].'.jpg', $content, static::$folder);
FileManager::download($this->msg['MsgId'] . '.jpg', $content, static::$folder);
}
}
\ No newline at end of file
......@@ -78,12 +78,8 @@ class Message
{
if ($this->msg['MsgType'] == 51) {
$this->fromType = 'System';
} elseif ($this->msg['MsgType'] == 37) {
$this->fromType = 'FriendRequest';
} elseif ($this->msg['FromUserName'] === myself()->username) {
$this->fromType = 'Self';
} elseif ($this->msg['ToUserName'] === 'filehelper') {
$this->fromType = 'FileHelper';
} elseif (substr($this->msg['FromUserName'], 0, 2) === '@@') { # group
$this->fromType = 'Group';
} elseif (contact()->getContactByUsername($this->msg['FromUserName'])) {
......
......@@ -33,7 +33,7 @@ class Text extends Message implements MessageInterface
*/
public static function send($username, string $word)
{
if(!$word){
if (!$word) {
return false;
}
......@@ -52,10 +52,10 @@ class Text extends Message implements MessageInterface
'Scene' => 0
];
$result = http()->post(server()->baseUri . '/webwxsendmsg?pass_ticket=' . server()->passTicket,
json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), true
json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), true
);
if($result['BaseResponse']['Ret'] != 0){
if ($result['BaseResponse']['Ret'] != 0) {
Console::log('发送消息失败');
return false;
}
......@@ -67,6 +67,6 @@ class Text extends Message implements MessageInterface
{
$this->content = $this->msg['Content'];
$this->isAt = str_contains($this->content, '@'.myself()->nickname);
$this->isAt = str_contains($this->content, '@' . myself()->nickname);
}
}
\ No newline at end of file
......@@ -33,28 +33,28 @@ class Video extends Message implements MessageInterface, MediaInterface
{
$response = static::uploadMedia($username, $file);
if(!$response){
if (!$response) {
Console::log("视频 {$file} 上传失败");
return false;
}
$mediaId = $response['MediaId'];
$url = sprintf(server()->baseUri . '/webwxsendvideomsg?fun=async&f=json&pass_ticket=%s' , server()->passTicket);
$url = sprintf(server()->baseUri . '/webwxsendvideomsg?fun=async&f=json&pass_ticket=%s', server()->passTicket);
$data = [
'BaseRequest'=> server()->baseRequest,
'Msg'=> [
'Type'=> 43,
'MediaId'=> $mediaId,
'FromUserName'=> myself()->username,
'ToUserName'=> $username,
'LocalID'=> time() * 1e4,
'ClientMsgId'=> time() * 1e4
'BaseRequest' => server()->baseRequest,
'Msg' => [
'Type' => 43,
'MediaId' => $mediaId,
'FromUserName' => myself()->username,
'ToUserName' => $username,
'LocalID' => time() * 1e4,
'ClientMsgId' => time() * 1e4
]
];
$result = http()->json($url, $data, true);
if($result['BaseResponse']['Ret'] != 0){
if ($result['BaseResponse']['Ret'] != 0) {
Console::log('发送视频失败');
return false;
}
......@@ -89,7 +89,7 @@ class Video extends Message implements MessageInterface, MediaInterface
'Range' => 'bytes=0-'
]
]);
FileManager::download($this->msg['MsgId'].'.mp4', $content, static::$folder);
FileManager::download($this->msg['MsgId'] . '.mp4', $content, static::$folder);
}
public function make()
......
......@@ -29,17 +29,6 @@ class Voice extends Message implements MessageInterface, MediaInterface
}
/**
* 根据MsgID发送文件
*
* @param $username
* @param $msgId
* @return mixed
*/
public static function sendByMsgId($username, $msgId)
{
}
/**
* 下载文件
*
* @return mixed
......
......@@ -13,15 +13,6 @@ interface MediaInterface
{
/**
* 根据MsgID发送文件
*
* @param $username
* @param $msgId
* @return mixed
*/
public static function sendByMsgId($username, $msgId);
/**
* 下载文件
*
* @return mixed
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!