1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* 使用 message handler. |
4
|
|
|
*/ |
5
|
|
|
use Slince\SmartQQ\Client; |
6
|
|
|
use Slince\SmartQQ\Message\Response\FriendMessage; |
7
|
|
|
use Slince\SmartQQ\Message\Response\GroupMessage; |
8
|
|
|
use Slince\SmartQQ\Message\Response\DiscussMessage; |
9
|
|
|
use Slince\SmartQQ\Entity\Discuss; |
10
|
|
|
use Slince\SmartQQ\Entity\DiscussDetail; |
11
|
|
|
|
12
|
|
|
include __DIR__.'/bootstrap.php'; |
13
|
|
|
|
14
|
|
|
//创建smartQQ客户端 |
15
|
|
|
$smartQQ = new Client(getCredential()); |
16
|
|
|
$friends = $smartQQ->getFriends(); |
17
|
|
|
$groups = $smartQQ->getGroups(); |
18
|
|
|
$discusses = $smartQQ->getDiscusses(); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* 获取讨论组详情. |
22
|
|
|
* |
23
|
|
|
* @param Discuss $discuss |
24
|
|
|
* |
25
|
|
|
* @return DiscussDetail |
26
|
|
|
*/ |
27
|
|
|
function getDiscussDetails(Discuss $discuss) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
global $smartQQ; |
30
|
|
|
static $discussDetails = []; |
31
|
|
|
if (isset($discussDetails[$discuss->getId()])) { |
32
|
|
|
return $discussDetails[$discuss->getId()]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $discussDetails[$discuss->getId()] = $smartQQ->getDiscussDetail($discuss); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$handler = $smartQQ->getMessageHandler(); |
39
|
|
|
|
40
|
|
|
$handler->onMessage(function($message) use ($friends, $groups, $discusses){ |
41
|
|
|
|
42
|
|
|
if ($message instanceof FriendMessage) { |
43
|
|
|
$friend = $friends->firstByAttribute('uin', $message->getFromUin()); |
44
|
|
|
$content = sprintf("好友[%s] -- %s: \r\n %s", |
45
|
|
|
$friend->getNick(), |
46
|
|
|
date('H:i:s'), |
47
|
|
|
$message |
48
|
|
|
); |
49
|
|
|
} elseif ($message instanceof GroupMessage) { |
50
|
|
|
$group = $groups->firstByAttribute('id', $message->getFromUin()); |
51
|
|
|
$content = sprintf("群[%s] -- %s: \r\n %s", |
52
|
|
|
$group->getName(), |
53
|
|
|
date('H:i:s'), |
54
|
|
|
$message |
55
|
|
|
); |
56
|
|
|
} elseif ($message instanceof DiscussMessage) { |
57
|
|
|
$discuss = $discusses->firstByAttribute('id', $message->getFromUin()); |
58
|
|
|
$sender = getDiscussDetails($discuss)->getMembers()->firstByAttribute('uin', $message->getSendUin()); |
59
|
|
|
$content = sprintf("讨论组[%s] [%s]-- %s: \r\n %s", |
60
|
|
|
$discuss->getName(), |
61
|
|
|
$sender->getNick(), |
62
|
|
|
date('H:i:s'), |
63
|
|
|
$message |
64
|
|
|
); |
65
|
|
|
} else { |
66
|
|
|
exit('不支持的消息类型'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
printR('收到消息'.PHP_EOL); |
70
|
|
|
printR($content); |
71
|
|
|
echo str_repeat(PHP_EOL, 2); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
$handler->listen(); |
This check looks for functions that have already been defined in other files.
Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the
@ignore
annotation.See also the PhpDoc documentation for @ignore.