Commit 3936b291 by HanSon

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

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