Commit ce435cad by hanccc

消息发送模块

1 parent 889d390d
...@@ -21,5 +21,8 @@ ...@@ -21,5 +21,8 @@
"psr-4": { "psr-4": {
"Hanson\\Robot\\": "src/" "Hanson\\Robot\\": "src/"
} }
},
"require-dev": {
"phpstan/phpstan": "^0.4.2"
} }
} }
...@@ -40,13 +40,14 @@ class Account extends Collection ...@@ -40,13 +40,14 @@ class Account extends Collection
/** /**
* 增加群聊天 * 增加群聊天
* *
* @param $id
* @param $groupMember * @param $groupMember
*/ */
public function addGroupMember($groupMember) public function addGroupMember($id, $groupMember)
{ {
$account = static::$instance->all(); $account = static::$instance->all();
$account[static::GROUP_MEMBER][] = $groupMember; $account[static::GROUP_MEMBER][$id] = $groupMember;
static::$instance = static::$instance->make($account); static::$instance = static::$instance->make($account);
} }
...@@ -54,13 +55,14 @@ class Account extends Collection ...@@ -54,13 +55,14 @@ class Account extends Collection
/** /**
* 增加联系人聊天 * 增加联系人聊天
* *
* @param $id
* @param $normalMember * @param $normalMember
*/ */
public function addNormalMember($normalMember) public function addNormalMember($id, $normalMember)
{ {
$account = static::$instance->all(); $account = static::$instance->all();
$account[static::NORMAL_MEMBER][] = $normalMember; $account[static::NORMAL_MEMBER][$id] = $normalMember;
static::$instance = static::$instance->make($account); static::$instance = static::$instance->make($account);
} }
...@@ -109,4 +111,11 @@ class Account extends Collection ...@@ -109,4 +111,11 @@ class Account extends Collection
return $target[$id] ?? null; return $target[$id] ?? null;
} }
public function getGroupMember($id)
{
$target = static::$instance->get(static::GROUP_MEMBER);
return $target[$id] ?? null;
}
} }
\ No newline at end of file \ No newline at end of file
...@@ -37,11 +37,10 @@ class ContactFactory ...@@ -37,11 +37,10 @@ class ContactFactory
$content = $this->server->http->json($url, [ $content = $this->server->http->json($url, [
'BaseRequest' => $this->server->baseRequest 'BaseRequest' => $this->server->baseRequest
]); ], true);
// file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode($content));
$memberList = json_decode($content, true)['MemberList'];
$this->makeContactList($memberList); $this->makeContactList($content['MemberList']);
} }
/** /**
...@@ -65,7 +64,7 @@ class ContactFactory ...@@ -65,7 +64,7 @@ class ContactFactory
$type = 'contact'; $type = 'contact';
ContactAccount::getInstance()->put($contact['UserName'], $contact); ContactAccount::getInstance()->put($contact['UserName'], $contact);
} }
Account::getInstance()->addNormalMember([$contact['UserName'] => ['type' => $type, 'info' => $contact]]); Account::getInstance()->addNormalMember($contact['UserName'], ['type' => $type, 'info' => $contact]);
} }
$this->getBatchGroupMembers(); $this->getBatchGroupMembers();
...@@ -88,11 +87,11 @@ class ContactFactory ...@@ -88,11 +87,11 @@ class ContactFactory
$list[] = ['UserName' => $key, 'EncryChatRoomId' => '']; $list[] = ['UserName' => $key, 'EncryChatRoomId' => ''];
}); });
file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode([ // file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode([
'BaseRequest' => $this->server->baseRequest, // 'BaseRequest' => $this->server->baseRequest,
'Count' => GroupAccount::getInstance()->count(), // 'Count' => count($list),
'List' => $list // 'List' => $list
])); // ]));
$content = $this->server->http->json($url, [ $content = $this->server->http->json($url, [
'BaseRequest' => $this->server->baseRequest, 'BaseRequest' => $this->server->baseRequest,
...@@ -116,7 +115,7 @@ class ContactFactory ...@@ -116,7 +115,7 @@ class ContactFactory
$groupAccount['ChatRoomId'] = $group['EncryChatRoomId']; $groupAccount['ChatRoomId'] = $group['EncryChatRoomId'];
GroupAccount::getInstance()->put($group['UserName'], $groupAccount); GroupAccount::getInstance()->put($group['UserName'], $groupAccount);
foreach ($group['MemberList'] as $member) { foreach ($group['MemberList'] as $member) {
Account::getInstance()->addGroupMember([$member['UserName'] => ['type' => 'groupMember', 'info' => $member, 'group' => $group]]); Account::getInstance()->addGroupMember($member['UserName'], ['type' => 'groupMember', 'info' => $member, 'group' => $group]);
} }
} }
......
...@@ -30,4 +30,8 @@ class GroupAccount extends Collection ...@@ -30,4 +30,8 @@ class GroupAccount extends Collection
return static::$instance; return static::$instance;
} }
public static function isGroup($userName){
return strstr($userName, '@@') !== false;
}
} }
\ No newline at end of file \ No newline at end of file
...@@ -22,11 +22,13 @@ class Http ...@@ -22,11 +22,13 @@ class Http
return $this->request($url, 'GET', $query); return $this->request($url, 'GET', $query);
} }
public function post($url, $options = []) public function post($url, $options = [], $array = false)
{ {
$key = is_array($options) ? 'form_params' : 'body'; $key = is_array($options) ? 'form_params' : 'body';
return $this->request($url, 'POST', [$key => $options]); $content = $this->request($url, 'POST', [$key => $options]);
return $array ? json_decode($content, true) : $content;
} }
public function json($url, $options = [], $array = false) public function json($url, $options = [], $array = false)
......
...@@ -106,15 +106,30 @@ class MessageHandler ...@@ -106,15 +106,30 @@ class MessageHandler
return false; return false;
} }
$this->server->http->json(Server::BASE_URI . '/webwxsendmsg?pass_ticket=' . $this->server->passTicket, [ $random = strval(time() * 1000) . '0' . strval(rand(100, 999));
echo $response;
$result = $this->server->http->json(Server::BASE_URI . '/webwxsendmsg?pass_ticket=' . $this->server->passTicket, [
'BaseRequest' => $this->server->baseRequest, 'BaseRequest' => $this->server->baseRequest,
'Msg' => [ 'Msg' => [
'Type' => 1, 'Type' => 1,
'Content' => urlencode($response), 'Content' => $response,
'FromUserName' => $this->server->getMyAccount(), 'FromUserName' => $this->server->getMyAccount(),
'ToUserName' => $content->to 'ToUserName' => $content->rawMsg['FromUserName'],
'LocalID' => $random,
'ClientMsgId' => $random
] ]
]); ], true);
if($result['BaseResponse']['Ret'] != 0){
Log::echo('发送消息失败');
}
}
private function unicode($utf8_str) {
$unicode = (ord($utf8_str[0]) & 0x1F) << 12;
$unicode |= (ord($utf8_str[1]) & 0x3F) << 6;
$unicode |= (ord($utf8_str[2]) & 0x3F);
return dechex($unicode);
} }
/** /**
......
...@@ -11,6 +11,7 @@ namespace Hanson\Robot\Core; ...@@ -11,6 +11,7 @@ namespace Hanson\Robot\Core;
use Endroid\QrCode\QrCode; use Endroid\QrCode\QrCode;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Hanson\Robot\Collections\Account;
use Hanson\Robot\Collections\ContactFactory; use Hanson\Robot\Collections\ContactFactory;
use Hanson\Robot\Collections\GroupAccount; use Hanson\Robot\Collections\GroupAccount;
use Hanson\Robot\Support\Log; use Hanson\Robot\Support\Log;
...@@ -213,21 +214,30 @@ class Server ...@@ -213,21 +214,30 @@ class Server
'BaseRequest' => $this->baseRequest 'BaseRequest' => $this->baseRequest
]); ]);
print_r($this->baseRequest);
$result = json_decode($content, true); $result = json_decode($content, true);
$this->generateSyncKey($result); $this->generateSyncKey($result);
static::$myAccount = $result['User']; static::$myAccount = $result['User'];
$this->initContactList($result['ContactList']);
if($result['BaseResponse']['Ret'] != 0){ if($result['BaseResponse']['Ret'] != 0){
print_r($result);
file_put_contents($this->config['tmp'] . 'debug.json', $content);
throw new Exception('[ERROR] init fail!'); throw new Exception('[ERROR] init fail!');
} }
} }
protected function initContactList($contactList)
{
if($contactList){
foreach ($contactList as $contact) {
if(GroupAccount::isGroup($contact['UserName'])){
GroupAccount::getInstance()->put($contact['UserName'], $contact);
Account::getInstance()->addNormalMember($contact['UserName'], ['type' => 'group', 'info' => $contact]);
}
}
}
}
protected function initContact() protected function initContact()
{ {
new ContactFactory($this); new ContactFactory($this);
......
...@@ -78,10 +78,10 @@ class Message ...@@ -78,10 +78,10 @@ class Message
// $this->setContent(); // $this->setContent();
$this->setType();
$this->setFromType(); $this->setFromType();
$this->setType();
return $this; return $this;
} }
...@@ -205,7 +205,7 @@ class Message ...@@ -205,7 +205,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']);
} }
...@@ -218,7 +218,7 @@ class Message ...@@ -218,7 +218,7 @@ class Message
{ {
list($uid, $content) = explode('<br/>', $content, 2); list($uid, $content) = explode('<br/>', $content, 2);
$this->sender = Account::getInstance()->get('normalMember')[substr($uid, 0, -1)]; $this->sender = Account::getInstance()->getGroupMember(substr($uid, 0, -1));
$this->rawMsg['Content'] = $this->formatContent($content); $this->rawMsg['Content'] = $this->formatContent($content);
} }
......
...@@ -8,35 +8,23 @@ ...@@ -8,35 +8,23 @@
require_once __DIR__ . './../vendor/autoload.php'; require_once __DIR__ . './../vendor/autoload.php';
$robot = new \Hanson\Robot\Robot([
'tmp' => realpath('./tmp') . '/',
'debug' => true,
'tuling' => true,
'tuling_key' => ''
]);
//$robot->run();
$robot = new \Hanson\Robot\Foundation\Robot([ $robot = new \Hanson\Robot\Foundation\Robot([
'tmp' => realpath('./tmp') . '/', 'tmp' => realpath('./tmp') . '/',
'debug' => true, 'debug' => true,
'tuling' => true,
'tuling_key' => ''
]); ]);
$client = new \GuzzleHttp\Client();
$robot->server->setMessageHandler(function($message){ $robot->server->setMessageHandler(function($message) use ($client, $robot){
// if($message->type === 'text'){ $url = 'http://www.tuling123.com/openapi/api';
//
// }elseif ($message->type === 'location'){
// return Message::make();
// }
//
// if($message->FromUserName === ''){
// # do something;
// }
$result = $robot->server->http->post($url, [
'key' => '1dce02aef026258eff69635a06b0ab7d',
'info' => $message->rawMsg['Content']
], true);
print_r($message); print_r($message);
print_r($result);
return $result['text'];
}); });
$robot->server->run(); $robot->server->run();
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!