matrix-shoutbox

git clone https://git.tarina.org/matrix-shoutbox
Log | Files | Refs

commit 940ca2145a0293c15061809df26c3e2ac3e2e6a2
Author: rbckman <rob@tarina.org>
Date:   Sun, 19 Apr 2020 15:24:28 +0300

first

Diffstat:
Aclient.py | 16++++++++++++++++
Aconfig.py | 5+++++
Arecv.py | 20++++++++++++++++++++
Aserver.py | 43+++++++++++++++++++++++++++++++++++++++++++
Atest.html | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 147 insertions(+), 0 deletions(-)

diff --git a/client.py b/client.py @@ -0,0 +1,16 @@ +import asyncio +import sys + +async def tcp_echo_client(message, loop): + reader, writer = await asyncio.open_connection('127.0.0.1', 8888, loop=loop) + print('Send: %r' % message) + writer.write(message.encode()) + data = await reader.read(1000) + print('Received: %r' % data.decode()) + print('Close the socket') + writer.close() + +message = sys.argv[1] +loop = asyncio.get_event_loop() +loop.run_until_complete(tcp_echo_client(message, loop)) +loop.close() diff --git a/config.py b/config.py @@ -0,0 +1,5 @@ +matrixserver="" +username="" +password="" +room_id="" +display_name="" diff --git a/recv.py b/recv.py @@ -0,0 +1,20 @@ +import asyncio +from nio import (AsyncClient, RoomMessageText) +import config + +open('msg.txt', 'w').close() + +async def message_cb(room, event): + if room.display_name == config.display_name: + print("{}: {}".format(room.user_name(event.sender), event.body)) + msg_file = open('msg.txt', 'a') + msg_file.write(room.user_name(event.sender) + ': ' + event.body + '\n') + +async def main(): + client = AsyncClient(config.matrixserver, config.username) + client.add_event_callback(message_cb, RoomMessageText) + + await client.login(config.password) + await client.sync_forever(timeout=30000) + +asyncio.get_event_loop().run_until_complete(main()) diff --git a/server.py b/server.py @@ -0,0 +1,43 @@ +import asyncio +from nio import AsyncClient +import config + +async def handle_echo(reader, writer): + client = AsyncClient(config.matrixserver, config.username) + await client.login(config.password) + + data = await reader.read(512) + message = data.decode() + addr = writer.get_extra_info('peername') + print("Received %r from %r" % (message, addr)) + writer.write(data) + await writer.drain() + print("Send: %r" % message) + print("Close the client socket") + writer.close() + print('sending msg to matrix') + await client.room_send( + room_id=config.room_id, + message_type="m.room.message", + content={ + "msgtype": "m.text", + "body": message + } + ) + await client.close() + +loop = asyncio.get_event_loop() +coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop) +server = loop.run_until_complete(coro) + +# Serve requests until Ctrl+C is pressed +print('Serving on {}'.format(server.sockets[0].getsockname())) +try: + loop.run_forever() +except KeyboardInterrupt: + pass + +# Close the server +server.close() +loop.run_until_complete(server.wait_closed()) +loop.close() diff --git a/test.html b/test.html @@ -0,0 +1,63 @@ +<!DOCTYPE html> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=utf8" /> + <script type="text/javascript"> + function refresh() { + var req = new XMLHttpRequest(); + console.log("Grabbing Value"); + req.onreadystatechange = function () { + if (req.readyState == 4 && req.status == 200) { + document.getElementById('matrix-shoutbox').innerText = req.responseText; + } + } + req.open("GET", 'msg.txt', true); + req.send(null); + } + + function init() { + refresh() + var int = self.setInterval(function () { + refresh() + }, 1000); + } + </script> +</head> + +<body onload="init()"> + <div id="main"> + <div id="updateMe"> + <h2>Shout out to Radio Rymd!</h2> + <form> + <input id="msg"></input> + <button id="button">send</button> + </form> + <span id="send">transmitting...</span> + <pre id="matrix-shoutbox"></pre> + </div> + </div> +</body> +</html> + +<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> +<script> +$(document).ready(function(){ + $('#send').hide(); +}); +$("button").click(function(e) { + var msg = $("input#textfield").val() + e.preventDefault(); + $.ajax({ + type: "POST", + url: "/shoutout", + data: {signal:msg}, + success: function(result) { + $('#send').show(1500); + $('#send').hide(1500); + alert('ok'); + }, + error: function(result) { + alert('error'); + } + }); +}); +</script>