Files
autman-plugins/plugins/autman_cron_test.js
T

132 lines
5.7 KiB
JavaScript

//[open_source: true]
//[create_at: 2026-05-23 00:00:00]
//[title: Autman 定时运行测试]
//[author: Hermes]
//[service: BOSS]
//[class: 工具类]
// [version: 1.0.0]
// [platform: web,qq,wx,tg,tb,fs,we]
// [public: false]
// [price: 0]
// [disable: false]
// [description: 用于验证 Autman 当前版本定时插件入口、运行上下文和可用参数。论坛与现有插件约定使用 cron 头注;手动发送“定时测试”可交互返回诊断参数;定时每 5 分钟触发一次并打印 console.log。]
// [rule: ^(定时测试|cron测试|task测试|autman定时测试)(.*)$]
// [admin: true]
// [priority: 1000]
// [cron: */5 * * * *]
var PLUGIN_NAME = "Autman 定时运行测试"
var PLUGIN_VERSION = "1.0.0"
function safeCall(label, fn) {
try {
if (typeof fn !== "function") return { ok: false, value: "<not function>" }
var v = fn()
if (v === undefined) return { ok: true, value: "<undefined>" }
if (v === null) return { ok: true, value: "<null>" }
return { ok: true, value: String(v) }
} catch (e) {
return { ok: false, value: "ERROR: " + (e && e.message ? e.message : String(e)) }
}
}
function hasFn(name) {
try { return typeof this[name] === "function" } catch (e) { return false }
}
function valueOf(name) {
try {
var v = this[name]
if (typeof v === "function") return "<function>"
if (v === undefined) return "<undefined>"
if (v === null) return "<null>"
if (typeof v === "object") return JSON.stringify(v)
return String(v)
} catch (e) {
return "ERROR: " + (e && e.message ? e.message : String(e))
}
}
function callInfo(param) {
return safeCall("call(" + param + ")", function () {
if (typeof call !== "function") return "<call not function>"
var f = call(param)
if (typeof f !== "function") return "<call result not function>"
if (param === "timeFormat") return f("yyyy-MM-dd hh:mm:ss")
return f()
}).value
}
function getContentCompat() {
var r = safeCall("GetContent", function () { return GetContent() })
if (r.ok && r.value !== "<not function>" && r.value !== "<undefined>" && r.value !== "<null>") return r.value
r = safeCall("getContent", function () { return getContent() })
if (r.ok && r.value !== "<not function>" && r.value !== "<undefined>" && r.value !== "<null>") return r.value
return ""
}
function detectImType() {
var r = safeCall("ImType", function () { return ImType() })
if (r.ok && r.value !== "<not function>" && r.value !== "<undefined>" && r.value !== "<null>" && r.value !== "") return r.value
r = safeCall("GetImType", function () { return GetImType() })
if (r.ok && r.value !== "<not function>" && r.value !== "<undefined>" && r.value !== "<null>" && r.value !== "") return r.value
return ""
}
function isCronRun(content, imType) {
var t = String(imType || "").toLowerCase()
if (t === "cron" || t === "task") return true
// 只作兜底:部分版本定时触发没有消息内容。
return !content
}
function replyText(text) {
try { if (typeof sendText === "function") { sendText(text); return } } catch (e) {}
try { if (typeof sender !== "undefined" && sender && typeof sender.reply === "function") { sender.reply(text); return } } catch (e2) {}
try { if (typeof Sender !== "undefined" && Sender && typeof Sender.Reply === "function") { Sender.Reply(text); return } } catch (e3) {}
try { if (typeof console !== "undefined" && console && typeof console.log === "function") console.log(text) } catch (e4) {}
}
function buildReport() {
var content = getContentCompat()
var imType = detectImType()
var cronRun = isCronRun(content, imType)
var lines = []
lines.push("【" + PLUGIN_NAME + " v" + PLUGIN_VERSION + "】")
lines.push("触发类型: " + (cronRun ? "定时触发" : "交互触发"))
lines.push("调度头注: // [cron: */5 * * * *]")
lines.push("说明: 当前论坛/仓库约定使用 cron;task 仅作为兼容诊断值检测。")
lines.push("")
lines.push("-- 基础上下文 --")
lines.push("content: " + JSON.stringify(content))
lines.push("ImType/GetImType: " + JSON.stringify(imType))
lines.push("GetUserID: " + safeCall("GetUserID", function () { return GetUserID() }).value)
lines.push("GetChatID: " + safeCall("GetChatID", function () { return GetChatID() }).value)
lines.push("isAdmin: " + safeCall("isAdmin", function () { return isAdmin() }).value)
lines.push("")
lines.push("-- call() 信息 --")
lines.push("name: " + callInfo("name"))
lines.push("version: " + callInfo("version"))
lines.push("port: " + callInfo("port"))
lines.push("machineId: " + callInfo("machineId"))
lines.push("time: " + callInfo("timeFormat"))
lines.push("")
lines.push("-- 函数存在性 --")
lines.push("cron API: " + (hasFn("cron") ? "function" : valueOf("cron")))
lines.push("GetContent: " + (hasFn("GetContent") ? "function" : valueOf("GetContent")))
lines.push("getContent: " + (hasFn("getContent") ? "function" : valueOf("getContent")))
lines.push("ImType: " + (hasFn("ImType") ? "function" : valueOf("ImType")))
lines.push("GetImType: " + (hasFn("GetImType") ? "function" : valueOf("GetImType")))
lines.push("sendText: " + (hasFn("sendText") ? "function" : valueOf("sendText")))
return lines.join("\n")
}
function main() {
var report = buildReport()
// 关键:交互运行或定时运行时,都先写 console.log,方便在 Autman 日志里确认 main 是否被调用及上下文参数。
try { if (typeof console !== "undefined" && console && typeof console.log === "function") console.log(report) } catch (e) {}
replyText(report)
}
main()