Recently, we need to realize a QQ automatic message reply function. Baidu has checked the relevant blog reference: https://blog.csdn.net/qq_45404396/article/details/112750110
Article catalog
python automation: automatic reply to QQ messages
1. Relevant software and python modules to be installed
2. Connect the mobile phone
3. Test whether the connection is successful
4. Code implementation
5. Summary
1. Relevant software and python modules to be installed
1.1 installing the client module
Open the command window and enter the command: PIP install appium Python client
1.2 installing Appium Server
Download at: appium
1.3 installing JDK
After installation, add the environment variable JAVA_HOME, specify the installation directory of jdk, and the minor part is:
1.4 installing android sdk
Add an environment variable ANDROID_HOME, set the value to the decompression directory of the sdk package, and the minor code is:
In addition, you'd better add adb.exe in the path Exe environment variable
1.5 screen projection software
Xiaobian uses the same screen assistant of mizhuo. The download website is: Midro screen assistant
2. Connect the mobile phone
The USB connection used by Xiaobian needs a data cable to connect the mobile phone to the computer. At the same time, it needs to turn on the developer mode of the mobile phone.
So how to open the developer mode, take Xiaobian's mobile phone as an example (OPPO mobile phone)
Go to mobile phone settings, click about mobile phone, and then keep clicking the version number. When the following words appear
Then enter other settings (different phones may be different), and you can see that there is an additional developer option here.
Enter the developer option and turn it on. You can find a USB debugging switch here. Turn it on.
After opening, you can see the picture of the mobile phone on the projection software
3. Test whether the connection is successful
Enter: adb devices -l in the command window. If the following screen appears, the connection should be successful
Sometimes, an error may be reported. The general reason is the ADB of the projection software adb.exe version and sdk If the exe version is inconsistent, (Xiaobian guessed by himself), you should only add any ADB Just copy exe to another one.
4. Code implementation
Reference codes are as follows:
from appium import webdriver import time ''' Xiaomi mobile phone and oppo mobile phone appium Startup error:writing to settings requires:android.permission.WRITE_SECURE_SETTINGS https://blog.csdn.net/zzwfd/article/details/104005744 Xiaomi: in the developer option, put“ USB Commissioning (security settings)"Just open it. allow USB Click debug modify permission or simulate oppo: In the developer options, put"Prohibit permission monitoring"Just open it. ''' desired_caps={ 'platformName':'Android', 'platformVersion':'10.0', 'deviceName':'ea5812bd', 'appPackage':'com.tencent.mobileqq', # Automation application 'appActivity':'com.tencent.mobileqq.activity.SplashActivity', #'unicodeKeyboard':True, #'resetKeyboard':True, 'noReset':True, 'newCommandTimeout':6000, 'automationName':'UiAutomator2' } driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps) driver.implicitly_wait(10) driver2=driver.find_element_by_id('recent_chat_list') list2=driver2.find_elements_by_class_name('android.widget.LinearLayout') print('current QQ Message is%d individual'%(len(list2))) time.sleep(2) list2[0].click() def send_Message(text2:str): # Send a message driver4=driver.find_element_by_id('inputBar') driver4.find_element_by_id('input').send_keys(text2) driver4.find_element_by_id('fun_btn').click() time.sleep(2) print("send message:%s"%(text2)) list4=[ "Liu Bang, Ziji, from Fengyi, Peijun county (now Fengxian County, Jiangsu Province). Outstanding statesmen, strategists and military commanders in Chinese history, the founding emperor of the Han Dynasty, and the great founders and pioneers of the Han nation and Han culture have made outstanding contributions to the development of the Han nationality and the reunification of China.", "not yet", "Hunan Province, abbreviated as "Xiang", is the provincial administrative region of the people's Republic of China and the provincial capital Changsha, bounded at 24 n°38′~30°08′,108 east longitude°47′~114°15′It is adjacent to Jiangxi in the East, Chongqing and Guizhou in the west, Guangdong and Guangxi in the South and Hubei in the north, with a total area of 21.18 10000 square kilometers." ] list5=[ "1(Please consult xxx", "2(Welcome to visit https://blog.csdn.net/qq_45404396/article/details/112750110 ", "3(Please consult my assistant" ] while True: try: driver3=driver.find_element_by_id('listView1') list3=driver3.find_elements_by_class_name('android.widget.RelativeLayout') text=list3[-1].find_element_by_id('chat_item_content_layout').text print('Received message:%s'%(text)) # receive messages time.sleep(5) if(text=='Hello, please look up Liu Bang's profile for me'): send_Message(list4[0]) elif(text=="Have you had lunch yet"): send_Message(list4[1]) elif(text=="Introduce Hunan!"): send_Message(list4[2]) if (text == '1((drawing)'): send_Message(list5[0]) elif (text == "2((order receiving)"): send_Message(list5[1]) elif (text == "3((learning)"): send_Message(list5[2]) except Exception as e: pass
In the process of running the code, some things will be automatically installed on the mobile phone, and you can agree to install them.
Operation results:
Using python to automatically reply to QQ messages
5. Summary
This is Ui automation realized by appium and needs to be improved. Here is the website for Xiaobian learning: automation At the same time, there is also a link to download the above software compression package (Baidu online disk) under this website.