4.1 KiB
Modules and Adding Features
This document explains how to add new modules and commands to your Meshtastic mesh-bot project.
Table of Contents
- Overview
- Adding a New Command
- Running a Shell Command
- Best Practices
- Technical Assistance & Troubleshooting
Overview
For code testing, see etc/simulator.py to simulate a bot.
You can also use meshtasticd (Linux-native) in noradio mode with MQTT server and client to emulate a mesh network.
Adding a New Command
Follow these steps to add a new BBS option or command to the bot:
1. Define the Command Handler
Add a new function in mesh_bot.py to handle your command.
Example for a command called newcommand:
def handle_newcommand(message, message_from_id, deviceID):
return "This is a response from the new command."
If your command is complex, consider creating a new module (e.g., modules/newcommand.py).
Import your new module where needed (see modules/system.py for examples).
2. Add the Command to the Auto Response
Update the auto_response function in mesh_bot.py to include your new command:
def auto_response(message, snr, rssi, hop, pkiStatus, message_from_id, channel_number, deviceID, isDM):
#...
"newcommand": lambda: handle_newcommand(message, message_from_id, deviceID),
#...
3. Update the Trap List and Help
Edit modules/system.py to include your new command in the trap list and help message:
#...
trap_list = ("cmd", "cmd?", "newcommand") # Add your command here
help_message = "Bot CMD?:newcommand, "
#...
Preferred method:
Add a configuration block below ping (around line 28):
# newcommand Configuration
newcommand_enabled = True # settings.py handles config.ini values; this is a placeholder
if newcommand_enabled:
trap_list_newcommand = ("newcommand",)
trap_list = trap_list + trap_list_newcommand
help_message = help_message + ", newcommand"
4. Test the New Command
Run MeshBot and test your new command by sending a message with newcommand to ensure it responds correctly.
Running a Shell Command
You can make a command that calls a bash script on the system (requires the filemon module):
def auto_response(message, snr, rssi, hop, pkiStatus, message_from_id, channel_number, deviceID, isDM):
#...
"switchON": lambda: call_external_script(message)
This will call the default script located at script/runShell.sh and return its output.
Best Practices
- Modularize: Place complex or reusable logic in its own module.
- Document: Add docstrings and comments to your functions.
- Test: Use the simulator or a test mesh to verify new features.
- Update Help: Keep the help message and trap list up to date for users.
- Configuration: Use
settings.pyandconfig.inifor feature toggles and settings.
Technical Assistance & Troubleshooting
- Debug Logging:
Use theloggermodule for debug output. Check logs for errors or unexpected behavior. - Common Issues:
- Module Import Errors: Ensure your new module is in the
modules/directory and imported correctly. - Command Not Responding: Verify your command is in the trap list and auto_response dictionary.
- Configuration Problems: Double-check
settings.pyandconfig.inifor typos or missing entries.
- Module Import Errors: Ensure your new module is in the
- Testing:
- Use
etc/simulator.pyfor local testing without radio hardware. - Use
meshtasticdinnoradiomode for network emulation.
- Use
- Python Environment:
- Use a virtual environment (
python3 -m venv venv) to manage dependencies. - Install requirements with
pip install -r requirements.txt.
- Use a virtual environment (
- Updating Dependencies:
- try not to I want to remove some.
- Getting Help:
- Check the project wiki or issues page for common questions.
- Use inline comments and docstrings for clarity.
- If you’re stuck, ask for help on the project’s GitHub Discussions or Issues tab.
Happy hacking!