Commit 3936b291 by HanSon

表情支持从表情库中随机发送

sync接口设置35秒超时
Contact构造增加判断
优化自身用的demo
1 parent ec977c7e
...@@ -35,7 +35,8 @@ $robot = new Vbot([ ...@@ -35,7 +35,8 @@ $robot = new Vbot([
]); ]);
// 图灵自动回复 // 图灵自动回复
function reply($str){ function reply($str)
{
return http()->post('http://www.tuling123.com/openapi/api', [ return http()->post('http://www.tuling123.com/openapi/api', [
'key' => '1dce02aef026258eff69635a06b0ab7d', 'key' => '1dce02aef026258eff69635a06b0ab7d',
'info' => $str 'info' => $str
...@@ -49,8 +50,8 @@ $robot->server->setMessageHandler(function ($message) use ($path) { ...@@ -49,8 +50,8 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
// 位置信息 返回位置文字 // 位置信息 返回位置文字
if ($message instanceof Location) { if ($message instanceof Location) {
/** @var $message Location */ /** @var $message Location */
Text::send('地图链接:'.$message->from['UserName'], $message->url); Text::send('地图链接:' . $message->from['UserName'], $message->url);
return '位置:'.$message; return '位置:' . $message;
} }
// 文字信息 // 文字信息
...@@ -60,136 +61,146 @@ $robot->server->setMessageHandler(function ($message) use ($path) { ...@@ -60,136 +61,146 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
if ($message->fromType === 'Contact') { if ($message->fromType === 'Contact') {
return reply($message->content); return reply($message->content);
// 群组@我回复 // 群组@我回复
} elseif ($message->fromType === 'Group' && $message->isAt) { } elseif ($message->fromType === 'Group') {
if (str_contains($message->content, '设置群名称') && $message->from['Alias'] === 'hanson1994') {
group()->setGroupName($message->from['UserName'], str_replace('设置群名称', '', $message->content));
}
if ($message->isAt) {
return reply($message->content); return reply($message->content);
} }
} }
}
// 图片信息 返回接收到的图片 // 图片信息 返回接收到的图片
if ($message instanceof Image) { if ($message instanceof Image) {
return $message; // return $message;
} }
// 视频信息 返回接收到的视频 // 视频信息 返回接收到的视频
if ($message instanceof Video) { if ($message instanceof Video) {
return $message; // return $message;
} }
// 表情信息 返回接收到的表情 // 表情信息 返回接收到的表情
if ($message instanceof Emoticon) { if ($message instanceof Emoticon) {
return $message; Emoticon::sendRandom($message->from['UserName']);
} }
// 语音消息 // 语音消息
if($message instanceof Voice){ if ($message instanceof Voice) {
/** @var $message Voice */ /** @var $message Voice */
return '收到一条语音并下载在' . $message::getPath($message::$folder) . "/{$message->msg['MsgId']}.mp3"; // return '收到一条语音并下载在' . $message::getPath($message::$folder) . "/{$message->msg['MsgId']}.mp3";
} }
// 撤回信息 // 撤回信息
if ($message instanceof Recall && $message->msg['FromUserName'] !== myself()->username) { if ($message instanceof Recall && $message->msg['FromUserName'] !== myself()->username) {
/** @var $message Recall */ /** @var $message Recall */
if($message->origin instanceof Image){ if ($message->origin instanceof Image) {
Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一张照片"); Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一张照片");
Image::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']); Image::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']);
}elseif($message->origin instanceof Emoticon){ } elseif ($message->origin instanceof Emoticon) {
Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一个表情"); Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一个表情");
Emoticon::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']); Emoticon::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']);
}elseif($message->origin instanceof Video){ } elseif ($message->origin instanceof Video) {
Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一个视频"); Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一个视频");
Video::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']); Video::sendByMsgId($message->msg['FromUserName'], $message->origin->msg['MsgId']);
}elseif($message->origin instanceof Voice){ } elseif ($message->origin instanceof Voice) {
Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一条语音"); Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一条语音");
}else{ } else {
Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一条信息 \"{$message->origin->msg['Content']}\""); Text::send($message->msg['FromUserName'], "{$message->nickname} 撤回了一条信息 \"{$message->origin->msg['Content']}\"");
} }
} }
// 红包信息 // 红包信息
if($message instanceof RedPacket){ if ($message instanceof RedPacket) {
// do something to notify if you want ... // do something to notify if you want ...
return $message->content . ' 来自 ' .$message->from['NickName']; return $message->content . ' 来自 ' . $message->from['NickName'];
} }
// 转账信息 // 转账信息
if($message instanceof Transfer){ if ($message instanceof Transfer) {
/** @var $message Transfer */ /** @var $message Transfer */
return $message->content . ' 收到金额 ' . $message->fee; return $message->content . ' 收到金额 ' . $message->fee;
} }
// 推荐名片信息 // 推荐名片信息
if($message instanceof Recommend){ if ($message instanceof Recommend) {
/** @var $message Recommend */ /** @var $message Recommend */
if($message->isOfficial){ if ($message->isOfficial) {
return $message->from['NickName'] . ' 向你推荐了公众号 ' . $message->province . $message->city . return $message->from['NickName'] . ' 向你推荐了公众号 ' . $message->province . $message->city .
" {$message->info['NickName']} 公众号信息: {$message->description}"; " {$message->info['NickName']} 公众号信息: {$message->description}";
}else{ } else {
return $message->from['NickName'] . ' 向你推荐了 ' . $message->province . $message->city . return $message->from['NickName'] . ' 向你推荐了 ' . $message->province . $message->city .
" {$message->info['NickName']} 头像链接: {$message->bigAvatar}"; " {$message->info['NickName']} 头像链接: {$message->bigAvatar}";
} }
} }
// 请求添加信息 // 请求添加信息
if($message instanceof RequestFriend){ if ($message instanceof RequestFriend) {
/** @var $message RequestFriend */ /** @var $message RequestFriend */
$groupUsername = group()->getGroupsByNickname('芬芬', true)->first()['UserName']; $groupUsername = group()->getGroupsByNickname('芬芬', true)->first()['UserName'];
Text::send($groupUsername, "{$message->info['NickName']} 请求添加好友 \"{$message->info['Content']}\""); Text::send($groupUsername, "{$message->info['NickName']} 请求添加好友 \"{$message->info['Content']}\"");
if($message->info['Content'] === '上山打老虎'){ if ($message->info['Content'] === '上山打老虎') {
Text::send($groupUsername, '暗号正确'); Text::send($groupUsername, '暗号正确');
$message->verifyUser($message::VIA); $message->verifyUser($message::VIA);
}else{ } else {
Text::send($groupUsername, '暗号错误'); Text::send($groupUsername, '暗号错误');
} }
} }
// 分享信息 // 分享信息
if($message instanceof Share){ if ($message instanceof Share) {
/** @var $message Share */ /** @var $message Share */
$reply = "收到分享\n标题:{$message->title}\n描述:{$message->description}\n链接:{$message->url}"; $reply = "收到分享\n标题:{$message->title}\n描述:{$message->description}\n链接:{$message->url}";
if($message->app){ if ($message->app) {
$reply .= "\n来源APP:{$message->app}"; $reply .= "\n来源APP:{$message->app}";
} }
return $reply; return $reply;
} }
// 分享小程序信息 // 分享小程序信息
if($message instanceof Mina){ if ($message instanceof Mina) {
/** @var $message Mina */ /** @var $message Mina */
$reply = "收到小程序\n小程序名词:{$message->title}\n链接:{$message->url}"; $reply = "收到小程序\n小程序名词:{$message->title}\n链接:{$message->url}";
return $reply; return $reply;
} }
// 公众号推送信息 // 公众号推送信息
if($message instanceof Official){ if ($message instanceof Official) {
/** @var $message Official */ /** @var $message Official */
$reply = "收到公众号推送\n标题:{$message->title}\n描述:{$message->description}\n链接:{$message->url}\n来源公众号名称:{$message->app}"; $reply = "收到公众号推送\n标题:{$message->title}\n描述:{$message->description}\n链接:{$message->url}\n来源公众号名称:{$message->app}";
return $reply; return $reply;
} }
// 手机点击聊天事件 // 手机点击聊天事件
if($message instanceof Touch){ if ($message instanceof Touch) {
Text::send($message->msg['ToUserName'], "我点击了此聊天"); // Text::send($message->msg['ToUserName'], "我点击了此聊天");
} }
// 新增好友 // 新增好友
if($message instanceof \Hanson\Vbot\Message\Entity\NewFriend){ if ($message instanceof \Hanson\Vbot\Message\Entity\NewFriend) {
\Hanson\Vbot\Support\Console::log('新加好友:' . $message->from['NickName']); \Hanson\Vbot\Support\Console::log('新加好友:' . $message->from['NickName']);
} }
// 群组变动 // 群组变动
if($message instanceof GroupChange){ if ($message instanceof GroupChange) {
/** @var $message GroupChange */ /** @var $message GroupChange */
if($message->action === 'ADD'){ if ($message->action === 'ADD') {
\Hanson\Vbot\Support\Console::log('新人进群'); \Hanson\Vbot\Support\Console::log('新人进群');
return $message->content; return '欢迎新人 ' . $message->nickname;
}elseif($message->action === 'REMOVE'){ } elseif ($message->action === 'REMOVE') {
\Hanson\Vbot\Support\Console::log('群主踢人了'); \Hanson\Vbot\Support\Console::log('群主踢人了');
return $message->content; return $message->content;
}elseif($message->action === 'RENAME'){ } elseif ($message->action === 'RENAME') {
\Hanson\Vbot\Support\Console::log($message->from['NickName'].' 改名为 '.$message->rename); // \Hanson\Vbot\Support\Console::log($message->from['NickName'] . ' 改名为 ' . $message->rename);
return $message->content; if ($message->rename !== 'vbot 测试群'){
group()->setGroupName($message->from['UserName'], 'vbot 测试群');
return '行不改名,坐不改姓!';
}
} }
} }
...@@ -197,11 +208,11 @@ $robot->server->setMessageHandler(function ($message) use ($path) { ...@@ -197,11 +208,11 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
}); });
$robot->server->setExitHandler(function(){ $robot->server->setExitHandler(function () {
\Hanson\Vbot\Support\Console::log('其他设备登录'); \Hanson\Vbot\Support\Console::log('其他设备登录');
}); });
$robot->server->setExceptionHandler(function(){ $robot->server->setExceptionHandler(function () {
\Hanson\Vbot\Support\Console::log('异常退出'); \Hanson\Vbot\Support\Console::log('异常退出');
}); });
......
...@@ -34,11 +34,11 @@ class ContactFactory ...@@ -34,11 +34,11 @@ class ContactFactory
$this->makeContactList(); $this->makeContactList();
$contact = contact()->get(myself()->username); $contact = contact()->get(myself()->username);
myself()->alias = isset($contact['Alias']) ? $contact['Alias'] : myself()->nickname ? : myself()->username; myself()->alias = isset($contact['Alias']) ? $contact['Alias'] : myself()->nickname ?: myself()->username;
$this->getBatchGroupMembers(); $this->getBatchGroupMembers();
if(server()->config['debug']){ if (server()->config['debug']) {
FileManager::download('contact.json', json_encode(contact()->all())); FileManager::download('contact.json', json_encode(contact()->all()));
FileManager::download('member.json', json_encode(member()->all())); FileManager::download('member.json', json_encode(member()->all()));
FileManager::download('group.json', json_encode(group()->all())); FileManager::download('group.json', json_encode(group()->all()));
...@@ -56,23 +56,23 @@ class ContactFactory ...@@ -56,23 +56,23 @@ class ContactFactory
$url = sprintf(server()->baseUri . '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s&seq=%s', server()->passTicket, server()->skey, time(), $seq); $url = sprintf(server()->baseUri . '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s&seq=%s', server()->passTicket, server()->skey, time(), $seq);
$result = http()->json($url, [], true); $result = http()->json($url, [], true);
$memberList = $result['MemberList'];
$seq = $result['Seq'];
foreach ($memberList as $contact) { if (isset($result['MemberList']) && $result['MemberList']) {
if(official()->isOfficial($contact['VerifyFlag'])){ #公众号 foreach ($result['MemberList'] as $contact) {
if (official()->isOfficial($contact['VerifyFlag'])) { #公众号
Official::getInstance()->put($contact['UserName'], $contact); Official::getInstance()->put($contact['UserName'], $contact);
}elseif (in_array($contact['UserName'], static::SPECIAL_USERS)){ # 特殊账户 } elseif (in_array($contact['UserName'], static::SPECIAL_USERS)) { # 特殊账户
Special::getInstance()->put($contact['UserName'], $contact); Special::getInstance()->put($contact['UserName'], $contact);
}elseif (strstr($contact['UserName'], '@@') !== false){ # 群聊 } elseif (strstr($contact['UserName'], '@@') !== false) { # 群聊
group()->put($contact['UserName'], $contact); group()->put($contact['UserName'], $contact);
}else{ } else {
contact()->put($contact['UserName'], $contact); contact()->put($contact['UserName'], $contact);
} }
} }
}
if($seq != 0){ if (isset($result['Seq']) && $result['Seq'] != 0) {
$this->makeContactList($seq); $this->makeContactList($result['Seq']);
} }
} }
...@@ -84,7 +84,7 @@ class ContactFactory ...@@ -84,7 +84,7 @@ class ContactFactory
$url = sprintf(server()->baseUri . '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s', time(), server()->passTicket); $url = sprintf(server()->baseUri . '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s', time(), server()->passTicket);
$list = []; $list = [];
group()->each(function($item, $key) use (&$list){ group()->each(function ($item, $key) use (&$list) {
$list[] = ['UserName' => $key, 'EncryChatRoomId' => '']; $list[] = ['UserName' => $key, 'EncryChatRoomId' => ''];
}); });
...@@ -104,6 +104,7 @@ class ContactFactory ...@@ -104,6 +104,7 @@ class ContactFactory
*/ */
private function initGroupMembers($array) private function initGroupMembers($array)
{ {
if (isset($array['ContactList']) && $array['ContactList']) {
foreach ($array['ContactList'] as $group) { foreach ($array['ContactList'] as $group) {
$groupAccount = group()->get($group['UserName']); $groupAccount = group()->get($group['UserName']);
$groupAccount['MemberList'] = $group['MemberList']; $groupAccount['MemberList'] = $group['MemberList'];
...@@ -113,6 +114,7 @@ class ContactFactory ...@@ -113,6 +114,7 @@ class ContactFactory
member()->put($member['UserName'], $member); member()->put($member['UserName'], $member);
} }
} }
}
} }
......
...@@ -50,9 +50,11 @@ class Http ...@@ -50,9 +50,11 @@ class Http
return $array ? json_decode($content, true) : $content; return $array ? json_decode($content, true) : $content;
} }
public function json($url, $options = [], $array = false) public function json($url, $params = [], $array = false, $extra = [])
{ {
$content = $this->request($url, 'POST', ['json' => $options]); $params = array_merge(['json' => $params], $extra);
$content = $this->request($url, 'POST', $params);
return $array ? json_decode($content, true) : $content; return $array ? json_decode($content, true) : $content;
} }
...@@ -86,15 +88,9 @@ class Http ...@@ -86,15 +88,9 @@ class Http
*/ */
public function request($url, $method = 'GET', $options = []) public function request($url, $method = 'GET', $options = [])
{ {
try{
$response = $this->getClient()->request($method, $url, $options); $response = $this->getClient()->request($method, $url, $options);
return $response->getBody()->getContents();
}catch (\Exception $e){
Console::log('http链接失败:' . $e->getMessage(), Console::ERROR);
Console::log('错误URL:' . $url, Console::ERROR);
}
return null; return $response->getBody()->getContents();
} }
......
...@@ -52,7 +52,7 @@ class Sync ...@@ -52,7 +52,7 @@ class Sync
'BaseRequest' => server()->baseRequest, 'BaseRequest' => server()->baseRequest,
'SyncKey' => server()->syncKey, 'SyncKey' => server()->syncKey,
'rr' => ~time() 'rr' => ~time()
], true); ], true, ['timeout' => 35]);
if($result['BaseResponse']['Ret'] == 0){ if($result['BaseResponse']['Ret'] == 0){
$this->generateSyncKey($result); $this->generateSyncKey($result);
...@@ -60,7 +60,7 @@ class Sync ...@@ -60,7 +60,7 @@ class Sync
return $result; return $result;
}catch (\Exception $e){ }catch (\Exception $e){
return null; $this->sync();
} }
} }
......
...@@ -79,6 +79,22 @@ class Emoticon extends Message implements MediaInterface, MessageInterface ...@@ -79,6 +79,22 @@ class Emoticon extends Message implements MediaInterface, MessageInterface
} }
/** /**
* 从当前账号的本地表情库随机发送一个
*
* @param $username
*/
public static function sendRandom($username)
{
$path = static::getPath(static::$folder);
$files = scandir($path);
unset($files[0], $files[1]);
$msgId = $files[array_rand($files)];
static::send($username, $path . '/' . $msgId);
}
/**
* 下载文件 * 下载文件
* *
* @return mixed * @return mixed
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!