开发教程之写一个定时给群发消息的任务功能
模块自身是拥有定时任务模块的
只是今天以另外一种方式讲解并教大家开发 一个定时给群 发消息的功能
-----
第一步(方式1 访问URL启动任务)
首先我们在 bot/web/ 目录新建一个文件名为:test.php 内容如下:
<?php
namespace bot\web;
use app\model; #模型
use bot\mdb; #模型2
use think\facade\Db; #数据库
use think\facade\Cache; #缓存
use support\Redis; #redis
use Webman\RedisQueue\Client as RQamsg; #异步队列
use Webman\RedisQueue\Redis as RQmsg; #同步队列
use GuzzleHttp\Pool;
use GuzzleHttp\Client as Guzz_Client;
use GuzzleHttp\Psr7\Request as Guzz_Request;
use GuzzleHttp\Promise as Guzz_Promise;
use support\Request;
class test{
//访问将添加任务
public function index(Request $request){
$title = "crontest";//任务名称 不要重复
$task = Db::table("sys_crontab")->where("title",$title)->find(); //在数据库中查询该任务名是否存在
$target = 'bot\\task\\task123'; //对应任务文件路径为:bot/task/task123.php
//判断任务文件是否存在
if(!class_exists($target)){
return response("错误{$target} 任务文件不存在");
}
if(empty($task)){
//不存在添加
$param = [
'method' => 'crontabCreate',
'args' => [
'title' => $title,
'type' =>2,
'rule' => "0 */1 * * * *",//cron规则 1分钟1次
'target' => $target,
'status' => 1,
'remark' => $title,
]
];
$result = \yzh52521\Task\Client::instance()->request($param);
return response("定时任务已启动");
}else{
//存在修改
$param = [
'method' => 'crontabUpdate',
'args' => [
'id' => $task['id'],//任务id
'type' =>2,
'rule' => "0 */2 * * * *",//cron规则 2分钟1次
'target' => $target,
'status' => 1,
'remark' => $title,
]
];
$result = \yzh52521\Task\Client::instance()->request($param);
return response("定时任务已修改");
}
}
}其中的rule就是cron定时规则:0 */1 * * * * 代表每1分钟执行1次
规则说明:
每分钟的第5秒执行1次
每5秒执行1次
每小时的第25分00秒执行1次
每10分钟执行1次
每日的8点00分00秒执行1次(0-23)
每2小时执行1次
每3日执行1次
第二步
在bot/task/目录新建一个文件名为:task123.php 内容如下:
自己修改里面的内容
<?php
namespace bot\task;
use app\model; #模型
use bot\mdb; #模型2
use think\facade\Db; #数据库
use think\facade\Cache; #缓存
use support\Redis; #redis
use Webman\RedisQueue\Client as RQamsg; #异步队列
use Webman\RedisQueue\Redis as RQmsg; #同步队列
use TNTma\TronWeb\Address;
use TNTma\TronWeb\Account;
use TNTma\TronWeb\Tron;
use GuzzleHttp\Pool;
use GuzzleHttp\Client as Guzz_Client;
use GuzzleHttp\Psr7\Request as Guzz_Request;
use GuzzleHttp\Promise as Guzz_Promise;
class task123{
public function execute(): string{
$botuser = "SwapTRX8bot";//你的机器人用户名(确保已在后台添加该机器人)
$type = 1;//1代表文本消息 2图片消息 3视频消息
$photo = "";//图片地址
$video = "";//视频地址
$id = "-1001810783822";//目标群id
$value="这是消息内容";//消息内容
$queueData["autodel"] = 30;//30秒后删除消息 0不删除
$bot = model\bot_list::where("plugin","tgbot")->where("API_BOT",$botuser)->where("del",0)->cache("tgbot_{$botuser}")->find();
if(empty($bot)){
dump("数据库中 机器人{$botuser} 不存在");
return "ok";
}
$value = urlencode($value);
if($type == 1){
$url = "/sendMessage?chat_id={$id}&text={$value}";
}else if($type == 2){
$url = "/sendPhoto?chat_id={$id}&caption={$value}&photo={$photo}";
}else if($type == 3){
$url = "/sendVideo?chat_id={$id}&caption={$value}&video={$video}";
}else{
return "ok";
}
$queueData["type"] = "url";
$queueData["url"]=$bot["API_URL"].$bot["API_TOKEN"].$url;
RQmsg::send("TG_queue", $queueData);
return "ok";
}
}里面的内容 我都有注释自己修改吧
-------这样就写好了 (如果不是调试模式启动的框架 请写完后重启机器人框架)开发代码时务必使用调试模式启动框架,否则修改代码后都需要重启框架很麻烦的
接下来浏览器访问:http://你得服务器IP:8686/web/test/index
访问这个地址的目的是执行 bot/web/test.php 这个文件里面的index方法(添加定时任务 - 就这么一个作用) 执行1次后这个文件就可以删除了没啥用,你也可写到机器人得按钮,消息等下面 用按钮消息之类的添加定时任务。
好了接下来就会自动给对应的群发送消息了! 你可以借此教程开发各种定时任务 定时功能,自己发挥想象 遇到问题可以联系:@gd801 免费帮助你解决问题
上面我们说到的是通过访问url 来启动任务 下面再写一种 通过机器人命令来启动任务
第一步之(方式2)
在目录:bot/api_command 新建一个文件名为:test.php
<?php
namespace bot\api_command;
use app\model; #模型
use bot\mdb; #模型2
use think\facade\Db; #数据库
use think\facade\Cache; #缓存
use support\Redis; #redis
use Webman\RedisQueue\Client as RQamsg; #异步队列
use Webman\RedisQueue\Redis as RQmsg; #同步队列
//对应类文件
use plugin\tgbot\app\controller\Base;
use plugin\tgbot\app\controller\Template;
use GuzzleHttp\Pool;
use GuzzleHttp\Client as Guzz_Client;
use GuzzleHttp\Psr7\Request as Guzz_Request;
use GuzzleHttp\Promise as Guzz_Promise;
class test {
/**
* 【参数解答】
* $message['bot'] = 机器人配置信息
* $message['msgId'] = 聊天消息唯一ID
*
* $message['chatType'] = 聊天类型 群=supergroup 私聊=private
* $message['chatId'] = 聊天窗口ID
* $message['chatUser'] = 聊天窗口用户名(@xxx)
* $message['chatName'] = 聊天窗口标题 - 群组=群名称 ,用户=用户昵称
*
* $message['formId'] = 发消息的人ID
* $message['formUser'] = 发消息的人用户名
* $message['formName'] = 发消息的人昵称
* $message['fromVip'] = 发消息的人是否是电报VIP 0否 1是
* $message['fromLang'] = 发消息的人电报客户端语言(多语言需要)
*
* $message['text'] = 消息文本内容
* $message['time'] = 消息到达-服务器时间戳
* $message['tgTime'] = 消息到达-电报官方时间戳
*
* $message['photo'] = 有图片时为图片数据信息-自己打印查看 没有为0
* $message['document'] = 有文件时为文件数据信息-自己打印查看 没有为0
* $message['video'] = 有视频时为视频数据信息-自己打印查看 没有为0
* $message['gif'] = 有动画图片时为动画数据信息-自己打印查看 没有为0
*
* $message['isHuiFu'] = 是否属回复消息?1是 0否 :↓ 属于回复消息时才有以下参数
* $message['HuiFu']['msgId'] = 被回复的消息ID
* $message['HuiFu']['isBot'] = 被回复的目标是否为机器人 1是0否
* $message['HuiFu']['botUser']= 被回复的目标是机器人时才有效,返回机器人用户名
* $message['HuiFu']['toId'] = 被回复消息的人ID
* $message['HuiFu']['toUser'] = 被回复消息的人用户名
* $message['HuiFu']['toVip'] = 被回复消息的人是否是电报VIP 0否 1是
*
* $ret支持回调参数:sendText(文本消息) sendPhoto(发送照片) gif(发送动图) anniu(消息按钮) [ jianpan(回复键盘) && jianpanText(文字消息) || jianpanPhoto(照片消息)]
* @param $id
* @return array
*/
#默认执行函数
public function index($message){
$ret['key']=pathinfo(basename(__FILE__), PATHINFO_FILENAME);
$ret['level']=100; //优先级 (当存在多个模块都返回了文本消息或按钮时生效)数值大排上面 ,数值小排下面
if(preg_match('/\/(\w+)\s*(.*)/i', $message['text'], $com)){
if(count($com) != 3){
return $ret;
}
}else{
return $ret;
}
$type = $com[1]; //命令
$value = $com[2]; //参数
#多机器人群内同命令时 只处理被@的机器人命令
if(!empty($value)){
if($value[0] == "@"){
if(substr($value, 1) != $message['bot']['API_BOT']){
return $ret;
}
}
}
#判断命令是否机器人admin才能用
if(is_admin($type)){
if($message['formId'] != $message['bot']['Admin']){
return $ret;
}
}
#-----------上面代码固定不要修改 level 视情况调整改----------------
//这里检查一下 只有机器人管理者才有资格使用该文件里面的命令
if($message['formId'] != $message['bot']['Admin']){
$ret['sendText'] = "抱歉 你不是本机器人管理员 - 无法使用该指令";
return $ret;
}
switch ($type) {
default:
break;
case 'task1'://命令
$title = "crontest";//任务名称 不要重复
$task = Db::table("sys_crontab")->where("title",$title)->find(); //在数据库中查询该任务名是否存在
$target = 'bot\\task\\task123'; //对应任务文件路径为:bot/task/task123.php
//判断任务文件是否存在
if(!class_exists($target)){
$ret['sendText'] = "错误{$target} 任务文件不存在";
}else if(empty($task)){
//不存在添加
$param = [
'method' => 'crontabCreate',
'args' => [
'title' => $title,
'type' =>2,
'rule' => "*/1 * * * *",//cron规则 1分钟1次
'target' => $target,
'status' => 1,
'remark' => $title,
]
];
$result = \yzh52521\Task\Client::instance()->request($param);
$ret['sendText'] = "定时任务已启动";
}else{
//存在修改
$param = [
'method' => 'crontabUpdate',
'args' => [
'id' => $task['id'],//任务id
'type' =>2,
'rule' => "*/2 * * * *",//cron规则 2分钟1次
'target' => $target,
'status' => 1,
'remark' => $title,
]
];
$result = \yzh52521\Task\Client::instance()->request($param);
$ret['sendText'] = "定时任务已修改";
}
//按钮
$ret['anniu'] = [
[
[
"text" => "按钮名称1",
"callback_data" => "按钮事件_1"
]
],
[
[
"text" => "按钮名称2",
"callback_data" => "按钮事件_2"
]
],
];
break;
//我们将这个命令定义为删除任务
case 'task2':
$title = "crontest";//任务名称 不要重复
$task = Db::table("sys_crontab")->where("title",$title)->find(); //在数据库中查询该任务名是否存在
if(empty($task)){
$ret['sendText'] = "没有找到任务{$title},删除失败";
return $ret;
}
$param = [
'method' => 'crontabDelete',
'args' => [
'id' =>$task['id']
]
];
$result = \yzh52521\Task\Client::instance()->request($param);
$ret['sendText'] = "删除任务{$title}ID:{$task['id']} 成功!";
break;
}
return $ret;
}
}然后重启框架(调试模式启动的不用)
用管理号给机器人发送命令:/task1 启动任务 发送:/task2 删除任务
这种模式就不需要用上面的:bot/web/test.php 了 也不需要访问 url 启动任务了 省略掉第一步!
本次文档教程的文件我 上传了,可以直接下载后上传到机器人框架目录 解压后重启框架自行测试 修改