- Move n2yo_api_key and airnow_api_key from [Solar_Config] to [External_Data] - Update code references to use [External_Data] section instead of [Solar_Config] - Update config.ini.example to match the new organization - Improve error message to specify the correct config section - This provides better organization with all external API keys in one place
MeshCore Bot Framework
A Python framework for creating bots that connect to MeshCore networks via serial port or BLE. This bot can respond to messages containing specific keywords, manage user bans, send scheduled messages, and support future integrations with external data sources like weather and tide APIs.
Features
- Multi-Protocol Support: Connect via serial port or Bluetooth Low Energy (BLE)
- Keyword Response System: Configurable keyword-response pairs with template variables
- Rate Limiting: Configurable rate limiting to prevent spam
- User Management: Ban/unban users with persistent configuration
- Scheduled Messages: Send messages at specific times
- DM Support: Respond to direct messages
- Comprehensive Logging: Colored console output and file logging
- CLI Interface: Interactive and command-line management tools
- Extensible Architecture: Easy to add new features and integrations
Requirements
- Python 3.7+
- MeshCore-compatible device (Heltec V3, RAK Wireless, etc.)
- USB cable or BLE capability
Installation
- Clone or download this repository:
git clone <repository-url>
cd meshcore-bot
- Install dependencies:
pip install -r requirements.txt
- Configure the bot by editing
config.ini(a default config will be created if it doesn't exist)
Configuration
The bot uses a config.ini file for all settings. Here's an overview of the configuration sections:
Connection Settings
[Connection]
connection_type = serial # serial or ble
serial_port = /dev/ttyUSB0 # Serial port path
serial_baudrate = 115200 # Baud rate
ble_device_name = MeshCore # BLE device name
timeout = 30 # Connection timeout
Bot Settings
[Bot]
bot_name = MeshCoreBot # Bot identification name
node_id = # Leave empty for auto-assignment
enabled = true # Enable/disable bot
passive_mode = false # Only listen, don't respond
rate_limit_seconds = 10 # Rate limiting interval
Keywords and Responses
[Keywords]
# Format: keyword = response_template
# Available variables: {hops}, {path}, {sender}, {channel}, {content}, {timestamp}
test = "Message received! Hops: {hops}, Path: {path}, From: {sender}"
weather = "Weather info: {content}"
help = "Available commands: test, weather, help"
Channel Management
[Channels]
monitor_channels = general,test,emergency # Channels to monitor
respond_to_dms = true # Enable DM responses
User Management
[Banned_Users]
banned_users = user1,user2 # Comma-separated list
Scheduled Messages
[Scheduled_Messages]
# Format: time = channel:message
08:00 = general:Good morning! Weather update coming soon.
12:00 = general:Lunch time reminder
18:00 = general:Evening update
Logging
[Logging]
log_level = INFO # DEBUG, INFO, WARNING, ERROR, CRITICAL
log_file = meshcore_bot.log # Log file path (empty for console only)
colored_output = true # Enable colored console output
Usage
Running the Bot
- Basic Usage:
python meshcore_bot.py
- With custom config:
python meshcore_bot.py --config my_config.ini
CLI Management
The bot includes a comprehensive CLI for management:
- Interactive Mode:
python bot_cli.py --interactive
- Command Line Mode:
# Show bot status
python bot_cli.py status
# Add a keyword
python bot_cli.py keywords add "hello" "Hello there! How can I help?"
# Ban a user
python bot_cli.py users ban "spam_user"
# Send a message
python bot_cli.py send general "Hello everyone!"
# Add scheduled message
python bot_cli.py schedule add "09:00" general "Good morning!"
Interactive CLI Commands
When running in interactive mode, you can use these commands:
status- Show bot status and configurationkeywords- List all keyword-response pairsadd-keyword <keyword> <response>- Add new keywordremove-keyword <keyword>- Remove keywordban <user_id>- Ban a userunban <user_id>- Unban a userbanned- List banned userssend <channel> <message>- Send message to channelschedule- List scheduled messagesadd-schedule <time> <channel> <message>- Add scheduled messageremove-schedule <time>- Remove scheduled messagehelp- Show helpquitorexit- Exit CLI
Message Response Templates
When configuring keyword responses, you can use these template variables:
{hops}- Number of hops the message traveled{path}- Two-character path identifiers{sender}- Sender's node ID{channel}- Channel name{content}- Original message content{timestamp}- Message timestamp
Example:
[Keywords]
test = "Message received! Hops: {hops}, Path: {path}, From: {sender}"
weather = "Weather for {sender}: {content}"
echo = "You said: {content}"
Protocol Adaptation
The bot includes a flexible protocol adapter that can handle multiple message formats:
- JSON Format (preferred):
{
"type": "text",
"sender": "node1",
"channel": "general",
"content": "Hello world",
"hops": 2,
"path": "AB",
"timestamp": "2024-01-01T12:00:00Z"
}
- Pipe-Separated Format:
MSG|node1|general|Hello world|2|AB|msg_001|2024-01-01T12:00:00Z
- Space-Separated Format:
MSG node1 general Hello world 2 AB
Hardware Setup
Heltec V3 Setup
- Flash MeshCore firmware to your Heltec V3 device using the MeshCore Flasher
- Connect the device via USB
- Update the
config.iniwith the correct serial port:[Connection] connection_type = serial serial_port = /dev/ttyUSB0 # Linux # serial_port = COM3 # Windows # serial_port = /dev/tty.usbserial-* # macOS
BLE Setup
- Ensure your MeshCore device supports BLE
- Update the
config.ini:[Connection] connection_type = ble ble_device_name = MeshCore
Troubleshooting
Common Issues
-
Serial Port Not Found:
- Check device connection
- Verify port name in config
- Try listing available ports:
python -c "import serial.tools.list_ports; print([p.device for p in serial.tools.list_ports.comports()])"
-
BLE Connection Issues:
- Ensure device is discoverable
- Check device name in config
- Verify BLE permissions on your system
-
Message Parsing Errors:
- Check the protocol format in
meshcore_protocol.py - Verify message format from your MeshCore device
- Enable DEBUG logging for detailed information
- Check the protocol format in
-
Rate Limiting:
- Adjust
rate_limit_secondsin config - Check logs for rate limiting messages
- Adjust
Debug Mode
Enable debug logging for detailed information:
[Logging]
log_level = DEBUG
Extending the Bot
Adding External Data Sources
The bot is designed to be easily extended. To add weather or tide data:
- Create a new module (e.g.,
weather_integration.py) - Implement data fetching functions
- Add configuration options to
config.ini - Integrate with the main bot class
Example weather integration:
class WeatherIntegration:
def __init__(self, api_key: str):
self.api_key = api_key
async def get_weather(self, location: str) -> str:
# Implement weather API call
return f"Weather for {location}: Sunny, 22°C"
Custom Message Handlers
You can extend the message processing by adding custom handlers:
class CustomMessageHandler:
def __init__(self, bot):
self.bot = bot
async def handle_message(self, message: MeshMessage):
# Custom message processing logic
if "custom_command" in message.content:
await self.bot.send_message(message.channel, "Custom response!")
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- MeshCore Project for the mesh networking protocol
- The MeshCore community for documentation and examples
Support
- Check the MeshCore documentation
- Join the MeshCore Discord for community support
- Open an issue on this repository for bug reports or feature requests