Commit 288f7b2a by hanccc

增加example

1 parent 0871aa18
...@@ -104,7 +104,7 @@ class Account extends Collection ...@@ -104,7 +104,7 @@ class Account extends Collection
* @param $id * @param $id
* @return array * @return array
*/ */
public function getContact($id) public function getContactByUsername($id)
{ {
$target = static::$instance->get(static::NORMAL_MEMBER); $target = static::$instance->get(static::NORMAL_MEMBER);
...@@ -118,4 +118,51 @@ class Account extends Collection ...@@ -118,4 +118,51 @@ class Account extends Collection
return $target[$id] ?? null; return $target[$id] ?? null;
} }
/**
* 获取联系人列表
*
* @return Collection
*/
public function getNormalMembers()
{
$target = static::$instance->get(static::NORMAL_MEMBER);
return collect($target);
}
/**
* 根据微信号获取联系人
*
* @param $id
* @return mixed
*/
public function getContactById($id)
{
$contact = $this->getNormalMembers()->filter(function($item, $key) use ($id){
if($item['info']['Alias'] === $id){
return true;
}
})->first();
return $contact;
}
/**
* 根据微信号获取联系username
*
* @param $id
* @return mixed
*/
public function getUsernameById($id)
{
$contact = $this->getNormalMembers()->search(function($item, $key) use ($id){
if($item['info']['Alias'] === $id){
return true;
}
});
return $contact;
}
} }
\ No newline at end of file \ No newline at end of file
...@@ -23,7 +23,6 @@ class Http ...@@ -23,7 +23,6 @@ class Http
public static function getInstance() public static function getInstance()
{ {
if(!static::$instance){ if(!static::$instance){
echo 'http is null';
static::$instance = new Http(); static::$instance = new Http();
} }
......
...@@ -21,6 +21,8 @@ class MessageHandler ...@@ -21,6 +21,8 @@ class MessageHandler
private $handler; private $handler;
private $customHandler;
static $instance = null; static $instance = null;
/** /**
...@@ -46,6 +48,15 @@ class MessageHandler ...@@ -46,6 +48,15 @@ class MessageHandler
$this->handler = $closure; $this->handler = $closure;
} }
public function setCustomHandler(Closure $closure)
{
if(!$closure instanceof Closure){
throw new \Exception('message handler must be a closure!');
}
$this->customHandler = $closure;
}
/** /**
* listen the chat api * listen the chat api
*/ */
...@@ -54,6 +65,11 @@ class MessageHandler ...@@ -54,6 +65,11 @@ class MessageHandler
$this->preCheckSync(); $this->preCheckSync();
while (true){ while (true){
if($this->customHandler){
call_user_func_array($this->customHandler, []);
}
$time = time(); $time = time();
list($retCode, $selector) = $this->checkSync(); list($retCode, $selector) = $this->checkSync();
......
...@@ -292,6 +292,15 @@ class Server ...@@ -292,6 +292,15 @@ class Server
MessageHandler::getInstance()->setMessageHandler($closure); MessageHandler::getInstance()->setMessageHandler($closure);
} }
public function setCustomerHandler(\Closure $closure)
{
if(!is_callable($closure)){
throw new \Exception('[ERROR] message handler must be a closure!');
}
MessageHandler::getInstance()->setCustomHandler($closure);
}
public function debug($debug = true) public function debug($debug = true)
{ {
$this->debug = $debug; $this->debug = $debug;
......
...@@ -29,6 +29,8 @@ class Message ...@@ -29,6 +29,8 @@ class Message
*/ */
public $sender; public $sender;
public $username;
public $to; public $to;
public $content; public $content;
...@@ -38,7 +40,7 @@ class Message ...@@ -38,7 +40,7 @@ class Message
/** /**
* @var string 消息发送者类型 * @var string 消息发送者类型
*/ */
public $FromType; public $fromType;
/** /**
* @var string 消息类型 * @var string 消息类型
...@@ -84,34 +86,35 @@ class Message ...@@ -84,34 +86,35 @@ class Message
*/ */
private function setFrom() private function setFrom()
{ {
$this->from = Account::getInstance()->getContact($this->rawMsg['FromUserName']); $this->from = Account::getInstance()->getContactByUsername($this->rawMsg['FromUserName']);
$this->username = $this->rawMsg['FromUserName'];
} }
private function setTo() private function setTo()
{ {
$this->to = Account::getInstance()->getContact($this->rawMsg['ToUserName']); $this->to = Account::getInstance()->getContactByUsername($this->rawMsg['ToUserName']);
} }
private function setFromType() private function setFromType()
{ {
if ($this->rawMsg['MsgType'] == 51) { if ($this->rawMsg['MsgType'] == 51) {
$this->FromType = 'System'; $this->fromType = 'System';
} elseif ($this->rawMsg['MsgType'] == 37) { } elseif ($this->rawMsg['MsgType'] == 37) {
$this->FromType = 'FriendRequest'; $this->fromType = 'FriendRequest';
} elseif ($this->rawMsg['FromUserName'] === myself()->userName) { } elseif ($this->rawMsg['FromUserName'] === myself()->userName) {
$this->FromType = 'Self'; $this->fromType = 'Self';
} elseif ($this->rawMsg['ToUserName'] === 'filehelper') { } elseif ($this->rawMsg['ToUserName'] === 'filehelper') {
$this->FromType = 'FileHelper'; $this->fromType = 'FileHelper';
} elseif (substr($this->rawMsg['FromUserName'], 0, 2) === '@@') { # group } elseif (substr($this->rawMsg['FromUserName'], 0, 2) === '@@') { # group
$this->FromType = 'Group'; $this->fromType = 'Group';
} elseif (ContactAccount::getInstance()->isContact($this->rawMsg['FromUserName'])) { } elseif (ContactAccount::getInstance()->isContact($this->rawMsg['FromUserName'])) {
$this->FromType = 'Contact'; $this->fromType = 'Contact';
} elseif (OfficialAccount::getInstance()->isPublic($this->rawMsg['FromUserName'])) { } elseif (OfficialAccount::getInstance()->isPublic($this->rawMsg['FromUserName'])) {
$this->FromType = 'Public'; $this->fromType = 'Public';
} elseif (SpecialAccount::getInstance()->get($this->rawMsg['FromUserName'], false)) { } elseif (SpecialAccount::getInstance()->get($this->rawMsg['FromUserName'], false)) {
$this->FromType = 'Special'; $this->fromType = 'Special';
} else { } else {
$this->FromType = 'Unknown'; $this->fromType = 'Unknown';
} }
} }
...@@ -129,12 +132,12 @@ class Message ...@@ -129,12 +132,12 @@ class Message
*/ */
private function setTypeByFrom() private function setTypeByFrom()
{ {
if($this->FromType === 'System'){ if($this->fromType === 'System'){
$this->type = 'Empty'; $this->type = 'Empty';
}elseif ($this->FromType === 'FileHelper'){ # File Helper }elseif ($this->fromType === 'FileHelper'){ # File Helper
$this->type = 'Text'; $this->type = 'Text';
$this->content->msg = $this->formatContent($this->rawMsg['Content']); $this->content->msg = $this->formatContent($this->rawMsg['Content']);
}elseif ($this->FromType === 'Group'){ }elseif ($this->fromType === 'Group'){
$this->handleGroupContent($this->rawMsg['Content']); $this->handleGroupContent($this->rawMsg['Content']);
} }
} }
...@@ -203,7 +206,7 @@ class Message ...@@ -203,7 +206,7 @@ class Message
*/ */
private function setLocationMessage() private function setLocationMessage()
{ {
$this->FromType = 'Location'; $this->fromType = 'Location';
// $this->url = $this->rawMsg['Url']; // $this->url = $this->rawMsg['Url'];
$this->content->msg = Location::getLocationText($this->rawMsg['Content']); $this->content->msg = Location::getLocationText($this->rawMsg['Content']);
} }
......
...@@ -17,9 +17,11 @@ $client = new \GuzzleHttp\Client(); ...@@ -17,9 +17,11 @@ $client = new \GuzzleHttp\Client();
$robot->server->setMessageHandler(function($message) use ($client, $robot){ $robot->server->setMessageHandler(function($message) use ($client, $robot){
if($message->type === 'Text'){ if($message->type === 'Text'){
// print_r($message); $contact = \Hanson\Robot\Collections\Account::getInstance()->getUsernameById('hanson1994');
// echo $message->rawMsg['FromUserName'];
\Hanson\Robot\Message\Message::send('hi', $message->rawMsg['FromUserName']); \Hanson\Robot\Message\Message::send('hi', $message->rawMsg['FromUserName']);
\Hanson\Robot\Message\Message::send($message->content, $contact);
} }
}); });
$robot->server->setCustomerHandler(function(){
});
$robot->server->run(); $robot->server->run();
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2016/12/7
* Time: 16:33
*/
require_once __DIR__ . './../vendor/autoload.php';
$robot = new \Hanson\Robot\Foundation\Robot([
'tmp' => realpath('./tmp') . '/',
'debug' => true,
]);
$client = new \GuzzleHttp\Client();
$robot->server->setMessageHandler(function($message) use ($client, $robot){
/** @var $message \Hanson\Robot\Message\Message */
if($message->type === 'Text'){
\Hanson\Robot\Message\Message::send($message->fromType, $message->username);
}
});
$robot->server->run();
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!