Update for new Meshtastic version

master
Tanner Collin 3 weeks ago
parent 7a053a51fe
commit 4baa80657c
  1. 20
      main.py

@ -31,19 +31,19 @@ def int_to_mesh_id(num):
# example: -697240346 to !d670f4e6
hex_str = hex(num & 0xFFFFFFFF)
return '!' + hex_str[2:]
return '!' + hex_str[2:].zfill(8)
async def process_text(data):
msg_to = data.get('to', None)
if msg_to != -1:
if msg_to not in [-1, 4294967295]:
logging.debug('Message not for channel: %s', str(data))
return
try:
msg_text = data['payload']['text']
sender = data['from']
except KeyError:
except (KeyError, TypeError):
logging.info('Invalid payload: %s', str(data))
return
@ -51,20 +51,20 @@ async def process_text(data):
try:
from_name = storage['nodes'][sender_id]['long_name']
except KeyError:
from_name = 'Unknown sender'
except (KeyError, TypeError):
from_name = sender_id
logging.info('Channel message from %s: %s', from_name, msg_text)
msg = from_name + ': ' + msg_text
await bot.send_message(settings.CHAT_ID, msg)
await bot.send_message(settings.CHAT_ID, msg, link_preview=False)
async def process_info(data):
try:
node_id = data['payload']['id']
long_name = data['payload']['longname']
short_name = data['payload']['shortname']
except KeyError:
except (KeyError, TypeError):
logging.info('Invalid payload: %s', str(data))
return
@ -102,7 +102,7 @@ async def process_mqtt(message):
if msg_type == 'text':
await process_text(data)
if msg_type == 'nodeinfo':
elif msg_type == 'nodeinfo':
await process_info(data)
else:
logging.debug('Ignored message type %s: %s', msg_type, text)
@ -110,8 +110,8 @@ async def process_mqtt(message):
async def fetch_mqtt():
async with Client('localhost') as client:
async with client.filtered_messages('msh/+/json/#') as messages:
await client.subscribe('msh/+/json/#')
async with client.filtered_messages('msh/+/+/json/#') as messages:
await client.subscribe('msh/+/+/json/#')
async for message in messages:
await process_mqtt(message)

Loading…
Cancel
Save