{"id":480,"date":"2023-04-28T09:34:50","date_gmt":"2023-04-28T09:34:50","guid":{"rendered":"https:\/\/rodamoya.com\/?p=480"},"modified":"2023-05-26T07:41:44","modified_gmt":"2023-05-26T07:41:44","slug":"chat-gpt-in-whatsapp","status":"publish","type":"post","link":"https:\/\/rodamoya.com\/index.php\/2023\/04\/28\/chat-gpt-in-whatsapp\/","title":{"rendered":"Chat GPT in WhatsApp"},"content":{"rendered":"\n<p>The next code is the implementation of ChatGPT in WhatsApp to automate chat responses. WhatsApp doesn&#8217;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.<\/p>\n\n\n\n<div style=\"height:58px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-4\">\n<div class=\"wp-block-column is-layout-flow\"><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow\">\n<figure class=\"wp-block-video\"><video autoplay muted preload=\"none\" src=\"https:\/\/rodamoya.com\/wp-content\/uploads\/2023\/04\/Captura_WhatsApp-5.mp4\"><\/video><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<figure class=\"wp-block-video\"><video muted preload=\"none\" src=\"https:\/\/rodamoya.com\/wp-content\/uploads\/2023\/04\/Canada_Biggest_Country.mp4\"><\/video><\/figure>\n\n\n\n<p><strong>Steps:<\/strong><br><\/p>\n\n\n\n<ol>\n<li>Open the WhatsApp web using Selenium and identify by using the QR code.<\/li>\n\n\n\n<li>Enter to the chat of the person you want to reapply automatically.<\/li>\n\n\n\n<li>Execute the main \/ while True code.<\/li>\n\n\n\n<li>The program takes a screenshot of the chat and cuts the screenshot to the location of the last message received.<\/li>\n\n\n\n<li>Use the screenshot of the last message received and apply the OCR that will obtain the message with high accuracy.<\/li>\n\n\n\n<li>Pass the message received as a Chat GPT input and obtain the answers to that message according to the Chat GPT output.<\/li>\n\n\n\n<li>Find the XPaths to write and send the message on the WhatsApp webpage opened with selenium and send the answers.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ol>\n\n\n\n<p>Being inside the While True loop allows us to have unlimited responses to our WhatsApp messages without doing anything because is completely automatic.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Import\n\nimport time\nfrom selenium import webdriver\nimport datetime\nimport random\nfrom selenium.webdriver.common.by import By\nimport cv2\nimport numpy as np\nimport pytesseract\nimport datetime as dt\nimport openai as ai\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nfrom datetime import datetime\nfrom math import *\nfrom datetime import datetime as date\nimport matplotlib.pyplot as plt\nfrom iteration_utilities import duplicates\nfrom iteration_utilities import unique_everseen\n\nai.api_key = \"\"\n\n# Selenium exe path:\nbrowser = webdriver.Chrome(executable_path = r\"C:\\Users\\....) # &lt;-fill path   \n\n#Open the WhatsApp  and identify by using the QR code.\nbrowser.get(\"https:\/\/web.whatsapp.com\/\")\ntime.sleep(5)\n\n# Chat GPT functions:\n\ndef resposta_CHATGP3(question):\n        completion = ai.Completion()\n        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.  \"\"\"\n        \n        resposta = (chat(question,start_chat_log))\n        \n        try:\n            resposta = resposta.split(\"Human:\")&#91;0]\n        except:\n            pass\n        \n        return resposta\n\ndef chat(question,chat_log = None) -&gt; str:\n    completion = ai.Completion()\n    if(chat_log == None):\n        chat_log = start_chat_log\n    prompt = f\"{chat_log}Human: {question}\\nAI:\"\n    response = completion.create(prompt = prompt, engine =  \"davinci\", temperature = 0.85,top_p=1, frequency_penalty=0, \n    presence_penalty=0.7, best_of=2,max_tokens=100,stop = \"\\nHuman: \")\n    return response.choices&#91;0].text\n\ndef modify_start_message(chat_log,question,answer) -&gt; str:\n    if chat_log == None:\n        chat_log = start_chat_log\n    chat_log += f\"Human: {question}\\nAI: {answer}\\n\"\n    return chat_log\n\n# MAIN:\n# It is necessary to have an image saved with this name in the path where the program is located.\n\nold_text = cv2.imread('Cropped Image.jpg')\n\nwhile True:    \n\n    # Ejecutar cada segundo\n    m = time.strftime(\"%S\")  \n    m = int(m)\n    res = m%2\n    \n    if res == 0:\n        \n        #________________Screemshot_____________________________________________________________\n        \n        browser.save_screenshot('Chat.png')\n        #browser.save_screenshot('Chat.png')\n        \n        # Guardar texto en   img\n        img = cv2.imread('Chat.png')\n        cv2.imshow(\"original\", img)\n\n        # Cropping an image\n                #eje y superior \/in eje x menor \/  major\n        cropped_image = img&#91;780:820, 650:1700 ] \n\n        # Display cropped image\n        cv2.imshow(\"cropped\", cropped_image)\n        cv2.imwrite(\"Cropped Image.jpg\", cropped_image)\n        cv2.waitKey(27)\n        new_text = cv2.imread('Cropped Image.jpg')\n        old_text = cv2.imread('Cropped Image_old.jpg')\n        \n        difference = cv2.subtract(new_text, old_text)\n        b, g, r = cv2.split(difference)        \n        if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:\n            print(\"The images are completely Equal\")\n            time.sleep(10)\n            \n        \n        else:\n        \n            print(\"No son iguals\")\n            #_____________________________SCREMSHOT TO TXT____________________________________________\n            pytesseract.pytesseract.tesseract_cmd = r\"C:\\Program Files\\Tesseract-OCR\\tesseract\"  # &lt;-fill path     \n            texto = pytesseract.image_to_string(\"Cropped Image.jpg\")        \n            texto = texto.replace(\"\\n\", \" \")     \n\n            ##___RESPOSTA CHAT BOT_______________________________________________________________\n            #resposta = resposta_chatBot(texto)\n            resposta = resposta_CHATGP3(texto)\n            #_enviar resposta\n            elements = browser.find_element(By.XPATH, '\/\/*&#91;@id=\"main\"]\/footer\/div&#91;1]\/div\/span&#91;2]\/div\/div&#91;2]\/div&#91;1]\/div\/div&#91;1]')\n            elements.send_keys(resposta)\n            But_env = browser.find_element(By.XPATH, '\/\/*&#91;@id=\"main\"]\/footer\/div&#91;1]\/div\/span&#91;2]\/div\/div&#91;2]\/div&#91;2]\/button\/span')\n            But_env.click()\n\n            time.sleep(10)\n\n            browser.save_screenshot('Chat.png')\n\n            # Guardar texto en   img\n            img = cv2.imread('Chat.png')\n            cv2.imshow(\"original\", img)\n\n            # Cropping an image\n                    #eje y superior \/in eje x menor \/  major\n            cropped_image = img&#91;780:820, 650:1700 ] \n\n            # Display cropped image\n            cv2.imshow(\"cropped\", cropped_image)\n            cv2.imwrite(\"Cropped Image_old.jpg\", cropped_image)\n            cv2.waitKey(27)\n            \n\n            time.sleep(0.5)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The next code is the implementation of ChatGPT in WhatsApp to automate chat responses. WhatsApp doesn&#8217;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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/rodamoya.com\/index.php\/2023\/04\/28\/chat-gpt-in-whatsapp\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;Chat GPT in WhatsApp&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts\/480"}],"collection":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/comments?post=480"}],"version-history":[{"count":8,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts\/480\/revisions"}],"predecessor-version":[{"id":569,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts\/480\/revisions\/569"}],"wp:attachment":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/media?parent=480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/categories?post=480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/tags?post=480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}