下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:1133
这个Auto.js脚本实现了微信批量群发的主要功能,包括联系人/群组选择、消息内容编辑、定时发送等。使用时需要开启Auto.js的无障碍服务,并确保微信在前台运行。代码包含详细的注释和错误处理,可以根据实际需求进行修改扩展。
// 微信批量群发工具 v1.0
// 需要Auto.js 4.1.1及以上版本支持
"ui";
// 主界面布局
ui.layout(
<vertical padding="16">
<text text="微信批量群发工具" textSize="24sp" gravity="center"/>
<horizontal>
<input id="content" hint="请输入消息内容" layout_weight="1"/>
<button id="selectContent" text="选择文件"/>
</horizontal>
<horizontal>
<input id="interval" inputType="number" hint="发送间隔(毫秒)" text="2000"/>
<checkbox id="withImage" text="包含图片"/>
</horizontal>
<horizontal>
<button id="selectContacts" text="选择联系人"/>
<button id="selectGroups" text="选择群组"/>
</horizontal>
<list id="targetList">
<text text="{{this}}"/>
</list>
<button id="start" text="开始发送" bg="#4CAF50"/>
<button id="stop" text="停止" bg="#F44336"/>
<horizontal>
<text text="进度:"/>
<text id="progress" text="0/0"/>
</horizontal>
<log id="log" textSize="10sp"/>
</vertical>
);
// 全局变量
let running = false;
let targets = [];
let messageContent = "";
let imagePath = null;
let currentIndex = 0;
// 选择消息内容文件
ui.selectContent.click(() => {
let path = files.chooseFile();
if(path){
messageContent = files.read(path);
ui.content.setText("已加载文件内容");
}
});
// 选择联系人
ui.selectContacts.click(() => {
let contacts = getContacts();
dialogs.multiChoice("选择联系人", contacts, [])
.then(inds => {
targets = targets.concat(inds.map(i => contacts[i]));
updateTargetList();
});
});
// 选择群组
ui.selectGroups.click(() => {
let groups = getGroups();
dialogs.multiChoice("选择群组", groups, [])
.then(inds => {
targets = targets.concat(inds.map(i => groups[i]));
updateTargetList();
});
});
// 更新目标列表
function updateTargetList(){
ui.targetList.setDataSource(targets);
}
// 获取联系人列表
function getContacts(){
launchApp("微信");
waitForPackage("com.tencent.mm");
sleep(2000);
// 模拟点击通讯录
click(device.width - 100, 100);
sleep(1000);
// 滚动获取所有联系人
let contacts = [];
let lastY = 0;
while(true){
let objs = className("android.widget.TextView").find();
for(let obj of objs){
let text = obj.text();
if(text && !contacts.includes(text) && text.length > 1){
contacts.push(text);
}
}
swipe(device.width/2, device.height*0.8, device.width/2, device.height*0.2, 500);
sleep(500);
if(lastY === obj.bounds().top) break;
lastY = obj.bounds().top;
}
return contacts;
}
// 获取群组列表
function getGroups(){
launchApp("微信");
waitForPackage("com.tencent.mm");
sleep(2000);
// 模拟点击通讯录-群聊
click(device.width - 100, 100);
sleep(1000);
click(text("群聊").findOne().bounds().centerX(), text("群聊").findOne().bounds().centerY());
sleep(1000);
// 滚动获取所有群组
let groups = [];
let lastY = 0;
while(true){
let objs = className("android.widget.TextView").find();
for(let obj of objs){
let text = obj.text();
if(text && !groups.includes(text) && text.length > 1){
groups.push(text);
}
}
swipe(device.width/2, device.height*0.8, device.width/2, device.height*0.2, 500);
sleep(500);
if(lastY === obj.bounds().top) break;
lastY = obj.bounds().top;
}
return groups;
}
// 发送消息给指定目标
function sendMessage(target){
launchApp("微信");
waitForPackage("com.tencent.mm");
sleep(2000);
// 搜索目标
click(desc("搜索").findOne().bounds().centerX(), desc("搜索").findOne().bounds().centerY());
sleep(1000);
setText(target);
sleep(2000);
// 点击搜索结果
let targetObj = text(target).findOne();
if(!targetObj){
log("未找到目标: " + target);
return false;
}
click(targetObj.bounds().centerX(), targetObj.bounds().centerY());
sleep(2000);
// 输入消息
let input = className("android.widget.EditText").findOne();
if(!input){
log("未找到输入框");
return false;
}
setText(messageContent);
sleep(500);
// 发送图片
if(ui.withImage.isChecked() && imagePath){
click(desc("更多功能按钮").findOne().bounds().centerX(), desc("更多功能按钮").findOne().bounds().centerY());
sleep(500);
click(text("相册").findOne().bounds().centerX(), text("相册").findOne().bounds().centerY());
sleep(1000);
// 这里需要实现图片选择逻辑
// ...
sleep(1000);
click(text("发送").findOne().bounds().centerX(), text("发送").findOne().bounds().centerY());
sleep(1000);
}
// 发送文本
click(desc("发送").findOne().bounds().centerX(), desc("发送").findOne().bounds().centerY());
sleep(1000);
// 返回
back();
sleep(500);
back();
sleep(500);
return true;
}
// 开始发送
ui.start.click(() => {
if(running) return;
if(targets.length === 0){
toast("请先选择发送目标");
return;
}
messageContent = ui.content.text();
if(!messageContent){
toast("请输入消息内容");
return;
}
running = true;
currentIndex = 0;
let interval = parseInt(ui.interval.text()) || 2000;
threads.start(function(){
while(running && currentIndex < targets.length){
let target = targets[currentIndex];
log("正在发送给: " + target);
if(sendMessage(target)){
currentIndex++;
ui.run(() => {
ui.progress.setText(currentIndex + "/" + targets.length);
});
}else{
log("发送失败: " + target);
}
sleep(interval);
}
running = false;
toast("发送完成");
});
});
// 停止发送
ui.stop.click(() => {
running = false;
});
// 日志输出
function log(msg){
let now = new Date();
let timeStr = now.toLocaleTimeString();
ui.run(() => {
ui.log.append(timeStr + ": " + msg + "\n");
});
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。