Skip to content

Commit 5cc62f1

Browse files
authored
Merge pull request #26 from F33RNI/next
Next
2 parents 9b40fa8 + 228a2e6 commit 5cc62f1

14 files changed

+254
-68
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ chats/
66
users.json
77
conversations/
88
EdgeGPT_cookies.json
9+
data/

Diff for: BardModule.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def __init__(self, config: dict, messages: dict, users_handler: UsersHandler.Use
3232
self._enabled = False
3333
self._chatbot = None
3434
self._restart_attempts = 0
35+
self._proxy = None
3536

3637
def initialize(self) -> None:
3738
"""
@@ -51,8 +52,11 @@ def initialize(self) -> None:
5152
# Set proxy
5253
proxy = self.config["bard"]["proxy"]
5354
if proxy and len(proxy) > 1 and proxy.strip().lower() != "auto":
55+
self._proxy = proxy
5456
self._chatbot.session.proxies.update({"http": proxy,
5557
"https": proxy})
58+
else:
59+
self._proxy = None
5660

5761
# Done?
5862
if self._chatbot is not None:
@@ -67,14 +71,17 @@ def initialize(self) -> None:
6771

6872
def set_proxy(self, proxy: str) -> None:
6973
"""
70-
Sets new proxy
74+
Sets new proxy from ProxyAutomation
75+
self.config["bard"]["proxy"] must be "auto"
7176
:param proxy: https proxy but in format http://IP:PORT
7277
:return:
7378
"""
74-
if not self._enabled or self._chatbot is None:
79+
if self.config["bard"]["proxy"].strip().lower() != "auto":
7580
return
76-
if self.config["bard"]["proxy"].strip().lower() == "auto":
77-
logging.info("Setting proxy {0} for Bard module".format(proxy))
81+
82+
logging.info("Setting proxy {0} for Bard module".format(proxy))
83+
self._proxy = proxy
84+
if self._enabled and self._chatbot is not None:
7885
self._chatbot.session.proxies.update({"http": proxy,
7986
"https": proxy})
8087

@@ -192,16 +199,19 @@ def restart(self):
192199
Restarts module and saves proxy
193200
:return:
194201
"""
195-
if not self._enabled or self._chatbot is None:
202+
if not self.config["modules"]["bard"]:
196203
return
197204
logging.info("Restarting Bard module")
198205

199-
# Save proxy
200-
proxy = self._chatbot.session.proxies
201-
202206
# Restart
203207
self.exit()
204208
self.initialize()
205209

206210
# Set proxy
207-
self._chatbot.session.proxies = proxy
211+
try:
212+
if self._proxy is not None:
213+
self._chatbot.session.proxies.update({"http": self._proxy,
214+
"https": self._proxy})
215+
except Exception as e:
216+
logging.error("Error setting back proxy to Bard module!", exc_info=e)
217+

Diff for: BotHandler.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
import asyncio
19+
import datetime
1920
import logging
2021
import queue
2122
import threading
@@ -582,18 +583,32 @@ async def bot_command_or_message_request(self, request_type: int,
582583

583584
# Check request
584585
if not request_message or len(request_message) <= 0:
585-
await _send_safe(user["user_id"], self.messages["empty_request"], context)
586+
# Module changed
587+
if self.config["modules"]["auto_module"]:
588+
await _send_safe(user["user_id"],
589+
self.messages["empty_request_module_changed"]
590+
.format(self.messages["modules"][request_type]), context)
591+
592+
# Empty request
593+
else:
594+
await _send_safe(user["user_id"], self.messages["empty_request"], context)
586595
return
587596

588597
# Check queue
589598
if self.queue_handler.requests_queue.full():
590599
await _send_safe(user["user_id"], self.messages["queue_overflow"], context)
591600
return
592601

602+
# Create request timestamp (for data collecting)
603+
request_timestamp = ""
604+
if self.config["data_collecting"]["enabled"]:
605+
request_timestamp = datetime.datetime.now().strftime(self.config["data_collecting"]["timestamp_format"])
606+
593607
# Create request
594608
request_response = RequestResponseContainer.RequestResponseContainer(user, update.message.message_id,
595609
request=request_message,
596-
request_type=request_type)
610+
request_type=request_type,
611+
request_timestamp=request_timestamp)
597612

598613
# Add request to the queue
599614
logging.info("Adding new {0} request from {1} ({2}) to the queue".format(request_type,

Diff for: ChatGPTModule.py

+27-14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(self, config: dict, messages: dict, users_handler: UsersHandler.Use
4040
self._exit_flag = False
4141
self._processing_flag = False
4242
self._restart_attempts = 0
43+
self._proxy = None
4344

4445
def initialize(self) -> None:
4546
"""
@@ -55,7 +56,7 @@ def initialize(self) -> None:
5556
return
5657

5758
# Get API type from config
58-
self._api_type = int(self.config["modules"]["chatgpt_api_type"])
59+
self._api_type = int(self.config["chatgpt"]["api_type"])
5960

6061
# Get conversations directory
6162
self._conversations_dir = self.config["files"]["conversations_dir"]
@@ -77,9 +78,13 @@ def initialize(self) -> None:
7778
logging.info("Initializing ChatGPT module with API type 3")
7879
from revChatGPT.V3 import Chatbot
7980
engine = str(self.config["chatgpt"]["engine"])
80-
proxy = str(self.config["chatgpt"]["proxy"])
81-
if proxy.strip().lower() == "auto":
81+
82+
proxy = self.config["edgegpt"]["proxy"]
83+
if proxy and len(proxy) > 1 and proxy.strip().lower() != "auto":
84+
self._proxy = proxy
85+
else:
8286
proxy = ""
87+
self._proxy = None
8388

8489
if len(engine) > 0:
8590
self._chatbot = Chatbot(str(self.config["chatgpt"]["api_key"]),
@@ -104,14 +109,17 @@ def initialize(self) -> None:
104109

105110
def set_proxy(self, proxy: str) -> None:
106111
"""
107-
Sets new proxy
112+
Sets new proxy from ProxyAutomation
113+
self.config["chatgpt"]["proxy"] must be "auto"
108114
:param proxy: https proxy but in format http://IP:PORT
109115
:return:
110116
"""
111-
if not self._enabled or self._chatbot is None:
117+
if self.config["chatgpt"]["proxy"].strip().lower() != "auto":
112118
return
113-
if self.config["chatgpt"]["proxy"].strip().lower() == "auto":
114-
logging.info("Setting proxy {0} for ChatGPT module".format(proxy))
119+
120+
logging.info("Setting proxy {0} for ChatGPT module".format(proxy))
121+
self._proxy = proxy
122+
if self._enabled and self._chatbot is not None:
115123
self._chatbot.proxy = proxy
116124

117125
def process_request(self, request_response: RequestResponseContainer) -> None:
@@ -312,19 +320,20 @@ def restart(self):
312320
Restarts module and saves proxy
313321
:return:
314322
"""
315-
if not self._enabled or self._chatbot is None:
323+
if not self.config["modules"]["chatgpt"]:
316324
return
317325
logging.info("Restarting ChatGPT module")
318326

319-
# Save proxy
320-
proxy = self._chatbot.proxy
321-
322327
# Restart
323328
self.exit()
324329
self.initialize()
325330

326331
# Set proxy
327-
self._chatbot.proxy = proxy
332+
try:
333+
if self._proxy is not None:
334+
self._chatbot.proxy = self._proxy
335+
except Exception as e:
336+
logging.error("Error setting back proxy to ChatGPT module!", exc_info=e)
328337

329338
def _save_conversation(self, conversation_id) -> bool:
330339
"""
@@ -458,8 +467,12 @@ def _get_chatbot_config(self) -> dict:
458467
raise Exception("Error! No credentials to login!")
459468

460469
# Add proxy
461-
if len(self.config["chatgpt"]["proxy"]) > 0 and self.config["chatgpt"]["proxy"].strip().lower() != "auto":
462-
config["proxy"] = self.config["chatgpt"]["proxy"]
470+
proxy = self.config["edgegpt"]["proxy"]
471+
if proxy and len(proxy) > 1 and proxy.strip().lower() != "auto":
472+
self._proxy = proxy
473+
config["proxy"] = proxy
474+
else:
475+
self._proxy = None
463476

464477
# Paid?
465478
config["paid"] = self.config["chatgpt"]["paid"]

Diff for: DALLEModule.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, config: dict, messages: dict, users_handler: UsersHandler.Use
3131

3232
self._enabled = False
3333
self._restart_attempts = 0
34+
self._proxy = None
3435

3536
def initialize(self) -> None:
3637
"""
@@ -50,7 +51,10 @@ def initialize(self) -> None:
5051
# Proxy for DALL-E
5152
proxy = self.config["dalle"]["proxy"]
5253
if proxy and len(proxy) > 1 and proxy.strip().lower() != "auto":
54+
self._proxy = proxy
5355
openai.proxy = proxy
56+
else:
57+
self._proxy = None
5458

5559
# Done?
5660
logging.info("DALL-E module initialized")
@@ -62,15 +66,17 @@ def initialize(self) -> None:
6266

6367
def set_proxy(self, proxy: str) -> None:
6468
"""
65-
Sets new proxy
69+
Sets new proxy from ProxyAutomation
70+
self.config["dalle"]["proxy"] must be "auto"
6671
:param proxy: https proxy but in format http://IP:PORT
6772
:return:
6873
"""
69-
if not self._enabled:
74+
if self.config["dalle"]["proxy"].strip().lower() != "auto":
7075
return
71-
if self.config["dalle"]["proxy"].strip().lower() == "auto":
72-
logging.info("Setting proxy {0} for DALL-E module".format(proxy))
73-
openai.proxy = proxy
76+
77+
logging.info("Setting proxy {0} for DALL-E module".format(proxy))
78+
self._proxy = proxy
79+
openai.proxy = proxy
7480

7581
def process_request(self, request_response: RequestResponseContainer) -> None:
7682
"""
@@ -138,15 +144,16 @@ def restart(self):
138144
Restarts module and saves proxy
139145
:return:
140146
"""
141-
if not self._enabled:
147+
if not self.config["modules"]["dalle"]:
142148
return
143149
logging.info("Restarting DALL-E module")
144150

145-
# Save proxy
146-
proxy = openai.proxy
147-
148151
# Restart
149152
self.initialize()
150153

151154
# Set proxy
152-
openai.proxy = proxy
155+
try:
156+
if self._proxy is not None:
157+
openai.proxy = self._proxy
158+
except Exception as e:
159+
logging.error("Error setting back proxy to DALL-E module!", exc_info=e)

Diff for: EdgeGPTModule.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(self, config: dict, messages: dict, users_handler: UsersHandler.Use
5353
self._enabled = False
5454
self._chatbot = None
5555
self._restart_attempts = 0
56+
self._proxy = None
5657

5758
def initialize(self) -> None:
5859
"""
@@ -72,8 +73,10 @@ def initialize(self) -> None:
7273
if proxy and len(proxy) > 1 and proxy.strip().lower() != "auto":
7374
async_helper(self._chatbot.create(cookie_path=self.config["edgegpt"]["cookie_file"],
7475
proxy=proxy))
76+
self._proxy = proxy
7577
else:
7678
async_helper(self._chatbot.create(cookie_path=self.config["edgegpt"]["cookie_file"]))
79+
self._proxy = None
7780

7881
# Check
7982
if self._chatbot is not None:
@@ -86,14 +89,17 @@ def initialize(self) -> None:
8689

8790
def set_proxy(self, proxy: str) -> None:
8891
"""
89-
Sets new proxy
92+
Sets new proxy from ProxyAutomation
93+
self.config["edgegpt"]["proxy"] must be "auto"
9094
:param proxy: https proxy but in format http://IP:PORT
9195
:return:
9296
"""
93-
if not self._enabled or self._chatbot is None:
97+
if self.config["edgegpt"]["proxy"].strip().lower() != "auto":
9498
return
95-
if self.config["edgegpt"]["proxy"].strip().lower() == "auto":
96-
logging.info("Setting proxy {0} for EdgeGPT module".format(proxy))
99+
100+
logging.info("Setting proxy {0} for EdgeGPT module".format(proxy))
101+
self._proxy = proxy
102+
if self._enabled and self._chatbot is not None:
97103
self._chatbot.proxy = proxy
98104

99105
def process_request(self, request_response: RequestResponseContainer) -> None:
@@ -227,16 +233,17 @@ def restart(self):
227233
Restarts module and saves proxy
228234
:return:
229235
"""
230-
if not self._enabled or self._chatbot is None:
236+
if not self.config["modules"]["edgegpt"]:
231237
return
232238
logging.info("Restarting EdgeGPT module")
233239

234-
# Save proxy
235-
proxy = self._chatbot.proxy
236-
237240
# Restart
238241
self.exit()
239242
self.initialize()
240243

241244
# Set proxy
242-
self._chatbot.proxy = proxy
245+
try:
246+
if self._proxy is not None:
247+
self._chatbot.proxy = self._proxy
248+
except Exception as e:
249+
logging.error("Error setting back proxy to EdgeGPT module!", exc_info=e)

Diff for: ProxyAutomation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def proxy_tester_process(test_proxy_queue: multiprocessing.Queue,
6565
response = session.get(check_url, timeout=timeout)
6666

6767
# Check result
68-
if len(str(response.headers)) > 1:
68+
if len(str(response.headers)) > 1 and response.status_code == 200:
6969
# Put working proxy to the queue
7070
working_proxy_queue.put(proxy_to_test, block=True, timeout=1)
7171

@@ -248,7 +248,7 @@ def _automation_loop(self) -> None:
248248
try:
249249
response = session.get(self.config["proxy_automation"]["check_url"],
250250
timeout=self.config["proxy_automation"]["check_timeout_seconds"])
251-
is_proxy_working = len(str(response.headers)) > 1
251+
is_proxy_working = len(str(response.headers)) > 1 and response.status_code == 200
252252
except Exception as e:
253253
logging.error("Error checking proxy: {0}".format(str(e)))
254254
session.close()

0 commit comments

Comments
 (0)