From e5cc90ea5e26cd6c5d2d01f33546302ab3cc4cd0 Mon Sep 17 00:00:00 2001 From: wwj718 Date: Mon, 6 Apr 2020 21:44:24 +0800 Subject: [PATCH] add webhook extension --- extensions_v2/extension_webhook.py | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 extensions_v2/extension_webhook.py diff --git a/extensions_v2/extension_webhook.py b/extensions_v2/extension_webhook.py new file mode 100644 index 0000000..79122d4 --- /dev/null +++ b/extensions_v2/extension_webhook.py @@ -0,0 +1,58 @@ +''' +局域网通信 +发送和接受http + +消息 + +对应积木 + +Adapter Webhook +''' + +import time +import socket +from codelab_adapter.core_extension import Extension +from codelab_adapter.utils import threaded +import webbrowser +from bottle import route, run, template +# queue +import queue +q = queue.Queue() + +@route('/webhook/') +def index(message): + html = ''' + ok! + ''' + if message == "hello": + # hello page + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect(("8.8.8.8", 80)) + html = f'''ok! +

IP: {s.getsockname()[0]}

+

Adapter Webhook url: http://{s.getsockname()[0]}/webhook/MESSAGE

+ ''' + q.put(message) + return template(html, message=message) + +class WebServerExtension(Extension): + def __init__(self): + super().__init__() + self.port = 8080 + + @threaded + def run_webserver_as_thread(self): + run(host='0.0.0.0', port=self.port) + + def run(self): + self.run_webserver_as_thread() + time.sleep(0.5) + webbrowser.open(f'http://127.0.0.1:{self.port}/webhook/hello') + while self._running: + message_web = q.get() + message = self.message_template() + message["payload"]["content"] = message_web + self.publish(message) + + +export = WebServerExtension \ No newline at end of file