如何使用python从联系人读取whatsapp消息?

问题描述:

我正在构建一个在指定时间登录缩放的机器人,并且正在从 whatsapp 获取链接.所以我想知道是否可以直接从 whatsapp 检索这些链接,而不必将其复制粘贴到 python 中.Google 充满了发送消息的指南,但有没有办法读取和检索这些消息,然后对其进行操作?

I'm building a bot that logs into zoom at specified times and the links are being obtained from whatsapp. So i was wondering if it is was possible to retrieve those links from whatsapp directly instead of having to copy paste it into python. Google is filled with guides to send messages but is there any way to READ and RETRIEVE those messages and then manipulate it?

更新:

请使用WhatsApp web中的inspect element section从WhatsApp web的当前时间类名更改每个部分的xpath的类名,以使用以下代码.因为 WhatsApp 已经更改了其元素的类名.

我已经尝试过使用 python 创建一个 WhatsApp 机器人.但是因为我也是初学者,所以还是有很多bug.

Update :

Please change the xpath's class name of each section from the current time class name of WhatsApp web by using inspect element section in WhatsApp web to use the following code. Because WhatsApp have changed its element's class names.

I have tried that in creating a WhatsApp bot using python. But there are still many bugs because of I am also beginner.

基于我的研究的步骤:

  1. 使用 selenium webdriver 打开浏览器
  2. 使用二维码登录 WhatsApp
  3. 如果您知道将从哪个号码收到会议链接,请使用此步骤,否则请检查此过程后提及的以下过程.

  • 找到并打开您要接收缩放会议链接的聊天室.
#user_name = "Name of meeting link Sender as in your contact list"
Example :
user_name = "Anurag Kushwaha"
#In above variable at place of `Anurag Kushwaha` pass Name or number of Your Teacher
# who going to sent you zoom meeting link same as you have in your contact list.
user = webdriver.find_element_by_xpath('//span[@title="{}"]'.format(user_name))
user.click()
# For getting message to perform action 
message = webdriver.find_elements_by_xpath("//span[@class='_3-8er selectable-text copyable-text']") 
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing received text message of any chat room.

for i in message:
    try:
        if "zoom.us" in str(i.text):
            # Here you can use you code to preform action according to your need
            print("Perform Your Action")
     except:
        pass

  1. 如果您不知道通过哪个号码收到链接.
  2. 然后您可以获取任何未读联系人块的 div 类,并打开包含该未读 div 类的所有聊天室列表.
  3. 然后查看打开聊天的所有未读消息,并从div类中获取消息.

当您不知道从谁那里收到 Zoom 会议链接时.

# For getting unread chats you can use
unread_chats = webdriver.find_elements_by_xpath("// span[@class='_38M1B']")
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing the number of unread message showing the contact card inside a green circle before opening the chat room.

# Open each chat using loop and read message.
for chat in unread_chats:
    chat.click()

    # For getting message to perform action
    message = webdriver.find_elements_by_xpath("//span[@class='_3-8er selectable-text copyable-text']")
    # In the above line Change the xpath's class name from the current time class name by inspecting span element
    # which containing received text message of any chat room.
    for i in messge:
        try:
            if "zoom.us" in str(i.text):
                # Here you can use you code to preform action according to your need
                print("Perform Your Action")
         except:
             pass

注意:在上面的代码中,webdriver"是您打开 web.whatsapp.com 的驱动程序

示例:

from selenium import webdriver
webdriver = webdriver.Chrome("ChromePath/chromedriver.exe")
webdriver.get("https://web.whatsapp.com")
# This wendriver variable is used in above code.
# If you have used any other name then please rename in my code or you can assign your variable in that code variable name as following line.
webdriver = your_webdriver_variable

完整的代码参考示例:


from selenium import webdriver
import time
webdriver = webdriver.Chrome("ChromePath/chromedriver.exe")
webdriver.get("https://web.whatsapp.com")
time.sleep(25) # For scan the qr code
# Plese make sure that you have done the qr code scan successful.
confirm = int(input("Press 1 to proceed if sucessfully login or press 0 for retry : "))
if confirm == 1:
   print("Continuing...")
elif confirm == 0:
   webdriver.close()
   exit()
else:
   print("Sorry Please Try again")
   webdriver.close()
   exit()
while True:
    unread_chats = webdriver.find_elements_by_xpath("// span[@class='_38M1B']")
    # In the above line Change the xpath's class name from the current time class name by inspecting span element
    # which containing the number of unread message showing the contact card inside a green circle before opening the chat room.
    
    # Open each chat using loop and read message.
    for chat in unread_chats:
        chat.click()
        time.sleep(2)
        # For getting message to perform action
        message = webdriver.find_elements_by_xpath("//span[@class='_3-8er selectable-text copyable-text']")
        # In the above line Change the xpath's class name from the current time class name by inspecting span element
        # which containing received text message of any chat room.
        for i in messge:
            try:
                if "zoom.us" in str(i.text):
                    # Here you can use you code to preform action according to your need
                    print("Perform Your Action")
             except:
                pass

  • 如果您要复制代码块,请确保缩进相同.

    • Please make sure that the indentation is equal in code blocks if you are copying it.

      可以在以下链接中阅读我的另一个答案,了解有关使用 python 的 WhatsApp 网络的更多信息.

      Can read my another answer in following link for more info about WhatsApp web using python.

      发送的 WhatsApp 消息中的换行符使用 Python

      我正在使用 python 开发 WhatsApp 机器人.

      I am developing WhatsApp bot using python.

      如需投稿,您可以联系:anurag.cs​​e016@gmail.com

      For contribution you can contact at : anurag.cse016@gmail.com

      请在我的 https://github.com/4NUR46 上给一颗星 如果这个答案帮助你.

      Please give a star on my https://github.com/4NUR46 If this Answer helps you.