-
Notifications
You must be signed in to change notification settings - Fork 10
/
TkDocsBot.py
135 lines (119 loc) · 4.19 KB
/
TkDocsBot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import datetime
import websocket
import websockets
import asyncio
import sqlite3
import json
import requests
from bs4 import BeautifulSoup
import tkinter as tk
from tkinter import messagebox
inputs = []
outputs = []
used_ports = []
root = tk.Tk()
root.title("WebSocket Client")
# UI Elements
input_text = tk.Text(root, height=20, width=150)
output_text = tk.Text(root, height=20, width=150)
clear_button = tk.Button(root, text="Clear Textboxes")
port_label = tk.Label(root, text="Enter Port:")
port_entry = tk.Entry(root)
port_slider = tk.Scale(root, from_=1000, to=9999, orient=tk.HORIZONTAL, label="Websocket server port")
start_button = tk.Button(root, text="Start WebSocket client")
stop_button = tk.Button(root, text="Stop WebSocket client")
user_input = tk.Text(root, height=2, width=150)
ask_agent = tk.Button(root, text="Ask the agent")
websocket_ports = tk.Text(root, height=1, width=150)
# UI Layout
input_text.pack()
output_text.pack()
port_label.pack()
port_entry.pack()
port_slider.pack()
start_button.pack()
stop_button.pack()
user_input.pack()
ask_agent.pack()
clear_button.pack()
websocket_ports.pack()
# Function to update the UI
def update_ui():
root.update()
# Function to clear the textboxes
def clear_textboxes():
input_text.delete('1.0', tk.END)
output_text.delete('1.0', tk.END)
user_input.delete('1.0', tk.END)
# Function to send a question to the chatbot and get the response
async def askQuestion(question):
url = 'https://api.docsbot.ai/teams/ZrbLG98bbxZ9EFqiPvyl/bots/oFFiXuQsakcqyEdpLvCB/chat'
headers = {
'Content-Type': 'application/json'
}
data = {
'question': question,
'full_source': False
}
try:
response = requests.post(url, headers=headers, json=data)
responseText = response.content.decode('utf-8')
return responseText
except requests.exceptions.RequestException as e:
# Handle request exceptions here
print(f"Request failed with exception: {e}")
async def start_client(websocketPort, iface):
uri = f'ws://localhost:{websocketPort}'
used_ports.append(websocketPort)
async with websockets.connect(uri) as ws:
await handleWebSocket(ws, '/')
async def handleWebSocket(ws, path):
print('New connection')
try:
async for message in ws:
print(f'Received message: {message}')
parsedMessage = json.loads(message)
inputMsg = {"Server: ": message}
input_text.insert(tk.END, str(inputMsg) + '\n')
update_ui() # Update the UI after inserting the message
try:
loop = asyncio.get_event_loop()
answer = await askQuestion(parsedMessage)
print(answer)
message = {"Docsbot(Wordpress) agent: ": answer}
output_text.insert(tk.END, str(message) + '\n')
update_ui() # Update the UI after inserting the answer
await ws.send(json.dumps(message))
except websockets.exceptions.ConnectionClosedError as e:
print(f"Connection closed: {e}")
except Exception as e:
print(f"Error: {e}")
except Exception as e:
print(f"Error: {e}")
def get_port():
if port_entry.get():
return int(port_entry.get())
else:
return port_slider.get()
def start_client_callback():
websocketPort = get_port()
websocket_ports.insert(tk.END, str(websocketPort))
update_ui()
loop = asyncio.get_event_loop()
loop.run_until_complete(start_client(websocketPort, None))
def stop_client_callback():
# Add your stop client logic here
pass
async def askAgent():
question = user_input.get('1.0','end')
try:
response = await askQuestion(question)
print(response)
output_text.insert('1.0', str(answer))
except Exception as e:
print(f"Error: {e}")
clear_button.config(command=clear_textboxes)
start_button.config(command=start_client_callback)
stop_button.config(command=stop_client_callback)
ask_agent.config(command=askAgent)
root.mainloop()