1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Get the last message sent by WhatsApp Web (Python)

Discussão em 'Python' iniciado por Stack, Setembro 13, 2024.

  1. Stack

    Stack Membro Participativo

    This is my first post, I'm doing a personal project to use the Gemini API to respond to messages on WhatsApp Web. However, I have a problem with 'get' in the last message.

    I'm a beginner, I'd like to know what's going on.

    Apparently the error is because it is not possible to find the element in the last message, I tried to get references from similar projects on GitHub and AI help to try this GET, but I still didn't get any results.



    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.common.exceptions import NoSuchElementException

    import time
    import google.generativeai as genai
    import json

    # Config - API Google Gemini
    genai.configure(api_key='Key_Api_Gemini')
    model = genai.GenerativeModel(model_name="gemini-1.5-flash-001")

    def TalkIA(question):
    #Talk to AI
    if question == 'Exit':
    return False
    else:
    response = model.generate_content(question)
    response_dict = response.to_dict()
    response_json = json.dumps(response_dict)
    response_data = json.loads(response_json)
    answer = response_data['candidates'][0]['content']['parts'][0]['text']
    return answer


    def get_last_message(nav_last):
    # Function to get the last message
    try:
    post = nav_last.find_element(By.CLASS_NAME, '_3_7SH')
    last = len(post) - 1
    x = post[last]
    msg = x.find_element(By.CSS_SELECTOR, "span.selectable-text").text
    return msg

    except (NoSuchElementException, IndexError) as e:
    print("Error to get the last message:", e)
    return None


    def send(nav_send, answer_send):
    #Function to send message
    main_div = nav_send.find_element(By.CSS_SELECTOR, 'div#main')
    input_box = main_div.find_element(By.CSS_SELECTOR, 'div[role="textbox"]')
    input_box.click()

    input_box.send_keys(answer_send)

    send_button = main_div.find_element(By.CSS_SELECTOR, 'button span[data-icon="send"]')
    send_button.click()


    # Config the driver
    navegador = webdriver.Chrome()
    navegador.get("https://web.whatsapp.com/")
    navegador.implicitly_wait(15)

    # Wait the WhatsApp open
    while len(navegador.find_elements(By.ID, 'side')) < 1:
    time.sleep(1)

    # Configura o número e a mensagem
    tel = 559999999999
    text = 'Hello, I am the Google Gemini, if you want help press: 1'
    link = f"https://web.whatsapp.com/send?phone={tel}&text={text}"
    navegador.get(link)

    # Wait the WhatsApp open and send the 'text'
    try:
    # Waiting...
    while len(navegador.find_elements(By.ID, 'side')) < 1:
    time.sleep(1)
    send_button = WebDriverWait(navegador, 30).until(
    EC.element_to_be_clickable((By.XPATH, '//span[@data-icon="send"]'))
    )
    send_button.click()
    print("Message < Hello > send with sucess")

    except Exception as e:
    print("Error to send the first message:", e)

    while True:
    last_msg = get_last_message(navegador)

    if last_msg == "1":
    # Send the first question to user
    first_msg = "You activated the AI, what's your question?"
    send(navegador, first_msg)
    time.sleep(5) # Waiting the question...

    while True:
    new_question = get_last_message(navegador)

    # Call the AI function to get the response
    request = TalkIA(new_question)

    send(navegador, request)
    time.sleep(5) # Waiting...

    else:
    time.sleep(5)
    print('Waiting 1')

    -


    I need to get the last message

    Continue reading...

Compartilhe esta Página