Commit ce435cad by hanccc

消息发送模块

1 parent 889d390d
......@@ -21,5 +21,8 @@
"psr-4": {
"Hanson\\Robot\\": "src/"
}
},
"require-dev": {
"phpstan/phpstan": "^0.4.2"
}
}
......@@ -40,13 +40,14 @@ class Account extends Collection
/**
* 增加群聊天
*
* @param $id
* @param $groupMember
*/
public function addGroupMember($groupMember)
public function addGroupMember($id, $groupMember)
{
$account = static::$instance->all();
$account[static::GROUP_MEMBER][] = $groupMember;
$account[static::GROUP_MEMBER][$id] = $groupMember;
static::$instance = static::$instance->make($account);
}
......@@ -54,13 +55,14 @@ class Account extends Collection
/**
* 增加联系人聊天
*
* @param $id
* @param $normalMember
*/
public function addNormalMember($normalMember)
public function addNormalMember($id, $normalMember)
{
$account = static::$instance->all();
$account[static::NORMAL_MEMBER][] = $normalMember;
$account[static::NORMAL_MEMBER][$id] = $normalMember;
static::$instance = static::$instance->make($account);
}
......@@ -109,4 +111,11 @@ class Account extends Collection
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
......@@ -37,11 +37,10 @@ class ContactFactory
$content = $this->server->http->json($url, [
'BaseRequest' => $this->server->baseRequest
]);
$memberList = json_decode($content, true)['MemberList'];
], true);
// file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode($content));
$this->makeContactList($memberList);
$this->makeContactList($content['MemberList']);
}
/**
......@@ -65,7 +64,7 @@ class ContactFactory
$type = '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();
......@@ -88,11 +87,11 @@ class ContactFactory
$list[] = ['UserName' => $key, 'EncryChatRoomId' => ''];
});
file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode([
'BaseRequest' => $this->server->baseRequest,
'Count' => GroupAccount::getInstance()->count(),
'List' => $list
]));
// file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode([
// 'BaseRequest' => $this->server->baseRequest,
// 'Count' => count($list),
// 'List' => $list
// ]));
$content = $this->server->http->json($url, [
'BaseRequest' => $this->server->baseRequest,
......@@ -116,7 +115,7 @@ class ContactFactory
$groupAccount['ChatRoomId'] = $group['EncryChatRoomId'];
GroupAccount::getInstance()->put($group['UserName'], $groupAccount);
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
return static::$instance;
}
public static function isGroup($userName){
return strstr($userName, '@@') !== false;
}
}
\ No newline at end of file
......@@ -22,11 +22,13 @@ class Http
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';
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)
......
......@@ -106,15 +106,30 @@ class MessageHandler
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,
'Msg' => [
'Type' => 1,
'Content' => urlencode($response),
'Content' => $response,
'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;
use Endroid\QrCode\QrCode;
use GuzzleHttp\Client;
use Hanson\Robot\Collections\Account;
use Hanson\Robot\Collections\ContactFactory;
use Hanson\Robot\Collections\GroupAccount;
use Hanson\Robot\Support\Log;
......@@ -212,22 +213,31 @@ class Server
$content = $this->http->json($url, [
'BaseRequest' => $this->baseRequest
]);
print_r($this->baseRequest);
$result = json_decode($content, true);
$this->generateSyncKey($result);
static::$myAccount = $result['User'];
$this->initContactList($result['ContactList']);
if($result['BaseResponse']['Ret'] != 0){
print_r($result);
file_put_contents($this->config['tmp'] . 'debug.json', $content);
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()
{
new ContactFactory($this);
......
......@@ -78,10 +78,10 @@ class Message
// $this->setContent();
$this->setType();
$this->setFromType();
$this->setType();
return $this;
}
......@@ -205,7 +205,7 @@ class Message
private function setLocationMessage()
{
$this->FromType = 'Location';
$this->url = $this->rawMsg['Url'];
// $this->url = $this->rawMsg['Url'];
$this->content->msg = Location::getLocationText($this->rawMsg['Content']);
}
......@@ -218,7 +218,7 @@ class Message
{
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);
}
......
......@@ -8,35 +8,23 @@
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([
'tmp' => realpath('./tmp') . '/',
'debug' => true,
'tuling' => true,
'tuling_key' => ''
]);
$client = new \GuzzleHttp\Client();
$robot->server->setMessageHandler(function($message){
// if($message->type === 'text'){
//
// }elseif ($message->type === 'location'){
// return Message::make();
// }
//
// if($message->FromUserName === ''){
// # do something;
// }
$robot->server->setMessageHandler(function($message) use ($client, $robot){
$url = 'http://www.tuling123.com/openapi/api';
$result = $robot->server->http->post($url, [
'key' => '1dce02aef026258eff69635a06b0ab7d',
'info' => $message->rawMsg['Content']
], true);
print_r($message);
print_r($result);
return $result['text'];
});
$robot->server->run();
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!