怎么写一个脚本,指定操作后截图指定元素发送到企业微信机器人

已存档
4ywhpkay0c · 26 天前

回答

Hamibot AI
1
· 26 天前

文档地址 https://docs.hamibot.com/

// 1. 企业微信机器人webhook地址
const webhookUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的密钥";

// 2. 定位目标元素并截图
function captureElement() {
    // 使用uiSelector定位元素,示例选择id为target的元素
    var target = uiSelector().id("target").findOne();
    if (!target) {
        toast("未找到目标元素");
        return;
    }
    
    // 获取元素坐标和尺寸
    var bounds = target.bounds();
    var image = capture(bounds.left, bounds.top, bounds.width(), bounds.height());
    
    // 转换为base64(需使用Hamibot的images模块)
    var base64 = images.toBase64(image);
    sendToWeCom(base64);
}

// 3. 发送图文消息到企业微信
function sendToWeCom(base64) {
    var payload = {
        "msgtype": "markdown",
        "markdown": {
            "content": "检测到目标元素变化:",
            "mentioned_list": ["@all"]
        },
        "image": {
            "base64": base64
        }
    };
    
    var res = http.post(webhookUrl, JSON.stringify(payload));
    if(res.statusCode == 200) {
        toast("发送成功");
    } else {
        toast("发送失败: " + res.body.string());
    }
}

// 4. 执行操作后触发截图
// 示例:点击某个按钮后截图
uiSelector().text("开始检测").findOne().click();
sleep(3000); // 等待界面变化
captureElement();

实现原理

  1. 使用uiSelector()定位需要截图的界面元素
  2. 通过capture(x,y,width,height)截取指定区域
  3. 使用images.toBase64()将图片转为base64格式
  4. 通过HTTP POST请求发送到企业微信机器人webhook

注意

  1. 需要开启无障碍服务和截图权限
  2. 企业微信机器人需先在管理后台创建并获取webhook地址
  3. 实际使用需替换以下参数:
    • 元素定位条件(id/target)
    • 企业微信webhook地址
    • 消息内容模板
  4. 需处理界面元素加载延迟问题,建议配合waitForActivity()使用
ℹ️ 本回答由 AI 生成,内容仅供参考