Commit 6e6fc3c5 by HanSon

增加了消息类型 “新增好友” 以及 “群组变动”

修复了请求好友获取头像bug
修复了撤回消息bug
1 parent 560148d1
......@@ -100,6 +100,8 @@ $robot->server->run();
- [x] 好友验证
- [x] 分享
- [x] 公众号推送
- [x] 新好友
- [x] 群变动(增加成员,移除成员,更改群名)
- [x] 小程序
- [x] 消息存储
......
......@@ -25,6 +25,8 @@ use Hanson\Vbot\Message\Entity\Official;
use Hanson\Vbot\Message\Entity\Touch;
use Hanson\Vbot\Message\Entity\Mina;
use Hanson\Vbot\Message\Entity\RequestFriend;
use Hanson\Vbot\Message\Entity\GroupChange;
use Hanson\Vbot\Message\Entity\NewFriend;
$path = __DIR__ . '/./../tmp/';
$robot = new Vbot([
......@@ -171,6 +173,26 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
Text::send($message->msg['ToUserName'], "我点击了此聊天");
}
// 新增好友
if($message instanceof \Hanson\Vbot\Message\Entity\NewFriend){
\Hanson\Vbot\Support\Console::log('新加好友:' . $message->from['NickName']);
}
// 群组变动
if($message instanceof GroupChange){
/** @var $message GroupChange */
if($message->action === 'ADD'){
\Hanson\Vbot\Support\Console::log('新人进群');
return $message->content;
}elseif($message->action === 'REMOVE'){
\Hanson\Vbot\Support\Console::log('群主踢人了');
return $message->content;
}elseif($message->action === 'RENAME'){
\Hanson\Vbot\Support\Console::log($message->from['NickName'].' 改名为 '.$message->rename);
return $message->content;
}
}
return false;
});
......
......@@ -30,10 +30,8 @@ class ContactFactory
public function getContacts()
{
$this->makeContactList();
$contact = contact()->get(myself()->username);
myself()->alias = isset($contact['Alias']) ? $contact['Alias'] : myself()->nickname ? : myself()->username;
......
......@@ -13,6 +13,8 @@ use Hanson\Vbot\Message\Entity\Emoticon;
use Hanson\Vbot\Message\Entity\Image;
use Hanson\Vbot\Message\Entity\Location;
use Hanson\Vbot\Message\Entity\Message;
use Hanson\Vbot\Message\Entity\NewFriend;
use Hanson\Vbot\Message\Entity\GroupChange;
use Hanson\Vbot\Message\Entity\Recall;
use Hanson\Vbot\Message\Entity\Recommend;
use Hanson\Vbot\Message\Entity\RedPacket;
......@@ -44,6 +46,8 @@ class MessageFactory
case 1: //文本消息
if(Location::isLocation($msg)){
return new Location($msg);
}elseif(contact()->get($msg['FromUserName']) && str_contains($msg['Content'], '过了你的朋友验证请求')){
return new NewFriend($msg);
}else{
return new Text($msg);
}
......@@ -61,8 +65,11 @@ class MessageFactory
if(str_contains($msg['Content'], '利是') || str_contains($msg['Content'], '红包') || str_contains($msg['Content'], 'Red Packet')){
return new RedPacket($msg);
}
else if(str_contains($msg['Content'], '添加') || str_contains($msg['Content'], 'have added')){
else if(str_contains($msg['Content'], '添加') || str_contains($msg['Content'], 'have added') || str_contains($msg['Content'], '打招呼')){
# 添加好友
return new NewFriend($msg);
}else if(str_contains($msg['Content'], '加入了群聊') || str_contains($msg['Content'], '移出了群聊') || str_contains($msg['Content'], '改群名为')){
return new GroupChange($msg);
}
break;
case 49:
......
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2017/2/12
* Time: 20:44
*/
namespace Hanson\Vbot\Message\Entity;
use Hanson\Vbot\Collections\ContactFactory;
use Hanson\Vbot\Message\MessageInterface;
use Hanson\Vbot\Support\Console;
class GroupChange extends Message implements MessageInterface
{
public $action;
public $rename;
public function __construct($msg)
{
parent::__construct($msg);
$this->make();
}
public function make()
{
if(str_contains($this->msg['Content'], '加入了群聊')){
$this->action = 'ADD';
Console::log("检测到 {$this->from['NickName']} 有新成员,正在刷新群成员列表...");
(new ContactFactory())->makeContactList();
Console::log('群成员更新成功!');
}elseif(str_contains($this->msg['Content'], '移出了群聊')){
$this->action = 'REMOVE';
}elseif(str_contains($this->msg['Content'], '改群名为')){
$this->action = 'RENAME';
preg_match('/改群名为“(.+)”/', $this->msg['Content'], $match);
$this->updateGroupName($match[1]);
}
$this->content = $this->msg['Content'];
}
private function updateGroupName($name)
{
$group = group()->get($this->from['UserName']);
$group['NickName'] = $this->rename = $name;
group()->put($group['UserName'], $group);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2017/2/12
* Time: 20:44
*/
namespace Hanson\Vbot\Message\Entity;
use Hanson\Vbot\Collections\ContactFactory;
use Hanson\Vbot\Message\MessageInterface;
use Hanson\Vbot\Support\Console;
class NewFriend extends Message implements MessageInterface
{
public function __construct($msg)
{
$this->make();
parent::__construct($msg);
}
public function make()
{
Console::log('检测到新加好友,正在刷新好友列表...');
(new ContactFactory())->makeContactList();
Console::log('好友更新成功!');
}
}
\ No newline at end of file
......@@ -52,7 +52,7 @@ class Recall extends Message implements MessageInterface
$this->origin = message()->get($msgId, null);
if($this->origin){
$this->nickname = $this->origin->sender ? $message->sender['NickName'] : account()->getAccount($this->origin->msg['FromUserName'])['NickName'];
$this->nickname = $this->origin->sender ? $this->origin->sender['NickName'] : account()->getAccount($this->origin->msg['FromUserName'])['NickName'];
$this->setContent();
}
......
......@@ -41,7 +41,7 @@ class RequestFriend extends Message implements MessageInterface
$isMatch = preg_match('/bigheadimgurl="(.+?)"/', $this->msg['Content'], $matches);
if ($isMatch) {
$this->avatar = $matches[3];
$this->avatar = $matches[1];
}
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!