Chat GPT in WhatsApp

The next code is the implementation of ChatGPT in WhatsApp to automate chat responses. WhatsApp doesn’t have any API to send or read messages. For this reason, I had to use Selenium and OCR (Optical Character Recognition) to read the new messages and send the answers according to the ChatGPT. This program is fun to use because, even though sometimes the answers given by ChatGPT make sense, most of the time they make no sense.

Steps:

  1. Open the WhatsApp web using Selenium and identify by using the QR code.
  2. Enter to the chat of the person you want to reapply automatically.
  3. Execute the main / while True code.
  4. The program takes a screenshot of the chat and cuts the screenshot to the location of the last message received.
  5. Use the screenshot of the last message received and apply the OCR that will obtain the message with high accuracy.
  6. Pass the message received as a Chat GPT input and obtain the answers to that message according to the Chat GPT output.
  7. Find the XPaths to write and send the message on the WhatsApp webpage opened with selenium and send the answers.
  8. The code will take constant screenshots of the chat until the last screenshot is not the same as the last one which will mean that the person replays the message and the process will be repeated again.

Being inside the While True loop allows us to have unlimited responses to our WhatsApp messages without doing anything because is completely automatic. 

Code:

# Import

import time
from selenium import webdriver
import datetime
import random
from selenium.webdriver.common.by import By
import cv2
import numpy as np
import pytesseract
import datetime as dt
import openai as ai
import pandas as pd
import numpy as np
import seaborn as sns
from datetime import datetime
from math import *
from datetime import datetime as date
import matplotlib.pyplot as plt
from iteration_utilities import duplicates
from iteration_utilities import unique_everseen

ai.api_key = ""

# Selenium exe path:
browser = webdriver.Chrome(executable_path = r"C:\Users\....) # <-fill path   

#Open the WhatsApp  and identify by using the QR code.
browser.get("https://web.whatsapp.com/")
time.sleep(5)

# Chat GPT functions:

def resposta_CHATGP3(question):
        completion = ai.Completion()
        start_chat_log = """Human: Hello, I am Human. AI: Hello, human I am openai gpt3. Human: How are you? AI: I am fine, thanks for asking.  """
        
        resposta = (chat(question,start_chat_log))
        
        try:
            resposta = resposta.split("Human:")[0]
        except:
            pass
        
        return resposta

def chat(question,chat_log = None) -> str:
    completion = ai.Completion()
    if(chat_log == None):
        chat_log = start_chat_log
    prompt = f"{chat_log}Human: {question}\nAI:"
    response = completion.create(prompt = prompt, engine =  "davinci", temperature = 0.85,top_p=1, frequency_penalty=0, 
    presence_penalty=0.7, best_of=2,max_tokens=100,stop = "\nHuman: ")
    return response.choices[0].text

def modify_start_message(chat_log,question,answer) -> str:
    if chat_log == None:
        chat_log = start_chat_log
    chat_log += f"Human: {question}\nAI: {answer}\n"
    return chat_log

# MAIN:
# It is necessary to have an image saved with this name in the path where the program is located.

old_text = cv2.imread('Cropped Image.jpg')

while True:    

    # Ejecutar cada segundo
    m = time.strftime("%S")  
    m = int(m)
    res = m%2
    
    if res == 0:
        
        #________________Screemshot_____________________________________________________________
        
        browser.save_screenshot('Chat.png')
        #browser.save_screenshot('Chat.png')
        
        # Guardar texto en   img
        img = cv2.imread('Chat.png')
        cv2.imshow("original", img)

        # Cropping an image
                #eje y superior /in eje x menor /  major
        cropped_image = img[780:820, 650:1700 ] 

        # Display cropped image
        cv2.imshow("cropped", cropped_image)
        cv2.imwrite("Cropped Image.jpg", cropped_image)
        cv2.waitKey(27)
        new_text = cv2.imread('Cropped Image.jpg')
        old_text = cv2.imread('Cropped Image_old.jpg')
        
        difference = cv2.subtract(new_text, old_text)
        b, g, r = cv2.split(difference)        
        if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
            print("The images are completely Equal")
            time.sleep(10)
            
        
        else:
        
            print("No son iguals")
            #_____________________________SCREMSHOT TO TXT____________________________________________
            pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract"  # <-fill path     
            texto = pytesseract.image_to_string("Cropped Image.jpg")        
            texto = texto.replace("\n", " ")     

            ##___RESPOSTA CHAT BOT_______________________________________________________________
            #resposta = resposta_chatBot(texto)
            resposta = resposta_CHATGP3(texto)
            #_enviar resposta
            elements = browser.find_element(By.XPATH, '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]')
            elements.send_keys(resposta)
            But_env = browser.find_element(By.XPATH, '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[2]/button/span')
            But_env.click()

            time.sleep(10)

            browser.save_screenshot('Chat.png')

            # Guardar texto en   img
            img = cv2.imread('Chat.png')
            cv2.imshow("original", img)

            # Cropping an image
                    #eje y superior /in eje x menor /  major
            cropped_image = img[780:820, 650:1700 ] 

            # Display cropped image
            cv2.imshow("cropped", cropped_image)
            cv2.imwrite("Cropped Image_old.jpg", cropped_image)
            cv2.waitKey(27)
            

            time.sleep(0.5)

Leave a Comment

Your email address will not be published. Required fields are marked *