mirror of
https://github.com/m5stack/ESP-Claw.git
synced 2026-05-20 11:51:49 -07:00
feat: add lua module call capability
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
-- QQ send-file demo.
|
||||
-- Optional args:
|
||||
-- chat_id: target QQ chat, e.g. "c2c:<openid>" or "group:<group_openid>"
|
||||
-- path: local file path for qq_send_file; if omitted a demo file is created
|
||||
-- caption: optional caption for qq_send_file
|
||||
-- If chat_id is omitted, capability.call() may inherit it from the current QQ runtime context.
|
||||
local capability = require("capability")
|
||||
local storage = require("storage")
|
||||
|
||||
local a = type(args) == "table" and args or {}
|
||||
|
||||
local function string_arg(key, default)
|
||||
local value = a[key]
|
||||
if type(value) == "string" and value ~= "" then
|
||||
return value
|
||||
end
|
||||
return default
|
||||
end
|
||||
|
||||
local function build_call_opts()
|
||||
local opts = {
|
||||
channel = "qq",
|
||||
source_cap = "lua_builtin_demo",
|
||||
}
|
||||
|
||||
if type(a.session_id) == "string" and a.session_id ~= "" then
|
||||
opts.session_id = a.session_id
|
||||
end
|
||||
if type(a.chat_id) == "string" and a.chat_id ~= "" then
|
||||
opts.chat_id = a.chat_id
|
||||
end
|
||||
|
||||
return opts
|
||||
end
|
||||
|
||||
local function ensure_demo_file()
|
||||
local root = storage.get_root_dir()
|
||||
local demo_dir = storage.join_path(root, "demo")
|
||||
local demo_path = storage.join_path(demo_dir, "qq_cap_send_file_demo.txt")
|
||||
local chat_id = string_arg("chat_id", "<inherit-from-context>")
|
||||
local caption = string_arg("caption", "file from lua qq capability demo")
|
||||
local lines = {
|
||||
"ESP-Claw QQ send-file demo file",
|
||||
"chat_id=" .. chat_id,
|
||||
"caption=" .. caption,
|
||||
"This file is generated by lua_module_call_capability/lua_scripts/qq_cap_send_file.lua",
|
||||
}
|
||||
|
||||
storage.mkdir(demo_dir)
|
||||
storage.write_file(demo_path, table.concat(lines, "\n") .. "\n")
|
||||
return demo_path
|
||||
end
|
||||
|
||||
local function run()
|
||||
local chat_id = string_arg("chat_id", nil)
|
||||
local file_path = string_arg("path", nil)
|
||||
local payload = {
|
||||
caption = string_arg("caption", "file from lua qq capability demo"),
|
||||
}
|
||||
local ok, out, err
|
||||
|
||||
if not file_path then
|
||||
file_path = ensure_demo_file()
|
||||
print("[qq_cap_send_file] no args.path provided, generated demo file: " .. file_path)
|
||||
end
|
||||
if not storage.exists(file_path) then
|
||||
error("[qq_cap_send_file] file does not exist: " .. tostring(file_path))
|
||||
end
|
||||
|
||||
payload.path = file_path
|
||||
if chat_id then
|
||||
payload.chat_id = chat_id
|
||||
end
|
||||
|
||||
print("[qq_cap_send_file] sending QQ file...")
|
||||
ok, out, err = capability.call("qq_send_file", payload, build_call_opts())
|
||||
print(string.format(
|
||||
"[qq_cap_send_file] result ok=%s out=%s err=%s",
|
||||
tostring(ok), tostring(out), tostring(err)))
|
||||
|
||||
if not ok then
|
||||
error(string.format("qq_send_file failed: %s", tostring(err or out)))
|
||||
end
|
||||
|
||||
print("[qq_cap_send_file] done")
|
||||
end
|
||||
|
||||
local ok, err = xpcall(run, debug.traceback)
|
||||
if not ok then
|
||||
error(err)
|
||||
end
|
||||
@@ -0,0 +1,69 @@
|
||||
-- QQ send-image demo.
|
||||
-- Optional args:
|
||||
-- chat_id: target QQ chat, e.g. "c2c:<openid>" or "group:<group_openid>"
|
||||
-- path: local image path for qq_send_image
|
||||
-- caption: optional caption for qq_send_image
|
||||
-- If chat_id is omitted, capability.call() may inherit it from the current QQ runtime context.
|
||||
local capability = require("capability")
|
||||
local storage = require("storage")
|
||||
|
||||
local a = type(args) == "table" and args or {}
|
||||
|
||||
local function string_arg(key, default)
|
||||
local value = a[key]
|
||||
if type(value) == "string" and value ~= "" then
|
||||
return value
|
||||
end
|
||||
return default
|
||||
end
|
||||
|
||||
local function build_call_opts()
|
||||
local opts = {
|
||||
channel = "qq",
|
||||
source_cap = "lua_builtin_demo",
|
||||
}
|
||||
|
||||
if type(a.session_id) == "string" and a.session_id ~= "" then
|
||||
opts.session_id = a.session_id
|
||||
end
|
||||
if type(a.chat_id) == "string" and a.chat_id ~= "" then
|
||||
opts.chat_id = a.chat_id
|
||||
end
|
||||
|
||||
return opts
|
||||
end
|
||||
|
||||
local function run()
|
||||
local chat_id = string_arg("chat_id", nil)
|
||||
local image_path = string_arg("path", "/fatfs/static/ESP-Claw.png")
|
||||
local payload = {
|
||||
caption = string_arg("caption", "image from lua qq capability demo"),
|
||||
}
|
||||
local ok, out, err
|
||||
|
||||
if not storage.exists(image_path) then
|
||||
error("[qq_cap_send_image] file does not exist: " .. tostring(image_path))
|
||||
end
|
||||
|
||||
payload.path = image_path
|
||||
if chat_id then
|
||||
payload.chat_id = chat_id
|
||||
end
|
||||
|
||||
print("[qq_cap_send_image] sending QQ image...")
|
||||
ok, out, err = capability.call("qq_send_image", payload, build_call_opts())
|
||||
print(string.format(
|
||||
"[qq_cap_send_image] result ok=%s out=%s err=%s",
|
||||
tostring(ok), tostring(out), tostring(err)))
|
||||
|
||||
if not ok then
|
||||
error(string.format("qq_send_image failed: %s", tostring(err or out)))
|
||||
end
|
||||
|
||||
print("[qq_cap_send_image] done")
|
||||
end
|
||||
|
||||
local ok, err = xpcall(run, debug.traceback)
|
||||
if not ok then
|
||||
error(err)
|
||||
end
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
-- QQ send-message demo.
|
||||
-- Optional args:
|
||||
-- chat_id: target QQ chat, e.g. "c2c:<openid>" or "group:<group_openid>"
|
||||
-- message: text to send with qq_send_message
|
||||
-- If chat_id is omitted, capability.call() may inherit it from the current QQ runtime context.
|
||||
local capability = require("capability")
|
||||
|
||||
local a = type(args) == "table" and args or {}
|
||||
|
||||
local function string_arg(key, default)
|
||||
local value = a[key]
|
||||
if type(value) == "string" and value ~= "" then
|
||||
return value
|
||||
end
|
||||
return default
|
||||
end
|
||||
|
||||
local function build_call_opts()
|
||||
local opts = {
|
||||
channel = "qq",
|
||||
source_cap = "lua_builtin_demo",
|
||||
}
|
||||
|
||||
if type(a.session_id) == "string" and a.session_id ~= "" then
|
||||
opts.session_id = a.session_id
|
||||
end
|
||||
if type(a.chat_id) == "string" and a.chat_id ~= "" then
|
||||
opts.chat_id = a.chat_id
|
||||
end
|
||||
|
||||
return opts
|
||||
end
|
||||
|
||||
local function run()
|
||||
local chat_id = string_arg("chat_id", nil)
|
||||
local payload = {
|
||||
message = string_arg("message", "hello from lua qq capability demo"),
|
||||
}
|
||||
local ok, out, err
|
||||
|
||||
if chat_id then
|
||||
payload.chat_id = chat_id
|
||||
end
|
||||
|
||||
print("[qq_cap_send_message] sending QQ message...")
|
||||
ok, out, err = capability.call("qq_send_message", payload, build_call_opts())
|
||||
print(string.format(
|
||||
"[qq_cap_send_message] result ok=%s out=%s err=%s",
|
||||
tostring(ok), tostring(out), tostring(err)))
|
||||
|
||||
if not ok then
|
||||
error(string.format("qq_send_message failed: %s", tostring(err or out)))
|
||||
end
|
||||
|
||||
print("[qq_cap_send_message] done")
|
||||
end
|
||||
|
||||
local ok, err = xpcall(run, debug.traceback)
|
||||
if not ok then
|
||||
error(err)
|
||||
end
|
||||
Reference in New Issue
Block a user