Last active
April 27, 2018 15:56
-
-
Save soundstorm/91cc6553b117b98b919498b99d3836e2 to your computer and use it in GitHub Desktop.
Public channel for Rocket.Chat without registration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
ws_port = 8000 # public [e.g. via proxy] | |
http_port = 8001 # just locally accessible | |
guest_identifier = ' [GUEST]' | |
incoming_url = 'https://example.org/hooks/token' | |
# First message to websocket will be the username, all afterwards will be sent to all connected WebSocket clients and Rocket.Chat | |
# Install SimpleWebSocketServer: | |
# pip install git+https://github.com/dpallot/simple-websocket-server.git | |
# Outgoing webhook posts to http://localhost:http_port/ with following script: | |
''' | |
class Script { | |
prepare_outgoing_request({ request }) { | |
if (!request.data.bot) { return request; } | |
} | |
process_outgoing_response({ request, response }) { | |
return; | |
} | |
} | |
''' | |
# Define outgoing webhook to listen on the same channel the incoming webhook posts to | |
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket | |
import sys, json, logging, cgi | |
pyth_vers = sys.version_info[0] | |
# Version specific imports | |
if pyth_vers == 3: | |
from http.server import BaseHTTPRequestHandler, HTTPServer | |
from _thread import start_new_thread | |
import urllib.request, urllib.parse | |
else: | |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
from thread import start_new_thread | |
import urllib, urllib2 | |
clients = [] | |
class PublicChat(WebSocket): | |
username = None | |
def postRocket(self, msg): | |
data = { | |
"text": msg, | |
"username": self.username | |
} | |
try: | |
if pyth_vers == 3: | |
return urllib.request.urlopen(urllib.request.Request(incoming_url, urllib.parse.urlencode(data).encode("utf-8"))) | |
else: | |
return urllib2.urlopen(incoming_url, urllib.urlencode(data, 'utf-8')) | |
except Exception as e: | |
loggin.error(e) | |
def handleMessage(self): | |
if self.username == None: | |
self.username = self.data | |
self.postRocket(self.username + ' has connected') | |
self.sendMessage('connected') | |
else: | |
self.postRocket(self.data) | |
for client in clients: | |
try: | |
client.sendMessage('<strong>' + self.username + guest_identifier + '</strong> ' + cgi.escape(self.data)) | |
except Exception as e: | |
loggin.error(e) | |
def handleConnected(self): | |
clients.append(self) | |
logging.info(self.address[0] + ' has connected') | |
def handleClose(self): | |
clients.remove(self) | |
if self.username != None: | |
self.postRocket(self.username + " has disconnected") | |
logging.info(self.address[0] + ' closed connection') | |
class S(BaseHTTPRequestHandler): | |
def _set_headers(self): | |
self.send_response(200) | |
self.send_header('Content-type', 'text/json') | |
self.end_headers() | |
def do_POST(self): | |
content_length = int(self.headers['Content-Length']) | |
post_data = self.rfile.read(content_length) | |
data = json.loads(post_data.decode('utf-8')) | |
if data['bot'] == False: # don't repost if using default outgoing script | |
for client in clients: | |
client.sendMessage('<strong>@' + data['user_name'] + '</strong> ' + cgi.escape(data['text'])) | |
self._set_headers() | |
self.wfile.write('{}') | |
def runServer(): | |
logging.basicConfig(level=logging.INFO) | |
server_address = ('', http_port) | |
httpd = server_class(server_address, S) | |
logging.info('Starting httpd on port ' + str(http_port) + '...') | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: # Exception will not really occur as the thread is killed by the main process | |
pass | |
httpd.server_close() | |
logging.info('Stopping httpd...') | |
start_new_thread(runServer, ()) | |
server = SimpleWebSocketServer('', ws_port, PublicChat) | |
server.serveforever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment