-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatxai.py
executable file
·52 lines (37 loc) · 1.5 KB
/
chatxai.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
#!/usr/bin/env python3
import slixmpp
import asyncio
import subprocess
import config
from command_runner import run_tool
class XMPPListenerBot(slixmpp.ClientXMPP):
def __init__(self, jid, password):
super().__init__(jid, password)
# Attach the event handler for session start
self.add_event_handler("session_start", self.start)
# Attach the event handler for receiving messages
self.add_event_handler("message", self.message)
async def start(self, event):
# Send presence to notify the server that we're available
self.send_presence()
await self.get_roster()
print("Connected to XMPP server and ready to receive messages.")
def message(self, msg):
# Extract the command from the message body
argument = msg['body']
print(f"Received command: {argument}")
#Run the tool to fetch the data and save it to output variable.
output=run_tool(argument)
# Print the command output (for server-side logging)
print(f"Command output: {output}")
# Reply to the sender with the command output
msg.reply(f"Command output:\n{output}").send()
if (len(config.JID)+len(config.PASSWORD)) > 1:
# Create the bot and connect
xmpp = XMPPListenerBot(config.JID, config.PASSWORD)
# Connect to the XMPP server and start listening
xmpp.connect()
# Start processing XMPP messages
xmpp.process(forever=True)
else:
print("ERROR: Configure the XMPP account on config.py")