初探 Autojs ,寫了個簡單的微信羣聊機器人(02.22新增了 face++的人臉識別 API )

簡單說一下 autojs ,這是一個開源(https://github.com/hyb1996/Auto.js 看帳號貌似是個96年的小朋友?)的利用 Android 的無障礙服務實現的自動操做軟件。javascript

 

-------------------18.2.22 凌晨更新----------------------html

此次更新同時支持了羣聊和普通聊天,而且加入了 face++ 的 人臉識別API 實現了【顏值測試】功能,增長了趣味性。。。java

這裏須要注意的是 你須要本身去 face++ 申請接口 https://console.faceplusplus.com.cn/register ,註冊個帳號而後建立一個 app_key 就好了git

【顏值測試】效果截圖:
0_1519250608024_daadc6bb-6585-4781-bae7-bba68a0512c2-image.pnggithub

-------------------------------------------------------------json

上代碼:api

"auto";

//設置無障礙模式 fast爲啓用緩存
auto();

//請求截屏權限  由於增長了這個權限請求 啓動腳本必須在 autojs APP界面 不然會有問題,主要是連電腦調試要注意
if(!images.requestScreenCapture()){
    toast("請求截圖失敗");
    exit();
}


toast("打開微信聊天界面");

/**
 * a4 : 聊天界面 時間控件 id
 * apt: 聊天列表頁 聊天摘要
 * jg:聊天界面 頭像
 * aec , ae2:聊天界面  聊天圖片
 * jh : 聊天暱稱
 * ji:
 * y:頭像和消息的父級  一個消息塊
 * 
 * aab : 聊天界面 輸入框
 * hj: 聊天界面 標題欄
 */

//console.log(text('微信').find());
//console.log(id('aab').exists());
//console.log(id('aps').findOne());
//log(id('y').findOnce(id('y').find().size()-1).child(2).findOne(id('ji')));

var wait = 0;

while(1){
    try{
        var msg = getMsg();
    }catch(e){
        var msg = 0;
    }
    log(msg); 
    if(msg){
       
        if(msg[0] == 'text')
        sendMsg(textAI(msg[1]));
        else
        sendMsg(imgAI(msg[1] ,msg[2]));
        
        wait = 0;
    }else{
        sleep(1000); //沒有新消息 等待1秒  ***這裏能夠根據狀況調整***
        wait += 1000;

        //5秒點擊一次 保持屏幕常亮
        if(wait > 5000){
            if(id('ji').exists())
            id('ji').findOne().click(); 
            wait = 0;
        }
    }
}


function getMsg(){

    
    if(currentActivity() != 'com.tencent.mm.ui.LauncherUI'){
        //不在聊天Activity

        //先判斷是否在微信中
        if(currentActivity().search('com.tencent.mm') == -1){
            //當前 APP 不是微信
            return false;
            //app.launch("com.tencent.mm");
        }
        //不在聊天Activity ,則有多是文字詳情界面  點擊任意區域回到聊天界面(點狀態欄區域比較保險)
        press(1 ,1,1);
        sleep(100); //這個時間根據手機的響應速度可能要稍微調整 ***這裏能夠根據狀況調整***
    }
    if(!id('aab').exists() || !id('y').exists()){
        //沒有找到輸入框則表示當前界面不是聊天窗口
        return false;
    }
    var last_y = id('y').findOnce(id('y').find().size()-1)

    if(last_y.childCount() != 2){
        return false;
    }

    var ji = last_y.child(1).findOne(id('ji'));

    if(ji){
        return ['text' , getText(ji)];
    }else {
        var aec = last_y.child(1).findOne(id('aec'));
        if(aec){
            var jh = last_y.child(1).findOne(id('jh'));
            if(jh){
                var nickname = jh.text();
            }else{
                var nickname = last_y.child(0).findOne(id('jg')).contentDescription.replace('頭像','');
            }
            return ['img' , getImg(aec) ,nickname];
        }
    }

    return false;
}

function getImg(uiObj){

    var img_rect = uiObj.bounds();

    //點擊圖片查看大圖
    press(img_rect.centerX() ,img_rect.centerY(),1)

    //var img = images.clip(captureScreen(), img_rect.left, img_rect.top, img_rect.width(), img_rect.height());
    //log(img);
    //images.save(img, "/sdcard/clip.png");

    //等待動畫完成
    sleep(750);

    //截屏並存到本地
    captureScreen("/sdcard/clip.png");

    //點擊屏幕回到聊天界面
    press(img_rect.centerX() ,img_rect.centerY(),1)

    return "/sdcard/clip.png";
}

function getText(uiObj){

    //微信的聊天文本不能直接獲取到 這裏模擬雙擊在文本詳情界面獲取內容
    //獲取文字區域 模擬雙擊
    var last_ji_rect = uiObj.bounds();
    press(last_ji_rect.centerX() ,last_ji_rect.centerY(),1);
    sleep(40); //雙擊間隔時間  ***這裏能夠根據狀況調整***
    press(last_ji_rect.centerX() ,last_ji_rect.centerY(),1);

    var afv = id('afv').findOne().text();
    press(last_ji_rect.centerX() ,last_ji_rect.centerY(),1);

    return afv;
}

function sendMsg(str){
    //設置輸入框文本
    id('aab').setText(str);

    //獲取標題欄區域並點擊,解決發送按鈕獲取不到的問題
    var hj = id('hj').findOne().bounds();
    click(hj.centerX(), hj.centerY());

    //點擊發送
    id('aah').click();
}

function imgAI(img_path ,nickname){

    var res = http.postMultipart(
        'https://api-cn.faceplusplus.com/facepp/v3/detect', 
        {
            api_key: "你的 key",
            api_secret:' 你的 secret',
            return_attributes:'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus',
            image_file: open(img_path)
    });
    var jsn = res.body.json();
    //log(jsn);
    if(typeof jsn.faces == 'undefined')
        return 'API 錯誤。。。';

    var face = jsn.faces;
    if(!face.length){
        str =  '抱歉!這可能不是一我的!\n';
    }else{
        
        var str = '';
        if(face.length > 1)
            str += '這可能不是一我的,但我一次只能檢測一張臉,至因而哪張臉你要猜猜看了...[\調皮]\n\n';

        face = face[0].attributes;

        //基本信息
        str += '基本信息:\n ';
        str += face.gender.value == 'Male'?' 男 ':' 女 ';
        str += face.age.value + '歲 ';
        if(face.ethnicity.value == 'Asian')
            str += '黃種人\n';
        else if(face.ethnicity.value == 'White')
            str += '白人\n';
        else
            str += '黑人\n';
        
        //面部表情
        var emotions = {anger:'憤怒',
            disgust:'厭惡',
            fear:'恐懼',
            happiness:'高興',
            neutral:'平靜',
            sadness:'傷心',
            surprise:'驚訝'};
        var emotion = {e80:0,e40:[]};
        for(var i in face.emotion){
            if(face.emotion[i] > 80){
                emotion.e80 = i;
                break;
            }
            if(face.emotion[i] > 40){
                emotion.e40.push(i);
            }
        }
        str += '表  情:\n ';
        if(emotion.e80){
            str += '很是'+ emotions[emotion.e80] ;
        }else if(emotion.e40.length == 2){
            str += emotion.e40[0] > emotion.e40[1] ? emotions[emotion.e40[0]]+'中帶着'+emotions[emotion.e40[1]] : emotions[emotion.e40[1]]+'中帶着'+emotions[emotion.e40[0]];
        }else if(emotion.e40.length == 1){
            str += emotions[emotion.e40[0]];
        }else{
            str += '五味雜陳';
        }
        str += '\n';

        //皮膚情況
        str += '皮膚情況:\n';
        str += ' 黑眼圈:'+parseInt(face.skinstatus.dark_circle)+'%\n';
        str += ' 青春痘:'+parseInt(face.skinstatus.acne)+'%\n';
        str += ' 色 斑:'+parseInt(face.skinstatus.stain)+'%\n';

        //顏值
        str += '顏  值:\n';
        str += ' 在大部分男性眼中:'+parseInt(face.beauty.male_score) +'分\n';
        str += ' 在大部分女性眼中:'+parseInt(face.beauty.female_score) +'分\n';
    }
    str += '\n(測試結果和照片清晰度及拍攝角度等因素有關,如對本次結果不滿意可發送新的照片進行測試,測試結果不表明本人觀點!)';

    return '@'+nickname+' 歡迎使用【顏值測試】功能,您的測試結果以下:\n\n'+ str;
}

function textAI(msg) {
    var url = "http://www.tuling123.com/openapi/api";
    id1 = "llmjk";
    var res = http.post(url, {
      key: "65458a5df537443b89b31f1c03202a80", //圖靈
      //key: "e0fcda33be9847c8b8534abe51b4b095", //唐嫣
      // key: "b4438ca194064134afa28f686fbd04a5", //my圖靈
      info: msg,
      userid: "1",
    });
    var html = res.body.string();
    eval("b=" + html);
    return b.text;
  }

 

目前只支持羣聊,實現方案可能也不是最好的,一些參數可能須要根據手機的狀況作些調整。。。緩存

裏面的圖靈機器人直接引用了論壇裏 @O了嗎 的‘QQ機器人’中的代碼,在此感謝。。。微信

相關文章
相關標籤/搜索