-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhello_world_plugin.py
More file actions
118 lines (92 loc) · 3.68 KB
/
Copy pathhello_world_plugin.py
File metadata and controls
118 lines (92 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Hello World Plugin - Example of using EnhancedPlugin
This is a minimal example plugin that demonstrates:
- Command registration
- Message handling
- Configuration access
- Mesh messaging
"""
from src.core.enhanced_plugin import EnhancedPlugin
from src.core.plugin_manager import PluginMetadata, PluginPriority
class HelloWorldPlugin(EnhancedPlugin):
"""
A simple hello world plugin that responds to commands.
"""
async def initialize(self) -> bool:
"""Initialize the plugin"""
self.logger.info("Initializing Hello World Plugin")
# Register commands
self.register_command(
"hello",
self.handle_hello_command,
"Say hello to the mesh network",
priority=100
)
self.register_command(
"greet",
self.handle_greet_command,
"Greet a specific user",
priority=100
)
# Register a message handler (optional)
self.register_message_handler(self.handle_message, priority=200)
# Register a scheduled task (optional)
greeting_interval = self.get_config("greeting_interval", 3600)
self.register_scheduled_task(
"periodic_greeting",
greeting_interval,
self.send_periodic_greeting
)
return True
async def handle_hello_command(self, args, context):
"""Handle the 'hello' command"""
sender = context.get('sender_id', 'unknown')
# Get custom greeting from config
greeting = self.get_config("greeting_message", "Hello")
response = f"{greeting}, {sender}! Welcome to the mesh network."
# Store interaction count
count = await self.retrieve_data("interaction_count", 0)
count += 1
await self.store_data("interaction_count", count)
response += f" (Interaction #{count})"
return response
async def handle_greet_command(self, args, context):
"""Handle the 'greet' command with a specific user"""
if not args:
return "Usage: greet <name>"
name = " ".join(args)
greeting = self.get_config("greeting_message", "Hello")
return f"{greeting}, {name}! Nice to meet you on the mesh."
async def handle_message(self, message, context):
"""Handle incoming messages (optional processing)"""
# Example: Log all messages containing "hello"
if "hello" in message.content.lower():
self.logger.info(f"Detected greeting from {message.sender_id}")
# Return None to allow other handlers to process
return None
async def send_periodic_greeting(self):
"""Send a periodic greeting to the mesh"""
enabled = self.get_config("periodic_greeting_enabled", False)
if enabled:
greeting = self.get_config("greeting_message", "Hello")
await self.send_message(f"{greeting} from Hello World Plugin!")
self.logger.info("Sent periodic greeting")
def get_metadata(self) -> PluginMetadata:
"""Get plugin metadata"""
return PluginMetadata(
name="hello_world",
version="1.0.0",
description="A simple hello world plugin demonstrating EnhancedPlugin features",
author="ZephyrGate Team",
priority=PluginPriority.NORMAL,
enabled=True
)
# Example configuration for this plugin (in config.yaml):
"""
plugins:
hello_world:
enabled: true
greeting_message: "Greetings"
greeting_interval: 3600 # 1 hour
periodic_greeting_enabled: false
"""