微信实现自动聊天功能
或许这个需求市场上看不到。但是可以用来玩。你自己调教机器人,乐趣自在其中。但最好不要用于商用。
用自己的app,去操作别人的App,实现自动操作,必须要懂的一些技术点:
1.熟练使用AccessibilityService、AccessibilityNodeInfo等一系列辅助类。
2.会使用uiautomatorviewer.bat 找控件id。
关于第一点,网上应该不少吧,你不懂可以去百度。我之后估计也会写写吧。
至于第二点,我给大家截个图,大家一目了然
在eclipse中,连接手机,然后点它,能看到手机控件详情,你也可以在SDK中的tools 包下面找到uiautomatorviewer.bat 打开看手机详情。studio中,我不关心,你可以自己找找。示意图如下:
你可以从这里,获知很多有价值的东西:
比如APP包名,package;
当前某一个控件的ID,resource-id;
当前控件是否可点击,clickable;
当前控件是否可滑动,srollable;
当前控件的坐标。
用你自己的鼠标,随心所欲的点,获取你所需的属性, 然后对症下药地进行操作。
此处举个例子:
既然,从这儿取到的控件id,那么,在代码中如何通过资源id,获取到控件呢?
public class AutoChatService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
//应用包名
String className = event.getPackageName().toString();
//AccessibilityService 监测到的当前页面的类名
String pageName = event.getClassName().toString();
System.out.println(className);
System.out.println(pageName);
//获取当前页面NodeInfo
AccessibilityNodeInfo root = this.getRootInActiveWindow();
//根据id,获取一个控件集合,注意,id和控件之间是 一对多 的关系 ,所以是一个集合
List<AccessibilityNodeInfo> tp =root.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/y1");
//既然获取到 这个id 的所有控件的集合,你就可以进行操作了。
}
}
要自动操作,必须要有自动操作权限
在xml文件中注册service
<service
android:name="com.heyanchen.autochat.AutoChatService"
android:enabled="true"
android:exported="true"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service" />
</service>
在res 下新建xml包,建xml文件 accessibility_service。
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="true"
android:description="@string/accessibility_description"
android:notificationTimeout="100"
android:packageNames="com.tencent.mm,com.tencent.mobileqq"
/>
现在,我假设,你已经有足够的基础,做辅助操作了,再说说怎么搞图灵。
1.先去图灵官网注册一个图灵账号,官网地址:http://www.tuling123.com/
2.创建一个自定义机器人如图:
3.在机器人详情里填点基本信息,然后着重看如下图:
4.图灵的接口是开发的,你可以随便请求,图灵文档要求,最好用post请求。在你的程序中,写网络请求 。特别别忘了加手机的网络权限昂;<uses-permission android:name="android.permission.INTERNET"/>
话说,我做这个的时候,就钻了牛角尖,明明报错说没有网络权限,可我总是认为是不是图灵官方限制我请求,需要认证什么的,尝试了很多方法,是就没往uses-permisson上去想,旁边的老程序员语重心长地对我说:“你还是对Android不熟悉啊!”。唉。。。欲哭无泪
代码贴出来吧:你在demo中也可以看到
public static String doPostTuLing(String msg) {
String key = null;
String result = null;
if (key == null) {
key = "61bb5e237773469aa4944fd54232562a";//春明
}
if (msg == null || msg.equals("")) {
return result;
}
HttpURLConnection connection = null;
OutputStreamWriter out = null;
InputStream ins = null;
InputStreamReader inr = null;
BufferedReader br = null;
try {
JSONObject json = new JSONObject();
json.put("key", key);
json.put("info", msg);
json.put("secret", "26636d213b84aaba");
// json.put("secret", "8548f9447b11182d");
String requestStr = json.toString();
URL url = new URL("http://www.tuling123.com/openapi/api");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
if (url == null || connection == null) {
return result;
}
connection.setConnectTimeout(5000);
connection.setReadTimeout(3000);
connection.setDoInput(true);
// 注释。会404
connection.setDoOutput(true);
// post 不使用缓存
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
out.write(requestStr);
out.flush();
out.close();
Log.i("请求数据", requestStr);
Log.i("返回码", connection.getResponseCode() + "");
// Util.log("返回码:" + connection.getResponseCode());
if (connection.getResponseCode() != 200) {
return null;
}
ins = connection.getInputStream();
inr = new InputStreamReader(ins);
br = new BufferedReader(inr);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
stringBuffer.append(line);
}
String jsonResult = stringBuffer.toString();
JSONObject resultJson = new JSONObject(jsonResult);
int code = 0;
if (jsonResult != null || !jsonResult.equals("")) {
code = resultJson.getInt("code");
result = resultJson.getString("text");
}
// Util.log("code:"+ code);
// Util.log("result:"+ result);
if (code == 40001) {
return null; // key 错误
}
if (code == 40002) {
return null; // 聊天内容为null
}
if (code == 40007) {
return null; // 数据格式异常
}
if (code == 40004) {
return null;
}
if (result.indexOf("微信") != -1 || result.indexOf("图灵") != -1 || result.indexOf("不明白你是什么意思") != -1) {
result = "啥子哟";
} else if (result.indexOf("主人") != -1) {
result = result.replace("主人", "你");
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (br != null) {
br.close();
}
if (inr != null) {
inr.close();
}
if (ins != null) {
ins.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return Emoj.autoFilter(result);
}
5.现在,你已经获取到聊天回复内容了。利用accesibilityNodeInfo的复制粘贴在微信app中自动回复消息吧。
附上源码demo下载地址:http://download.youkuaiyun.com/download/hycfire/9762110 点击打开链接
本文介绍了如何利用Android的AccessibilityService和uiautomatorviewer实现微信自动聊天功能,通过集成图灵机器人API进行对话。首先需要掌握辅助服务的相关类和控件查找工具,然后在AndroidManifest.xml中注册服务,并配置权限。接着设置accessibility_service.xml,指定关注的包名。最后,注册图灵机器人并结合代码实现自动回复。
4449

被折叠的 条评论
为什么被折叠?



