Skip to content

Instantly share code, notes, and snippets.

@winkidney
Created April 18, 2022 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winkidney/d7afa463d9e2f969f662b476ba0b1a57 to your computer and use it in GitHub Desktop.
Save winkidney/d7afa463d9e2f969f662b476ba0b1a57 to your computer and use it in GitHub Desktop.
HamiBot-WechatFileSender
function sleepR() {
sleep(random(1, 2) * 1000);
}
function clickWidget(widget) {
click(widget.bounds().centerX(), widget.bounds().centerY());
}
function longClickWidget(widget) {
console.log("got widget to long click", widget);
press(widget.bounds().centerX(), widget.bounds().centerY(), 1000);
toastLog("长按完毕:" + widget.text());
}
function goBackLeft() {
id("fs").findOne().click();
}
function isFilesExist(fileNames) {
let exists = true;
fileNames.forEach(
(fileName) => {
if (!text(fileName).exists()) {
toastLog("文件不存在,请检查后重新运行:" + fileName);
exists = false;
}
}
);
return exists;
}
function clickMessageCreate() {
const recentChat = text("最近聊天").findOne();
const btns = recentChat.parent().children();
const createChat = btns[btns.length - 1];
toastLog("从联系人中创建新聊天");
createChat.click();
}
function scrollInContacts() {
x_e = device.width / 2;
y_e = 0 + 400;
x_s = x_e;
y_s = device.height - 300;
swipe(x_s, y_s, x_e, y_e, 200);
}
function searchOneContanctInTag(tagName, alreadyUsed) {
toastLog("搜索框存在:" + text("搜索").exists())
text("搜索").findOne().click();
sleepR();
text(tagName).click();
sleepR();
text(tagName).untilFind();
toastLog("已打开标签对应的联系人列表:" + tagName);
let selected = false;
let shouldContinue = true;
let prevContacts = [];
let currentContacts = [];
while (!selected) {
id("kug").depth(17).find().forEach(
(target) => {
const contactName = target.text();
currentContacts.push([target, contactName]);
}
)
currentContacts.forEach(
(contact) => {
const contactName = contact[1];
const target = contact[0];
if (alreadyUsed.hasOwnProperty(contactName)) {
return;
}
if (selected) {
return;
}
toastLog("点击联系人:" + contactName);
clickWidget(target);
selected = true;
alreadyUsed[contactName] = true;
}
);
if (prevContacts.length > 0) {
if (prevContacts[prevContacts.length - 1][1] == currentContacts[currentContacts.length - 1][1]) {
// no new contacts
break;
}
}
prevContacts = currentContacts;
currentContacts = [];
scrollInContacts();
}
shouldContinue = selected;
return shouldContinue;
}
function enterToSendFile() {
toastLog("寻找确认按钮:" + className("android.widget.Button").depth(12).exists());
className("android.widget.Button").depth(12).findOne().click();
text("选择联系人").untilFind();
className("android.widget.Button").depth(12).findOne().click();
text("给朋友留言").untilFind();
text("发送").click();
}
function _selectFilesWithTag(tagName, fileNames) {
const firstFile = fileNames.pop();
sleepR();
text("文件").untilFind();
const widget = text(firstFile).findOne();
toastLog("文件存在:" + text(firstFile).exists());
longClickWidget(widget);
sleepR();
text("更多").findOne().click();
sleepR();
fileNames.forEach(
(fileName) => {
const widget = text(fileName).findOne();
clickWidget(widget);
sleepR();
}
)
toastLog("已经选中所有文件,准备发送");
sleepR();
desc("转发").findOne().click();
sleepR();
clickMessageCreate();
sleepR();
}
function shareFileToContants(tagName, fileNames) {
let shouldContinue = true;
const alreadySent = {};
while (shouldContinue) {
fileNames2Handle = [];
for (i = 0; i < fileNames.length; i++) {
fileNames2Handle[i] = fileNames[i];
}
_selectFilesWithTag(tagName, fileNames2Handle);
shouldContinue = searchOneContanctInTag(tagName, alreadySent);
if (shouldContinue) {
enterToSendFile();
}
sleepR();
}
toastLog("未找到更多联系人,停止发送");
}
function clickMeFromHome() {
// toastLog("回到主页");
// id("jub").indexInParent(0).findOne().click();
// sleepR();
toastLog("打开‘我’");
clickWidget(text("我").findOne());
}
function openFavoritesFiles() {
toastLog("打开收藏的文件");
clickWidget(text("收藏").findOne());
sleepR();
clickWidget(text("文件").findOne());
}
function goToTag(tagName) {
const succeed = launch('com.tencent.mm');
toastLog("start wechat: " + succeed);
while (!click("通讯录"));
sleepR();
while (!click("标签"));
sleepR();
const tagWidget = text(tagName).findOne();
clickWidget(tagWidget);
toastLog("clicked tag:" + tagName);
sleepR();
}
function testSingle(){
const widget = text("这次,终于有人把客户成功说清楚了.pdf").findOne();
longClickWidget(widget);
}
function main(){
launch("com.tencent.mm");
// 可以通过自义定tag_name和lyrics还有demo的名字来发送文件
// tag_name 是联系人标签名,demo和lirycs是完整的文件名
const { tag_name, lyrics, demo } = hamibot.env;
const fileNames = [];
if (lyrics !== "") {
fileNames.push(lyrics);
}
if (demo !== "") {
fileNames.push(demo);
}
console.log(lyrics, demo);
clickMeFromHome();
openFavoritesFiles();
text("文件").untilFind();
if (!isFilesExist(fileNames)) {
toastLog("脚本执行失败,文件未找到,退出执行");
return;
}
shareFileToContants(tag_name, fileNames);
}
main();
// testSingle();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment