Last active
September 24, 2024 11:07
-
-
Save ronnywang/4a466f963bc2419ec1e454bd9ee64039 to your computer and use it in GitHub Desktop.
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
<?php | |
$time = null; | |
$cur = filesize("log.jsonl"); | |
while (true) { | |
sleep(1); | |
$obj = json_decode(file_get_contents('https://g0v-slack-archive.g0v.ronny.tw/index/getmessage?channel=C056EHM42B1')); | |
if (is_null($time)) { | |
$time = $obj->messages[0]->ts; | |
continue; | |
} | |
if ($obj->messages[0]->ts > $time) { | |
$text = strip_tags($obj->messages[0]->html_content); | |
echo "from slack: " . $text; | |
file_put_contents("message.jsonl", $text . "\n", FILE_APPEND); | |
$time = $obj->messages[0]->ts; | |
} | |
clearstatcache(); | |
if (filesize("log.jsonl") > $cur) { | |
$fp = fopen("log.jsonl", "r"); | |
fseek($fp, $cur); | |
while ($line = fgets($fp)) { | |
$log = json_decode($line); | |
if ($log->decoded->portnum == 'TEXT_MESSAGE_APP') { | |
$text = $log->decoded->text; | |
echo "from meshtastic: " . $text; | |
$cmd = "curl -XPOST -d 'text=" . urlencode($text) . "' 'https://meet.jothon.online/api/postMessage?token=vnahjt7xwhDUAUmiG0zd5COz537O7FxE&channel=%23civil-defense'"; | |
shell_exec($cmd); | |
} | |
} | |
fclose($fp); | |
$cur = filesize("log.jsonl"); | |
} | |
} |
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
import time | |
import os | |
import json | |
import meshtastic | |
import meshtastic.serial_interface | |
from rich import print as rprint | |
from pubsub import pub | |
def onReceive(packet, interface): | |
indent_packet = json.dumps(packet, indent=2, default=lambda s: " ".join(str(s).split())) | |
if packet['decoded']['portnum'] != 'POSITION_APP' and packet['decoded']['portnum'] != 'TELEMETRY_APP': | |
print(indent_packet) | |
# write one line json to log.jsonl without json encode | |
packet = json.dumps(packet, default=lambda s: " ".join(str(s).split())) | |
with open('log.jsonl', 'a') as f: | |
f.write(packet + "\n") | |
def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect to the radio | |
# defaults to broadcast, specify a destination ID if you wish | |
print("Connected to radio") | |
pub.subscribe(onReceive, "meshtastic.receive") | |
pub.subscribe(onConnection, "meshtastic.connection.established") | |
interface = meshtastic.serial_interface.SerialInterface() | |
node = interface.nodes | |
packet = json.dumps(node, indent=2, default=lambda s: " ".join(str(s).split())) | |
# write packet to config.json | |
with open('config.json', 'w') as f: | |
f.write(packet) | |
# empty log.jsonl | |
open('log.jsonl', 'w').close() | |
# get the filesize of message.jsonl | |
cursor = os.path.getsize('message.jsonl') | |
while True: | |
time.sleep(1) | |
if cursor < os.path.getsize('message.jsonl'): | |
# read line from cursor = cursor | |
with open('message.jsonl') as f: | |
f.seek(cursor) | |
for line in f: | |
interface.sendText(line, channelIndex=2) | |
print("New message added to message.jsonl, sending message to radio" + line) | |
cursor = os.path.getsize('message.jsonl') | |
interface.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment