This repository has been archived by the owner on Sep 25, 2022. It is now read-only.
/
MatterBridgeConnection.py
131 lines (105 loc) · 3.71 KB
/
MatterBridgeConnection.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
from cmath import exp
import json
import requests
import threading
import time
import traceback
from queue import Queue
class MatterBridgeConnection:
_send_queue = Queue()
_recv_queue = Queue()
_connected = False
_running = False
def __init__(self, api, authToken, gateway):
self._api = api
self._authHeaders = {'Authorization': 'Bearer ' + authToken}
self._gateway = gateway
def run(self):
self._running = True
self.connect()
self._recv_thread = threading.Thread(target=self.listen)
self._recv_thread.start()
def connect(self):
print("[MatterBridge] Connecting...")
self._connected = False
try:
r = requests.get(self._api + "/stream",
headers=self._authHeaders, stream=True)
if r.encoding is None:
r.encoding = 'utf-8'
rlines = r.iter_lines(decode_unicode=True)
initInfo = json.loads(next(rlines))
if(initInfo["event"] == "api_connected"):
print("[MatterBridge] Connected")
self._rlines = rlines
self._connected = True
else:
print("[MatterBridge] Failed to connect")
except:
self._connected = False
print("[MatterBridge] Failed to connect")
print(traceback.format_exc())
return
def listen(self):
while self._running:
while not self._connected:
self.connect()
time.sleep(5)
try:
for line in self._rlines:
if line:
message = json.loads(line)
if(message["text"].startswith("!ts ")):
self._recv_queue.put(
("GLOBALMSG", message["username"],
message["protocol"],
message["text"][4:]))
else:
self._recv_queue.put(
("MSG", message["username"], message["protocol"], message["text"]))
except:
print(
"[MatterBridge] Error while fetching message from MatterBridge API")
print(traceback.format_exc())
self._connected = False
return
def relay_message(self, user, msg):
if not self._connected:
return
data = {
"text": msg,
"username": "[TeamSpeak] " + user,
"gateway": self._gateway
}
try:
requests.post(self._api + "/message",
headers=self._authHeaders, json=data, timeout=1)
except:
print("[MatterBridge] Error while sending message to MatterBridge API")
print(traceback.format_exc())
def send_text(self, text):
if not text:
return
data = {
"text": text,
"username": "[TeamSpeak Server] ",
"gateway": self._gateway
}
try:
requests.post(self._api + "/message",
headers=self._authHeaders, json=data, timeout=1)
except:
print("[MatterBridge] Error while sending message to MatterBridge API")
print(traceback.format_exc())
def send_raw(self, text):
msg = "%s\r\n" % (text, )
self._send_queue.put(msg)
def poll(self):
if self._recv_queue.empty():
return None
return self._recv_queue.get()
def disconnect(self):
self._running = False
self._connected = False
def running(self):
return self._running