其他
自动化篇 - 为闲鱼制作一个客服机器人(上)
点击上方“AirPython”,选择“置顶公众号”
第一时间获取 Python 技术干货!
阅读文本大概需要 18 分钟。
1
目 标 场 景
另外,如果你有多个闲鱼号,经常会因为消息处理不及时,导致订单丢失了。因此,对闲鱼消息进行统一管理变得非常有必要。
本篇文章的目的是利用 上篇文章 提到的无障碍方案带大家实现这些功能。
2
实 现 代 码
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="true"
android:description="@string/app_name"
android:notificationTimeout="100"
android:packageNames="com.taobao.idlefish" />
AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED
# 页面切换变化
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
# 页面内容变化
AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
int eventType = event.getEventType();
//获取包名
String packageName = event.getPackageName().toString();
//类名
String className = event.getClassName().toString();
//只筛选下面三个事件,其他事件过滤掉
if (eventType != AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED && eventType != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && eventType != AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED)
{
Log.e("xxx","eventType:"+eventType);
return;
}
/***
* 处理通知栏消息
* @param event
*/
private void handleNotificationEventMet(AccessibilityEvent event)
{
if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification)
{
Notification notification = (Notification) event.getParcelableData();
if (notification == null)
{
return;
}
PendingIntent pendingIntent = notification.contentIntent;
if (pendingIntent == null)
{
return;
}
try
{
//注意:通知栏的文字消息没有参考意义
//跳转到聊天信息界面
Log.e("xag", "准备跳转到聊天界面");
pendingIntent.send();
} catch (PendingIntent.CanceledException e)
{
e.printStackTrace();
}
}
}
/**
* 模拟下滑操作
*/
public void performScrollBackward()
{
try
{
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
performGlobalAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
}
/**
* 查找对应ID的View Level>=18
*
* @param id id
* @return View
*/
public AccessibilityNodeInfo findViewByID(String id)
{
AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
if (accessibilityNodeInfo == null)
{
return null;
}
List<AccessibilityNodeInfo> nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(id);
if (nodeInfoList != null && !nodeInfoList.isEmpty())
{
for (AccessibilityNodeInfo nodeInfo : nodeInfoList)
{
if (nodeInfo != null)
{
return nodeInfo;
}
}
}
return null;
}
//聊天列表
AccessibilityNodeInfo chat_lv = findViewByID(Ids.id_chat_listview);
int chat_count = chat_lv.getChildCount();
//注意:包含自己发送的消息、收到的消息、公共提示消息,比如:一直未回复?试试语音/视频通话
Log.e(TAG, "一共有:" + chat_count + "条聊天记录");
/所有聊天记录
//系统聊天信息,可以忽略
List<String> chat_msgs_common = new ArrayList<>();
//收到的信息
List<String> chat_msgs_from = new ArrayList<>();
//发出去的信息
List<String> chat_msgs_to = new ArrayList<>();
for (int i = 0; i < chat_count; i++)
{
AccessibilityNodeInfo tempChatAccessibilityNodeInfo = chat_lv.getChild(i);
//注意:通过判断是否有头像元素、昵称元素来判断是哪种聊天记录
//获取头像元素【左】【发送者】
AccessibilityNodeInfo headPortraitAccessibilityNodeInfoLeft = findViewByID(tempChatAccessibilityNodeInfo, Ids.id_chat_head_portrait_left);
//获取头像元素【右】【接受者】
AccessibilityNodeInfo headPortraitAccessibilityNodeInfoRight = findViewByID(tempChatAccessibilityNodeInfo, Ids.id_chat_head_portrait_right);
//获取文字内容
AccessibilityNodeInfo tempTextAccessibilityNodeInfo = findViewByID(tempChatAccessibilityNodeInfo, Ids.id_chat_text);
if (null == tempTextAccessibilityNodeInfo || null == tempTextAccessibilityNodeInfo.getText())
{
Log.e(TAG, "索引" + i + ",聊天内容为空");
continue;
}
String chatText = tempTextAccessibilityNodeInfo.getText().toString();
Log.e(TAG, "聊天内容为:" + chatText);
if (null != headPortraitAccessibilityNodeInfoLeft)
{
chat_msgs_from.add(0, chatText);
} else if (null != headPortraitAccessibilityNodeInfoRight)
{
chat_msgs_to.add(0, chatText);
} else
{
chat_msgs_common.add(0, chatText);
}
}
/***
* 回复消息
* @param event
*/
private void reply_content(AccessibilityEvent event, String content)
{
//元素:输入框
AccessibilityNodeInfo chat_edit = findViewByID(Ids.id_edittext);
//把文本输入进去
inputText(chat_edit, content);
//元素:发送按钮
AccessibilityNodeInfo chat_send = findViewByID(Ids.id_sendtext);
Log.e(TAG,"准备回复的内容是:"+content);
try
{
Thread.sleep(5000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
//模拟点击发送按钮
performViewClick(chat_send);
}
//定义一段固定的回复内容
public static String reply_first = "让我开心的是,可以在这里遇见有趣的事物,可以遇见您。\n有什么可以帮到您的呢?\n回复【11】获取商品信息\n回复【22】获取发货信息";
//11:商品信息
public static String reply_11 = "亲,商品还有货\n现在可以直接拍下\n等店主看到了会第一时间发货";
//22:发货信息
public static String reply_22 = "亲,我们是包邮的!\n需要邮寄快递可以给我们留言";
//other:其他
public static String reply_other = "亲,信息收到了!\n主人会第一时间给你答复,稍等哈~";
// 初次聊天就发送默认的信息
if (chat_msgs_from.size() == 1 && chat_msgs_to.size() == 0)
{
//输入之后,返回,退出输入框
Log.e(TAG, "第一次收到信息,回复默认准备的内容");
reply_content(event, Constants.reply_first);
} else if (chat_msgs_from.size() > 0)
{
//第一条文本内容
String first_msg = chat_msgs_from.get(0);
if ("11".equals(first_msg))
{
reply_content(event, Constants.reply_11);
} else if ("22".equals(first_msg))
{
reply_content(event, Constants.reply_22);
} else
{
reply_content(event, Constants.reply_other);
}
} else
{
Log.e(TAG, "对方没有应答,不处理");
}
/**
* 模拟返回操作
*/
public void performBackClick()
{
try
{
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
while (!AppUtils.judgeIsMainPage(getRootInActiveWindow()))
{
Log.e("xag","现在不是主页面,返回一次");
performGlobalAction(GLOBAL_ACTION_BACK);
}
}
/***
* 获取消息列表中有未读的消息Node
* @param node
* @return
*/
public static AccessibilityNodeInfo getUnreadMsgNode(AccessibilityNodeInfo node)
{
AccessibilityNodeInfo unread_node = null;
if (node == null || node.getChildCount() <= 1)
{
Log.e("xag", "未读消息判断,node==null");
return unread_node;
}
Log.e("xag", "未读消息判断,子类个数:" + node.getChildCount());
//过滤ListView的头部,包含:通知消息、互动消息、活动消息、鱼塘消息
for (int i = 1; i < node.getChildCount(); i++)
{
AccessibilityNodeInfo temp_node = node.getChild(i);
List<AccessibilityNodeInfo> unread_nodes = temp_node.findAccessibilityNodeInfosByViewId(Ids.id_main_unread_tag);
if (null == unread_nodes || unread_nodes.size() == 0)
{
Log.e("xag", "未读消息判断,索引:" + i + ",unread_nodes为空");
} else
{
unread_node = unread_nodes.get(0);
break;
}
}
Log.e("xag", "未读消息判断,unread_node==null:" + (unread_node == null));
return unread_node;
}
//选中在消息Tab,处理聊天页面
if (Constants.HOME_TAB.TAB_XIAO_XI == currentTab)
{
Log.e(TAG, "当前Tab:消息Tab");
//判断新消息在哪个位置,然后点击进入
AccessibilityNodeInfo main_listview_node = findViewByID(Ids.id_main_conversation_listview);
//未读消息Node
AccessibilityNodeInfo unread_tag_node = AppUtils.getUnreadMsgNode(main_listview_node);
if (null != unread_tag_node)
{
Log.e(TAG, "点击进入会话");
//点击进入消息列表
performViewClick(unread_tag_node);
//处理聊天信息
handleChatMet(event);
} else
{
Log.e(TAG, "列表中没有未读消息");
}
}
3
结 果 结 论