From accb8ff2f78dadf18f6b592e46447e73b92e1e74 Mon Sep 17 00:00:00 2001 From: specialOne-coder Date: Tue, 31 Jan 2023 19:32:34 +0100 Subject: [PATCH] Update: Improved python code to use openai directly with an api key and support for quoted messages. --- main.go | 19 ++++++++++++++--- server.py | 61 +++++++------------------------------------------------ 2 files changed, 23 insertions(+), 57 deletions(-) diff --git a/main.go b/main.go index 560b28e..9bb480b 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ import ( "os" "os/signal" "syscall" - _ "github.com/mattn/go-sqlite3" "github.com/mdp/qrterminal/v3" "go.mau.fi/whatsmeow" @@ -33,8 +32,22 @@ func (mycli *MyClient) register() { func (mycli *MyClient) eventHandler(evt interface{}) { switch v := evt.(type) { case *events.Message: - newMessage := v.Message - msg := newMessage.GetConversation() + // Ignore group messages + // Group messages cause a panic, so we need to check if it's a group message + if v.Info.IsGroup { + return + } + // Support quoted messages + // Whenever someone replies to your message by swiping left on it + var newMessage *waProto.Message + newMessage = v.Message + quoted := newMessage.ExtendedTextMessage + var msg string + if quoted == nil { + msg = newMessage.GetConversation() + } else { + msg = quoted.GetText() + } fmt.Println("Message from:", v.Info.Sender.User, "->", msg) if msg == "" { return diff --git a/server.py b/server.py index d93a7d3..6560a10 100644 --- a/server.py +++ b/server.py @@ -1,75 +1,28 @@ """Make some requests to OpenAI's chatbot""" -import time -import os import flask import sys +import openai from flask import g -from playwright.sync_api import sync_playwright -PROFILE_DIR = "/tmp/playwright" if '--profile' not in sys.argv else sys.argv[sys.argv.index('--profile') + 1] PORT = 5001 if '--port' not in sys.argv else int(sys.argv[sys.argv.index('--port') + 1]) APP = flask.Flask(__name__) -PLAY = sync_playwright().start() -BROWSER = PLAY.chromium.launch_persistent_context( - user_data_dir=PROFILE_DIR, - headless=False, -) -PAGE = BROWSER.new_page() +openai.api_key = 'Your OpenAI API Key' -def get_input_box(): - """Find the input box by searching for the largest visible one.""" - textareas = PAGE.query_selector_all("textarea") - candidate = None - for textarea in textareas: - if textarea.is_visible(): - if candidate is None: - candidate = textarea - elif textarea.bounding_box().width > candidate.bounding_box().width: - candidate = textarea - return candidate - -def is_logged_in(): - try: - return get_input_box() is not None - except AttributeError: - return False - -def send_message(message): - # Send the message - box = get_input_box() - box.click() - box.fill(message) - box.press("Enter") - while PAGE.query_selector(".result-streaming") is not None: - time.sleep(0.1) - -def get_last_message(): - """Get the latest message""" - page_elements = PAGE.query_selector_all(".flex.flex-col.items-center > div") - last_element = page_elements[-2] - return last_element.inner_text() @APP.route("/chat", methods=["GET"]) def chat(): message = flask.request.args.get("q") print("Sending message: ", message) - send_message(message) - response = get_last_message() + response = openai.Completion.create(model="text-davinci-003", prompt=""+ message, max_tokens=200,top_p=0.0,frequency_penalty=0.5,presence_penalty=0.0) print("Response: ", response) - return response + return response.choices[0].text -def start_browser(): - PAGE.goto("https://chat.openai.com/") +def startAI(): APP.run(port=PORT, threaded=False) - if not is_logged_in(): - print("Please log in to OpenAI Chat") - print("Press enter when you're done") - input() - else: - print("Logged in") + if __name__ == "__main__": - start_browser() \ No newline at end of file + startAI() \ No newline at end of file