2022-03-15 18:05:33 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2020-03-02 16:14:20 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Module\Example;
|
|
|
|
|
|
|
2022-05-03 10:06:57 +08:00
|
|
|
|
use ZM\Annotation\CQ\CommandArgument;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
use ZM\Annotation\CQ\CQBefore;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
use ZM\Annotation\CQ\CQCommand;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
use ZM\Annotation\CQ\CQMessage;
|
2020-11-08 19:40:16 +08:00
|
|
|
|
use ZM\Annotation\Http\Middleware;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
use ZM\Annotation\Http\RequestMapping;
|
2021-01-02 13:15:50 +08:00
|
|
|
|
use ZM\Annotation\Swoole\OnCloseEvent;
|
|
|
|
|
|
use ZM\Annotation\Swoole\OnOpenEvent;
|
|
|
|
|
|
use ZM\Annotation\Swoole\OnRequestEvent;
|
2022-04-30 20:02:44 +08:00
|
|
|
|
use ZM\Annotation\Swoole\OnStart;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
use ZM\API\CQ;
|
2021-10-31 22:01:14 +08:00
|
|
|
|
use ZM\API\OneBotV11;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
use ZM\API\TuringAPI;
|
2022-04-03 01:47:38 +08:00
|
|
|
|
use ZM\Config\ZMConfig;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use ZM\ConnectionManager\ConnectionObject;
|
|
|
|
|
|
use ZM\Console\Console;
|
2022-04-10 21:46:53 +08:00
|
|
|
|
use ZM\Context\Context;
|
2020-12-14 01:24:34 +08:00
|
|
|
|
use ZM\Event\EventDispatcher;
|
2021-03-18 14:56:35 +08:00
|
|
|
|
use ZM\Exception\InterruptException;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
use ZM\Module\QQBot;
|
2021-02-24 23:37:00 +08:00
|
|
|
|
use ZM\Requests\ZMRequest;
|
2022-04-30 20:02:44 +08:00
|
|
|
|
use ZM\Utils\CommandInfoUtil;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
use ZM\Utils\MessageUtil;
|
2020-12-14 01:24:34 +08:00
|
|
|
|
use ZM\Utils\ZMUtil;
|
2020-03-02 16:14:20 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Class Hello
|
2022-04-10 00:58:07 +08:00
|
|
|
|
*
|
2020-09-29 15:07:43 +08:00
|
|
|
|
* @since 2.0
|
2020-03-02 16:14:20 +08:00
|
|
|
|
*/
|
2020-06-05 19:54:20 +08:00
|
|
|
|
class Hello
|
2020-03-02 16:14:20 +08:00
|
|
|
|
{
|
2021-03-16 01:34:17 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* 默认的图片监听路由对应目录,如需要使用可取消下面的注释,把上面的 /* 换成 /**
|
|
|
|
|
|
* @OnStart(-1)
|
|
|
|
|
|
*/
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// public function onStart() {
|
2021-03-16 01:34:17 +08:00
|
|
|
|
// \ZM\Http\RouteManager::addStaticFileRoute("/images/", \ZM\Utils\DataProvider::getWorkingDir()."/zm_data/images/");
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// }
|
2021-03-16 01:34:17 +08:00
|
|
|
|
|
2020-12-14 01:24:34 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 使用命令 .reload 发给机器人远程重载,注意将 user_id 换成你自己的 QQ
|
|
|
|
|
|
* @CQCommand(".reload",user_id=627577391)
|
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function reload()
|
|
|
|
|
|
{
|
|
|
|
|
|
ctx()->reply('重启中...');
|
2020-12-14 01:24:34 +08:00
|
|
|
|
ZMUtil::reload();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-13 17:08:13 +08:00
|
|
|
|
/**
|
2020-11-08 19:40:16 +08:00
|
|
|
|
* @CQCommand("我是谁")
|
2020-06-13 17:08:13 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function whoami()
|
|
|
|
|
|
{
|
2021-10-31 22:50:12 +08:00
|
|
|
|
$bot = ctx()->getRobot()->getLoginInfo();
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$bot_id = $bot['data']['user_id'];
|
|
|
|
|
|
$r = OneBotV11::get($bot_id);
|
2021-10-31 22:50:12 +08:00
|
|
|
|
$QQid = ctx()->getUserId();
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$nick = $r->getStrangerInfo($QQid)['data']['nickname'];
|
|
|
|
|
|
return '你是' . $nick . ',QQ号是' . $QQid;
|
2020-06-13 17:08:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-02 16:14:20 +08:00
|
|
|
|
/**
|
2020-11-08 19:40:16 +08:00
|
|
|
|
* 向机器人发送"你好啊",也可回复这句话
|
2020-06-13 11:28:31 +08:00
|
|
|
|
* @CQCommand(match="你好",alias={"你好啊","你是谁"})
|
2020-03-02 16:14:20 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function hello()
|
|
|
|
|
|
{
|
|
|
|
|
|
return '你好啊,我是由炸毛框架构建的机器人!';
|
2020-03-02 16:14:20 +08:00
|
|
|
|
}
|
2020-03-08 16:40:04 +08:00
|
|
|
|
|
2021-02-24 23:37:00 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 一个最基本的第三方 API 接口使用示例
|
|
|
|
|
|
* @CQCommand("一言")
|
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function hitokoto()
|
|
|
|
|
|
{
|
|
|
|
|
|
$api_result = ZMRequest::get('https://v1.hitokoto.cn/');
|
|
|
|
|
|
if ($api_result === false) {
|
|
|
|
|
|
return '接口请求出错,请稍后再试!';
|
|
|
|
|
|
}
|
2021-02-24 23:37:00 +08:00
|
|
|
|
$obj = json_decode($api_result, true);
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if ($obj === null) {
|
|
|
|
|
|
return '接口解析出错!可能返回了非法数据!';
|
|
|
|
|
|
}
|
|
|
|
|
|
return $obj['hitokoto'] . "\n----「" . $obj['from'] . '」';
|
2021-02-24 23:37:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-24 23:34:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 图灵机器人的内置实现,在tuling123.com申请一个apikey填入下方变量即可。
|
|
|
|
|
|
* @CQCommand(start_with="机器人",end_with="机器人",message_type="group")
|
|
|
|
|
|
* @CQMessage(message_type="private",level=1)
|
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function turingAPI()
|
|
|
|
|
|
{
|
2021-03-24 23:34:46 +08:00
|
|
|
|
$user_id = ctx()->getUserId();
|
2022-04-03 01:47:38 +08:00
|
|
|
|
$api = ZMConfig::get('global', 'custom.turing_apikey') ?? ''; // 请在这里填入你的图灵机器人的apikey
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if ($api === '') {
|
|
|
|
|
|
return false;
|
2022-03-20 16:23:07 +08:00
|
|
|
|
} // 如果没有填入apikey则此功能关闭
|
2022-04-03 01:47:38 +08:00
|
|
|
|
if (property_exists($this, '_running_annotation') && ($this->_running_annotation instanceof CQCommand)) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$msg = ctx()->getFullArg('我在!有什么事吗?');
|
2021-03-24 23:34:46 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
$msg = ctx()->getMessage();
|
|
|
|
|
|
}
|
|
|
|
|
|
ctx()->setMessage($msg);
|
|
|
|
|
|
if (MessageUtil::matchCommand($msg, ctx()->getData())->status === false) {
|
|
|
|
|
|
return TuringAPI::getTuringMsg($msg, $user_id, $api);
|
|
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
|
QQBot::getInstance()->handle(ctx()->getData(), ctx()->getCache('level') + 1);
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// 执行嵌套消息,递归层级+1
|
2022-03-15 18:05:33 +08:00
|
|
|
|
return true;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 响应at机器人的消息
|
|
|
|
|
|
* @CQBefore("message")
|
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function changeAt()
|
|
|
|
|
|
{
|
2021-03-24 23:34:46 +08:00
|
|
|
|
if (MessageUtil::isAtMe(ctx()->getMessage(), ctx()->getRobotId())) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$msg = str_replace(CQ::at(ctx()->getRobotId()), '', ctx()->getMessage());
|
|
|
|
|
|
ctx()->setMessage('机器人' . $msg);
|
2021-03-24 23:34:46 +08:00
|
|
|
|
Console::info(ctx()->getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-06 17:26:50 +08:00
|
|
|
|
/**
|
2020-11-08 19:40:16 +08:00
|
|
|
|
* 一个简单随机数的功能demo
|
|
|
|
|
|
* 问法1:随机数 1 20
|
|
|
|
|
|
* 问法2:从1到20的随机数
|
2020-05-06 17:26:50 +08:00
|
|
|
|
* @CQCommand("随机数")
|
2020-11-04 18:43:50 +08:00
|
|
|
|
* @CQCommand(pattern="*从*到*的随机数")
|
2022-05-03 10:06:57 +08:00
|
|
|
|
* @CommandArgument(name="num1",type="int",required=true)
|
|
|
|
|
|
* @CommandArgument(name="num2",type="int",required=true)
|
|
|
|
|
|
* @param mixed $num1
|
|
|
|
|
|
* @param mixed $num2
|
2020-11-03 21:02:24 +08:00
|
|
|
|
* @return string
|
2020-05-06 17:26:50 +08:00
|
|
|
|
*/
|
2022-05-03 10:06:57 +08:00
|
|
|
|
public function randNum($num1, $num2)
|
2022-03-15 18:05:33 +08:00
|
|
|
|
{
|
2022-05-03 10:06:57 +08:00
|
|
|
|
$a = min($num1, $num2);
|
|
|
|
|
|
$b = max($num1, $num2);
|
2020-05-06 17:26:50 +08:00
|
|
|
|
// 回复用户结果
|
2022-05-03 10:06:57 +08:00
|
|
|
|
return '从' . $a . '到' . $b . '的随机数是:' . mt_rand($a, $b);
|
2020-05-06 17:26:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-08 16:40:04 +08:00
|
|
|
|
/**
|
2020-04-29 15:29:56 +08:00
|
|
|
|
* 中间件测试的一个示例函数
|
|
|
|
|
|
* @RequestMapping("/httpTimer")
|
2020-11-08 19:40:16 +08:00
|
|
|
|
* @Middleware("timer")
|
2020-03-08 16:40:04 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function timer()
|
|
|
|
|
|
{
|
|
|
|
|
|
return 'This page is used as testing TimerMiddleware! Do not use it in production.';
|
2020-03-08 16:40:04 +08:00
|
|
|
|
}
|
2020-03-08 17:05:12 +08:00
|
|
|
|
|
2020-05-06 17:26:50 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 默认示例页面
|
|
|
|
|
|
* @RequestMapping("/index")
|
2020-05-10 14:11:32 +08:00
|
|
|
|
* @RequestMapping("/")
|
2020-05-06 17:26:50 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function index()
|
|
|
|
|
|
{
|
|
|
|
|
|
return 'Hello Zhamao!';
|
2020-05-06 17:26:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-29 15:07:43 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 使用自定义参数的路由参数
|
|
|
|
|
|
* @RequestMapping("/whoami/{name}")
|
2022-04-10 00:58:07 +08:00
|
|
|
|
*
|
2022-04-02 23:37:22 +08:00
|
|
|
|
* @param array $param 参数
|
|
|
|
|
|
* @return string 返回的 HTML Body
|
2020-09-29 15:07:43 +08:00
|
|
|
|
*/
|
2022-04-02 23:37:22 +08:00
|
|
|
|
public function paramGet(array $param = []): string
|
2022-03-15 18:05:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
return 'Hello, ' . $param['name'];
|
2020-09-29 15:07:43 +08:00
|
|
|
|
}
|
2020-05-06 17:26:50 +08:00
|
|
|
|
|
2020-11-08 19:40:16 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 在机器人连接后向终端输出信息
|
2021-01-02 13:15:50 +08:00
|
|
|
|
* @OnOpenEvent("qq")
|
2022-04-10 00:58:07 +08:00
|
|
|
|
*
|
2022-04-02 23:37:22 +08:00
|
|
|
|
* @param ConnectionObject $conn WebSocket 连接对象
|
2020-11-08 19:40:16 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function onConnect(ConnectionObject $conn)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console::info('机器人 ' . $conn->getOption('connect_id') . ' 已连接!');
|
2020-11-08 19:40:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 在机器人断开连接后向终端输出信息
|
2021-01-02 13:15:50 +08:00
|
|
|
|
* @OnCloseEvent("qq")
|
2020-11-08 19:40:16 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function onDisconnect(ConnectionObject $conn)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console::info('机器人 ' . $conn->getOption('connect_id') . ' 已断开连接!');
|
2020-11-08 19:40:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-14 01:24:34 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 阻止 Chrome 自动请求 /favicon.ico 导致的多条请求并发和干扰
|
2021-01-02 13:15:50 +08:00
|
|
|
|
* @OnRequestEvent(rule="ctx()->getRequest()->server['request_uri'] == '/favicon.ico'",level=200)
|
2022-04-10 00:58:07 +08:00
|
|
|
|
*
|
2021-03-18 14:56:35 +08:00
|
|
|
|
* @throws InterruptException
|
2020-12-14 01:24:34 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function onRequest()
|
|
|
|
|
|
{
|
2020-12-14 01:24:34 +08:00
|
|
|
|
EventDispatcher::interrupt();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-08 17:05:12 +08:00
|
|
|
|
/**
|
2020-04-29 15:29:56 +08:00
|
|
|
|
* 框架会默认关闭未知的WebSocket链接,因为这个绑定的事件,你可以根据你自己的需求进行修改
|
2021-01-02 13:15:50 +08:00
|
|
|
|
* @OnOpenEvent("default")
|
2020-03-08 17:05:12 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public function closeUnknownConn()
|
|
|
|
|
|
{
|
|
|
|
|
|
Console::info('Unknown connection , I will close it.');
|
2021-07-09 10:43:00 +08:00
|
|
|
|
server()->disconnect(ctx()->getConnection()->getFd());
|
2020-03-08 17:05:12 +08:00
|
|
|
|
}
|
2022-03-21 04:34:08 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 输出帮助信息
|
|
|
|
|
|
*
|
|
|
|
|
|
* @CQCommand("帮助")
|
|
|
|
|
|
*/
|
|
|
|
|
|
#[CQCommand('帮助')]
|
|
|
|
|
|
public function help(): string
|
|
|
|
|
|
{
|
2022-04-30 20:02:44 +08:00
|
|
|
|
$util = resolve(CommandInfoUtil::class);
|
|
|
|
|
|
$commands = $util->get();
|
2022-05-03 16:13:13 +08:00
|
|
|
|
$helps = array_map(static function ($command) use ($util) {
|
|
|
|
|
|
return $util->getHelp($command['id']);
|
|
|
|
|
|
}, $commands);
|
2022-03-21 04:34:08 +08:00
|
|
|
|
array_unshift($helps, '帮助:');
|
|
|
|
|
|
return implode("\n", $helps);
|
|
|
|
|
|
}
|
2022-04-04 00:14:56 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @CQCommand("proxy")
|
|
|
|
|
|
*/
|
|
|
|
|
|
#[CQCommand('proxy')]
|
|
|
|
|
|
public function proxy()
|
|
|
|
|
|
{
|
|
|
|
|
|
bot()->all()->allGroups()->sendGroupMsg(0, ctx()->getMessage());
|
|
|
|
|
|
}
|
2022-04-05 01:07:51 +08:00
|
|
|
|
|
|
|
|
|
|
/*
|
2022-04-10 21:46:53 +08:00
|
|
|
|
* 欢迎来到容器时代
|
2022-04-05 01:07:51 +08:00
|
|
|
|
*
|
2022-04-10 21:46:53 +08:00
|
|
|
|
* @param Context $context 通过依赖注入实现的
|
|
|
|
|
|
*
|
|
|
|
|
|
* @CQCommand("容器你好")
|
2022-04-05 01:07:51 +08:00
|
|
|
|
*/
|
2022-04-10 21:46:53 +08:00
|
|
|
|
public function welcomeToContainerAge(Context $context)
|
2022-04-05 01:07:51 +08:00
|
|
|
|
{
|
2022-04-10 21:46:53 +08:00
|
|
|
|
$context->reply('欢迎来到容器时代');
|
2022-04-05 01:07:51 +08:00
|
|
|
|
}
|
2020-03-29 16:29:02 +08:00
|
|
|
|
}
|