Commit 0871aa18 by hanccc

大重构

1 parent f92223ed
...@@ -18,6 +18,9 @@ ...@@ -18,6 +18,9 @@
"illuminate/support": "^5.3" "illuminate/support": "^5.3"
}, },
"autoload": { "autoload": {
"files": [
"src/Support/helpers.php"
],
"psr-4": { "psr-4": {
"Hanson\\Robot\\": "src/" "Hanson\\Robot\\": "src/"
} }
......
...@@ -10,11 +10,11 @@ namespace Hanson\Robot\Collections; ...@@ -10,11 +10,11 @@ namespace Hanson\Robot\Collections;
use Hanson\Robot\Core\Server; use Hanson\Robot\Core\Server;
use Hanson\Robot\Support\Log; use Hanson\Robot\Support\Console;
class ContactFactory class ContactFactory
{ {
protected $server; // protected $server;
const SPECIAL_USERS = ['newsapp', 'fmessage', 'filehelper', 'weibo', 'qqmail', const SPECIAL_USERS = ['newsapp', 'fmessage', 'filehelper', 'weibo', 'qqmail',
'fmessage', 'tmessage', 'qmessage', 'qqsync', 'floatbottle', 'fmessage', 'tmessage', 'qmessage', 'qqsync', 'floatbottle',
...@@ -25,18 +25,17 @@ class ContactFactory ...@@ -25,18 +25,17 @@ class ContactFactory
'officialaccounts', 'notification_messages', 'wxid_novlwrv3lqwv11', 'officialaccounts', 'notification_messages', 'wxid_novlwrv3lqwv11',
'gh_22b87fa7cb3c', 'wxitil', 'userexperience_alarm', 'notification_messages']; 'gh_22b87fa7cb3c', 'wxitil', 'userexperience_alarm', 'notification_messages'];
public function __construct(Server $server) public function __construct()
{ {
$this->server = $server;
$this->getContacts(); $this->getContacts();
} }
public function getContacts() public function getContacts()
{ {
$url = sprintf(Server::BASE_URI . '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s', $this->server->passTicket, $this->server->skey, time()); $url = sprintf(Server::BASE_URI . '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s', server()->passTicket, server()->skey, time());
$content = $this->server->http->json($url, [ $content = http()->json($url, [
'BaseRequest' => $this->server->baseRequest 'BaseRequest' => server()->baseRequest
], true); ], true);
// file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode($content)); // file_put_contents($this->server->config['tmp'] . 'debug.json', json_encode($content));
...@@ -68,11 +67,11 @@ class ContactFactory ...@@ -68,11 +67,11 @@ class ContactFactory
} }
$this->getBatchGroupMembers(); $this->getBatchGroupMembers();
file_put_contents($this->server->config['tmp'] . 'account.json', json_encode(Account::getInstance()->all())); file_put_contents(server()->config['tmp'] . 'account.json', json_encode(Account::getInstance()->all()));
file_put_contents($this->server->config['tmp'] . 'OfficialAccount.json', json_encode(OfficialAccount::getInstance()->all())); file_put_contents(server()->config['tmp'] . 'OfficialAccount.json', json_encode(OfficialAccount::getInstance()->all()));
file_put_contents($this->server->config['tmp'] . 'SpecialAccount.json', json_encode(SpecialAccount::getInstance()->all())); file_put_contents(server()->config['tmp'] . 'SpecialAccount.json', json_encode(SpecialAccount::getInstance()->all()));
file_put_contents($this->server->config['tmp'] . 'GroupAccount.json', json_encode(GroupAccount::getInstance()->all())); file_put_contents(server()->config['tmp'] . 'GroupAccount.json', json_encode(GroupAccount::getInstance()->all()));
file_put_contents($this->server->config['tmp'] . 'ContactAccount.json', json_encode(ContactAccount::getInstance()->all())); file_put_contents(server()->config['tmp'] . 'ContactAccount.json', json_encode(ContactAccount::getInstance()->all()));
} }
/** /**
...@@ -80,7 +79,7 @@ class ContactFactory ...@@ -80,7 +79,7 @@ class ContactFactory
*/ */
public function getBatchGroupMembers() public function getBatchGroupMembers()
{ {
$url = sprintf(Server::BASE_URI . '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s', time(), $this->server->passTicket); $url = sprintf(Server::BASE_URI . '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s', time(), server()->passTicket);
$list = []; $list = [];
GroupAccount::getInstance()->each(function($item, $key) use (&$list){ GroupAccount::getInstance()->each(function($item, $key) use (&$list){
...@@ -93,8 +92,8 @@ class ContactFactory ...@@ -93,8 +92,8 @@ class ContactFactory
// 'List' => $list // 'List' => $list
// ])); // ]));
$content = $this->server->http->json($url, [ $content = http()->json($url, [
'BaseRequest' => $this->server->baseRequest, 'BaseRequest' => server()->baseRequest,
'Count' => GroupAccount::getInstance()->count(), 'Count' => GroupAccount::getInstance()->count(),
'List' => $list 'List' => $list
], true); ], true);
......
...@@ -13,8 +13,23 @@ use GuzzleHttp\Client as HttpClient; ...@@ -13,8 +13,23 @@ use GuzzleHttp\Client as HttpClient;
class Http class Http
{ {
static $instance;
protected $client; protected $client;
/**
* @return Http
*/
public static function getInstance()
{
if(!static::$instance){
echo 'http is null';
static::$instance = new Http();
}
return static::$instance;
}
public function get($url, array $options = []) public function get($url, array $options = [])
{ {
$query = $options ? ['query' => $options] : []; $query = $options ? ['query' => $options] : [];
......
...@@ -11,7 +11,7 @@ namespace Hanson\Robot\Core; ...@@ -11,7 +11,7 @@ namespace Hanson\Robot\Core;
use Closure; use Closure;
use Hanson\Robot\Collections\Account; use Hanson\Robot\Collections\Account;
use Hanson\Robot\Message\Message; use Hanson\Robot\Message\Message;
use Hanson\Robot\Support\Log; use Hanson\Robot\Support\Console;
class MessageHandler class MessageHandler
{ {
...@@ -23,29 +23,15 @@ class MessageHandler ...@@ -23,29 +23,15 @@ class MessageHandler
static $instance = null; static $instance = null;
const MESSAGE_MAP = [
2 => 'text', // 新消息
3 => 'unknown', // 未知
4 => 'contactUpdate', // 通讯录更新
6 => 'money', // 可能是红包
7 => 'mobile' // 手机上操作了微信
];
public function __construct(Server $server)
{
$this->server = $server;
}
/** /**
* get a message handler single instance * get a message handler single instance
* *
* @param Server $server
* @return MessageHandler * @return MessageHandler
*/ */
public static function getInstance($server = null) public static function getInstance()
{ {
if(static::$instance === null){ if(static::$instance === null){
static::$instance = new MessageHandler($server); static::$instance = new MessageHandler();
} }
return static::$instance; return static::$instance;
...@@ -81,7 +67,6 @@ class MessageHandler ...@@ -81,7 +67,6 @@ class MessageHandler
$this->checkTime($time); $this->checkTime($time);
} }
// call_user_func_array($this->handler, []);
} }
private function handlerMessage($selector) private function handlerMessage($selector)
...@@ -108,14 +93,13 @@ class MessageHandler ...@@ -108,14 +93,13 @@ class MessageHandler
} }
$random = strval(time() * 1000) . '0' . strval(rand(100, 999)); $random = strval(time() * 1000) . '0' . strval(rand(100, 999));
echo $word; $result = http()->post(Server::BASE_URI . '/webwxsendmsg?pass_ticket=' . server()->passTicket,
$result = $this->server->http->post(Server::BASE_URI . '/webwxsendmsg?pass_ticket=' . $this->server->passTicket,
json_encode([ json_encode([
'BaseRequest' => $this->server->baseRequest, 'BaseRequest' => server()->baseRequest,
'Msg' => [ 'Msg' => [
'Type' => 1, 'Type' => 1,
'Content' => $word, 'Content' => $word,
'FromUserName' => $this->server->getMyAccount(), 'FromUserName' => myself()->userName,
'ToUserName' => $content->rawMsg['FromUserName'], 'ToUserName' => $content->rawMsg['FromUserName'],
'LocalID' => $random, 'LocalID' => $random,
'ClientMsgId' => $random, 'ClientMsgId' => $random,
...@@ -124,7 +108,7 @@ class MessageHandler ...@@ -124,7 +108,7 @@ class MessageHandler
], JSON_UNESCAPED_UNICODE), true); ], JSON_UNESCAPED_UNICODE), true);
if($result['BaseResponse']['Ret'] != 0){ if($result['BaseResponse']['Ret'] != 0){
Log::echo('发送消息失败'); Console::log('发送消息失败');
} }
} }
...@@ -137,16 +121,16 @@ class MessageHandler ...@@ -137,16 +121,16 @@ class MessageHandler
{ {
$url = 'https://' . $this->syncHost . '/cgi-bin/mmwebwx-bin/synccheck?' . http_build_query([ $url = 'https://' . $this->syncHost . '/cgi-bin/mmwebwx-bin/synccheck?' . http_build_query([
'r' => time(), 'r' => time(),
'sid' => $this->server->sid, 'sid' => server()->sid,
'uin' => $this->server->uin, 'uin' => server()->uin,
'skey' => $this->server->skey, 'skey' => server()->skey,
'deviceid' => $this->server->deviceId, 'deviceid' => server()->deviceId,
'synckey' => $this->server->syncKeyStr, 'synckey' => server()->syncKeyStr,
'_' => time() '_' => time()
]); ]);
try{ try{
$content = $this->server->http->get($url); $content = http()->get($url);
preg_match('/window.synccheck=\{retcode:"(\d+)",selector:"(\d+)"\}/', $content, $matches); preg_match('/window.synccheck=\{retcode:"(\d+)",selector:"(\d+)"\}/', $content, $matches);
...@@ -177,12 +161,12 @@ class MessageHandler ...@@ -177,12 +161,12 @@ class MessageHandler
private function sync() private function sync()
{ {
$url = sprintf(Server::BASE_URI . '/webwxsync?sid=%s&skey=%s&lang=en_US&pass_ticket=%s', $this->server->sid, $this->server->skey, $this->server->passTicket); $url = sprintf(Server::BASE_URI . '/webwxsync?sid=%s&skey=%s&lang=en_US&pass_ticket=%s', server()->sid, server()->skey, server()->passTicket);
try{ try{
$result = $this->server->http->json($url, [ $result = http()->json($url, [
'BaseRequest' => $this->server->baseRequest, 'BaseRequest' => server()->baseRequest,
'SyncKey' => $this->server->syncKey, 'SyncKey' => server()->syncKey,
'rr' => ~time() 'rr' => ~time()
], true); ], true);
...@@ -203,15 +187,15 @@ class MessageHandler ...@@ -203,15 +187,15 @@ class MessageHandler
*/ */
private function generateSyncKey($result) private function generateSyncKey($result)
{ {
$this->server->syncKey = $result['SyncKey']; server()->syncKey = $result['SyncKey'];
$syncKey = []; $syncKey = [];
foreach ($this->server->syncKey['List'] as $item) { foreach (server()->syncKey['List'] as $item) {
$syncKey[] = $item['Key'] . '_' . $item['Val']; $syncKey[] = $item['Key'] . '_' . $item['Val'];
} }
$this->server->syncKeyStr = implode('|', $syncKey); server()->syncKeyStr = implode('|', $syncKey);
} }
/** /**
...@@ -237,7 +221,7 @@ class MessageHandler ...@@ -237,7 +221,7 @@ class MessageHandler
*/ */
private function debugMessage($retCode, $selector, $sleep = null) private function debugMessage($retCode, $selector, $sleep = null)
{ {
Log::echo('[DEBUG] retcode:' . $retCode . ' selector:' . $selector); Console::log('[DEBUG] retcode:' . $retCode . ' selector:' . $selector);
if($sleep){ if($sleep){
sleep($sleep); sleep($sleep);
......
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2017/1/3
* Time: 21:54
*/
namespace Hanson\Robot\Core;
class Myself
{
static $instance;
public $nickname;
public $userName;
public $uin;
public $sex;
public static function getInstance()
{
if(!static::$instance){
static::$instance = new Myself();
}
return static::$instance;
}
public function init($user)
{
$this->nickname = $user['NickName'];
$this->userName = $user['UserName'];
$this->sex = $user['Sex'];
$this->uin= $user['Uin'];
}
}
\ No newline at end of file \ No newline at end of file
...@@ -14,13 +14,15 @@ use GuzzleHttp\Client; ...@@ -14,13 +14,15 @@ use GuzzleHttp\Client;
use Hanson\Robot\Collections\Account; 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\Console;
use QueryPath\Exception; use QueryPath\Exception;
use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\DomCrawler\Crawler;
class Server class Server
{ {
static $instance;
protected $uuid; protected $uuid;
protected $redirectUri; protected $redirectUri;
...@@ -39,12 +41,8 @@ class Server ...@@ -39,12 +41,8 @@ class Server
public $syncKey; public $syncKey;
static $myAccount;
public $syncKeyStr; public $syncKeyStr;
public $http;
public $config; public $config;
public $messageHandler; public $messageHandler;
...@@ -57,23 +55,35 @@ class Server ...@@ -57,23 +55,35 @@ class Server
public function __construct($config = []) public function __construct($config = [])
{ {
$this->http = new Http();
$this->config = $config; $this->config = $config;
} }
/** /**
* @param array $config
* @return Server
*/
public static function getInstance($config = [])
{
if(!static::$instance){
static::$instance = new Server($config);
}
return static::$instance;
}
/**
* start a wechat trip * start a wechat trip
*/ */
public function run() public function run()
{ {
$this->prepare(); $this->prepare();
$this->init(); $this->init();
Log::echo('[INFO] init success!'); Console::log('[INFO] init success!');
$this->statusNotify(); $this->statusNotify();
Log::echo('[INFO] begin to init contacts'); Console::log('[INFO] begin to init contacts');
$this->initContact(); $this->initContact();
Log::echo('[INFO] init contacts success!'); Console::log('[INFO] init contacts success!');
MessageHandler::getInstance()->listen(); MessageHandler::getInstance()->listen();
} }
...@@ -82,11 +92,11 @@ class Server ...@@ -82,11 +92,11 @@ class Server
{ {
$this->getUuid(); $this->getUuid();
$this->generateQrCode(); $this->generateQrCode();
Log::echo('[INFO] please scan qrcode to login'); Console::log('[INFO] please scan qrcode to login');
$this->waitForLogin(); $this->waitForLogin();
$this->login(); $this->login();
Log::echo('[INFO] login success!'); Console::log('[INFO] login success!');
} }
/** /**
...@@ -96,7 +106,7 @@ class Server ...@@ -96,7 +106,7 @@ class Server
*/ */
protected function getUuid() protected function getUuid()
{ {
$content = $this->http->get('https://login.weixin.qq.com/jslogin', [ $content = http()->get('https://login.weixin.qq.com/jslogin', [
'appid' => 'wx782c26e4c19acffb', 'appid' => 'wx782c26e4c19acffb',
'fun' => 'new', 'fun' => 'new',
'lang' => 'zh_CN', 'lang' => 'zh_CN',
...@@ -143,14 +153,14 @@ class Server ...@@ -143,14 +153,14 @@ class Server
while($retryTime > 0){ while($retryTime > 0){
$url = sprintf('https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s', $tip, $this->uuid, time()); $url = sprintf('https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s', $tip, $this->uuid, time());
$content = $this->http->get($url); $content = http()->get($url);
preg_match('/window.code=(\d+);/', $content, $matches); preg_match('/window.code=(\d+);/', $content, $matches);
$code = $matches[1]; $code = $matches[1];
switch($code){ switch($code){
case '201': case '201':
Log::echo('[INFO] please confirm to login'); Console::log('[INFO] please confirm to login');
$tip = 0; $tip = 0;
break; break;
case '200': case '200':
...@@ -158,13 +168,13 @@ class Server ...@@ -158,13 +168,13 @@ class Server
$this->redirectUri = $matches[1] . '&fun=new'; $this->redirectUri = $matches[1] . '&fun=new';
return; return;
case '408': case '408':
Log::echo('[ERROR] login timeout. please try 1 second later.'); Console::log('[ERROR] login timeout. please try 1 second later.');
$tip = 1; $tip = 1;
$retryTime -= 1; $retryTime -= 1;
sleep(1); sleep(1);
break; break;
default: default:
Log::echo("[ERROR] login fail. exception code:$code . please try 1 second later."); Console::log("[ERROR] login fail. exception code:$code . please try 1 second later.");
$tip = 1; $tip = 1;
$retryTime -= 1; $retryTime -= 1;
sleep(1); sleep(1);
...@@ -182,7 +192,7 @@ class Server ...@@ -182,7 +192,7 @@ class Server
*/ */
public function login() public function login()
{ {
$content = $this->http->get($this->redirectUri); $content = http()->get($this->redirectUri);
$crawler = new Crawler($content); $crawler = new Crawler($content);
$this->skey = $crawler->filter('error skey')->text(); $this->skey = $crawler->filter('error skey')->text();
...@@ -210,18 +220,19 @@ class Server ...@@ -210,18 +220,19 @@ class Server
{ {
$url = sprintf(self::BASE_URI . '/webwxinit?r=%i&lang=en_US&pass_ticket=%s', time(), $this->passTicket); $url = sprintf(self::BASE_URI . '/webwxinit?r=%i&lang=en_US&pass_ticket=%s', time(), $this->passTicket);
$content = $this->http->json($url, [ $content = http()->json($url, [
'BaseRequest' => $this->baseRequest 'BaseRequest' => $this->baseRequest
]); ]);
$result = json_decode($content, true); $result = json_decode($content, true);
$this->generateSyncKey($result); $this->generateSyncKey($result);
static::$myAccount = $result['User']; myself()->init($result['User']);
$this->initContactList($result['ContactList']); $this->initContactList($result['ContactList']);
if($result['BaseResponse']['Ret'] != 0){ if($result['BaseResponse']['Ret'] != 0){
// Log::echo('init fail!');
throw new Exception('[ERROR] init fail!'); throw new Exception('[ERROR] init fail!');
} }
} }
...@@ -240,7 +251,7 @@ class Server ...@@ -240,7 +251,7 @@ class Server
protected function initContact() protected function initContact()
{ {
new ContactFactory($this); new ContactFactory();
} }
/** /**
...@@ -250,11 +261,11 @@ class Server ...@@ -250,11 +261,11 @@ class Server
{ {
$url = sprintf(self::BASE_URI . '/webwxstatusnotify?lang=zh_CN&pass_ticket=%s', $this->passTicket); $url = sprintf(self::BASE_URI . '/webwxstatusnotify?lang=zh_CN&pass_ticket=%s', $this->passTicket);
$this->http->json($url, [ http()->json($url, [
'BaseRequest' => $this->baseRequest, 'BaseRequest' => $this->baseRequest,
'Code' => 3, 'Code' => 3,
'FromUserName' => static::$myAccount['UserName'], 'FromUserName' => myself()->userName,
'ToUserName' => static::$myAccount['UserName'], 'ToUserName' => myself()->userName,
'ClientMsgId' => time() 'ClientMsgId' => time()
]); ]);
} }
...@@ -272,23 +283,13 @@ class Server ...@@ -272,23 +283,13 @@ class Server
$this->syncKeyStr = implode('|', $syncKey); $this->syncKeyStr = implode('|', $syncKey);
} }
public static function isMyself($fromUserName)
{
return $fromUserName === static::$myAccount['UserName'];
}
public function getMyAccount()
{
return static::$myAccount['UserName'];
}
public function setMessageHandler(\Closure $closure) public function setMessageHandler(\Closure $closure)
{ {
if(!is_callable($closure)){ if(!is_callable($closure)){
throw new \Exception('[ERROR] message handler must be a closure!'); throw new \Exception('[ERROR] message handler must be a closure!');
} }
MessageHandler::getInstance($this)->setMessageHandler($closure); MessageHandler::getInstance()->setMessageHandler($closure);
} }
public function debug($debug = true) public function debug($debug = true)
......
...@@ -10,9 +10,15 @@ namespace Hanson\Robot\Foundation; ...@@ -10,9 +10,15 @@ namespace Hanson\Robot\Foundation;
use Hanson\Robot\Core\Http; use Hanson\Robot\Core\Http;
use Hanson\Robot\Core\Server;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pimple\Container; use Pimple\Container;
/**
* Class Robot
* @package Hanson\Robot\Foundation
* @property Server $server
*/
class Robot extends Container class Robot extends Container
{ {
......
...@@ -14,11 +14,7 @@ class ServerServiceProvider implements ServiceProviderInterface ...@@ -14,11 +14,7 @@ class ServerServiceProvider implements ServiceProviderInterface
public function register(Container $pimple) public function register(Container $pimple)
{ {
$pimple['server'] = function ($pimple) { $pimple['server'] = function ($pimple) {
$server = new Server($pimple['config']); return server($pimple['config']);
$server->debug($pimple['config']['debug']);
return $server;
}; };
} }
} }
...@@ -16,6 +16,8 @@ use Hanson\Robot\Collections\OfficialAccount; ...@@ -16,6 +16,8 @@ use Hanson\Robot\Collections\OfficialAccount;
use Hanson\Robot\Collections\SpecialAccount; use Hanson\Robot\Collections\SpecialAccount;
use Hanson\Robot\Models\Content; use Hanson\Robot\Models\Content;
use Hanson\Robot\Models\Sender; use Hanson\Robot\Models\Sender;
use Hanson\Robot\Support\FileManager;
use Hanson\Robot\Support\Console;
class Message class Message
{ {
...@@ -64,20 +66,12 @@ class Message ...@@ -64,20 +66,12 @@ class Message
public function make($selector, $msg) public function make($selector, $msg)
{ {
$this->rawMsg = $msg; $this->rawMsg = $msg;
// $this->sender = new Sender();
// $this->content = new Content();
// $this->setSender();
$this->setFrom(); $this->setFrom();
$this->setTo(); $this->setTo();
// $this->setContent();
$this->setFromType(); $this->setFromType();
$this->setType(); $this->setType();
...@@ -104,7 +98,7 @@ class Message ...@@ -104,7 +98,7 @@ class Message
$this->FromType = 'System'; $this->FromType = 'System';
} elseif ($this->rawMsg['MsgType'] == 37) { } elseif ($this->rawMsg['MsgType'] == 37) {
$this->FromType = 'FriendRequest'; $this->FromType = 'FriendRequest';
} elseif (Server::isMyself($this->rawMsg['FromUserName'])) { } 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';
...@@ -123,9 +117,7 @@ class Message ...@@ -123,9 +117,7 @@ class Message
private function setType() private function setType()
{ {
// $msgType = $msg['MsgType'];
$this->rawMsg['Content'] = html_entity_decode($this->rawMsg['Content']); $this->rawMsg['Content'] = html_entity_decode($this->rawMsg['Content']);
// $msgId = $msg['MsgId'];
$this->setTypeByFrom(); $this->setTypeByFrom();
...@@ -155,23 +147,30 @@ class Message ...@@ -155,23 +147,30 @@ class Message
switch($this->rawMsg['MsgType']){ switch($this->rawMsg['MsgType']){
case 1: case 1:
if(Location::isLocation($this->rawMsg['Content'])){ if(Location::isLocation($this->rawMsg['Content'])){
// $this->setLocationMessage();
$this->type = 'Location'; $this->type = 'Location';
}else{ }else{
$this->type = 'Text'; $this->type = 'Text';
$this->content = $this->rawMsg['Content'];
} }
break; break;
case 3: case 3:
$this->type = 'Image'; $this->type = 'Image';
$this->content = Server::BASE_URI . sprintf('/webwxgetmsgimg?MsgID=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content);
FileManager::download(server()->config['tmp'] . $this->rawMsg['MsgId'] . '.jpg', $content);
break; break;
case 34: case 34:
$this->type = 'Voice'; $this->type = 'Voice';
$this->content = Server::BASE_URI . sprintf('/webwxgetvoice?msgid=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content);
FileManager::download(server()->config['tmp'] . $this->rawMsg['MsgId'] . '.mp3', $content);
break; break;
case 37: case 37:
$this->type = 'AddUser'; $this->type = 'AddUser';
break; break;
case 42: case 42:
$this->type = 'Recommend'; $this->type = 'Recommend';
$this->content = (object)$this->rawMsg['RecommendInfo'];
break; break;
case 47: case 47:
$this->type = 'Animation'; $this->type = 'Animation';
...@@ -227,4 +226,43 @@ class Message ...@@ -227,4 +226,43 @@ class Message
return str_replace('<br/>', '\n', $content); return str_replace('<br/>', '\n', $content);
} }
/**
* 发送消息
*
* @param $word string 消息内容
* @param $fromUser string 目标username
* @return bool
*/
public static function send($word, $fromUser)
{
if(!$word && !is_string($word)){
return false;
}
$random = strval(time() * 1000) . '0' . strval(rand(100, 999));
$data = [
'BaseRequest' => server()->baseRequest,
'Msg' => [
'Type' => 1,
'Content' => $word,
'FromUserName' => myself()->userName,
'ToUserName' => $fromUser,
'LocalID' => $random,
'ClientMsgId' => $random,
],
'Scene' => 0
];
$result = http()->post(Server::BASE_URI . '/webwxsendmsg?pass_ticket=' . server()->passTicket,
json_encode($data, JSON_UNESCAPED_UNICODE), true
);
if($result['BaseResponse']['Ret'] != 0){
Console::log('发送消息失败');
return false;
}
return true;
}
} }
\ No newline at end of file \ No newline at end of file
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
namespace Hanson\Robot\Support; namespace Hanson\Robot\Support;
class Log class Console
{ {
public static function echo($str) public static function log($str)
{ {
echo $str . PHP_EOL; echo $str . PHP_EOL;
} }
......
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2017/1/2
* Time: 22:21
*/
namespace Hanson\Robot\Support;
class FileManager
{
public static function download($name, $data)
{
file_put_contents($name, $data);
}
}
\ No newline at end of file \ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/29
* Time: 0:10
*/
use Hanson\Robot\Core\Server;
use Hanson\Robot\Core\Myself;
use Hanson\Robot\Core\Http;
if (! function_exists('server')) {
/**
* Get the available container instance.
*
* @param array $config
* @return Server
*/
function server($config = [])
{
return Server::getInstance($config);
}
}
if (! function_exists('myself')) {
/**
* Get the available container instance.
*
* @return Myself
*/
function myself()
{
return Myself::getInstance();
}
}
if (! function_exists('http')) {
/**
* Get the available container instance.
*
* @return Http
*/
function http()
{
return Http::getInstance();
}
}
\ No newline at end of file \ No newline at end of file
<?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){
if($message->type === 'Text'){
// print_r($message);
// echo $message->rawMsg['FromUserName'];
\Hanson\Robot\Message\Message::send('hi', $message->rawMsg['FromUserName']);
}
});
$robot->server->run();
...@@ -16,15 +16,16 @@ $robot = new \Hanson\Robot\Foundation\Robot([ ...@@ -16,15 +16,16 @@ $robot = new \Hanson\Robot\Foundation\Robot([
$client = new \GuzzleHttp\Client(); $client = new \GuzzleHttp\Client();
$robot->server->setMessageHandler(function($message) use ($client, $robot){ $robot->server->setMessageHandler(function($message) use ($client, $robot){
if($message->type === 'Text'){
$url = 'http://www.tuling123.com/openapi/api'; $url = 'http://www.tuling123.com/openapi/api';
$result = $robot->server->http->post($url, [ $result = $robot->server->http->post($url, [
'key' => '1dce02aef026258eff69635a06b0ab7d', 'key' => '1dce02aef026258eff69635a06b0ab7d',
'info' => $message->rawMsg['Content'] 'info' => $message->content
], true); ], true);
print_r($message); print_r($message);
print_r($result); print_r($result);
return $result['text']; 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!