Examples
Practical examples demonstrating Neonize features and common use cases.
Available Examples
Getting Started
- Basic Bot - Simple bot responding to messages
- Async Bot - Asynchronous version of the basic bot
Advanced Examples
Quick Examples
Echo Bot
A simple bot that echoes back all messages:
| Python |
|---|
| from neonize.client import NewClient
from neonize.events import MessageEv, event
client = NewClient("echo_bot")
@client.event(MessageEv)
def on_message(client: NewClient, event: MessageEv):
text = event.Message.conversation
if text:
client.reply_message(f"You said: {text}", event)
client.connect()
event.wait()
|
Command Bot
Bot with command handling:
| Python |
|---|
| from neonize.client import NewClient
from neonize.events import MessageEv, ConnectedEv, event
client = NewClient("command_bot")
@client.event(ConnectedEv)
def on_connected(client: NewClient, event: ConnectedEv):
print(f"✅ Bot connected as {event.device.User}")
@client.event(MessageEv)
def on_message(client: NewClient, event: MessageEv):
text = event.Message.conversation or ""
if text.startswith("/"):
command = text.split()[0][1:] # Remove /
if command == "help":
help_text = """
Available commands:
/help - Show this help
/ping - Pong!
/time - Current time
/info - Bot information
"""
client.reply_message(help_text, event)
elif command == "ping":
client.reply_message("Pong! 🏓", event)
elif command == "time":
from datetime import datetime
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
client.reply_message(f"🕐 {now}", event)
elif command == "info":
info = f"Bot: Neonize Command Bot\nVersion: 1.0"
client.reply_message(info, event)
client.connect()
event.wait()
|
Auto-Reply Bot
Bot with auto-reply functionality:
| Python |
|---|
| from neonize.client import NewClient
from neonize.events import MessageEv, event
client = NewClient("auto_reply_bot")
# Auto-reply rules
AUTO_REPLIES = {
"hello": "Hello! How can I help you?",
"hi": "Hi there! 👋",
"thanks": "You're welcome!",
"bye": "Goodbye! Have a great day! 👋",
}
@client.event(MessageEv)
def on_message(client: NewClient, event: MessageEv):
text = (event.Message.conversation or "").lower().strip()
# Check if message matches any auto-reply rule
if text in AUTO_REPLIES:
client.reply_message(AUTO_REPLIES[text], event)
client.connect()
event.wait()
|
More Examples
Async Bot
A basic async bot with event handling:
| Python |
|---|
| import asyncio
from neonize.aioze.client import NewAClient
from neonize.aioze.events import ConnectedEv, MessageEv
client = NewAClient("async_bot.sqlite3")
@client.event(ConnectedEv)
async def on_connected(client: NewAClient, event: ConnectedEv):
print("⚡ Connected")
@client.event(MessageEv)
async def on_message(client: NewAClient, event: MessageEv):
text = event.Message.conversation or event.Message.extendedTextMessage.text
if text == "ping":
await client.reply_message("pong!", event)
async def main():
await client.connect()
await client.idle()
asyncio.run(main())
|
Multi-Session Async
Handle multiple WhatsApp sessions with ClientFactory:
| Python |
|---|
| import asyncio
from neonize.aioze.client import ClientFactory, NewAClient
from neonize.aioze.events import ConnectedEv, MessageEv
factory = ClientFactory("sessions.db")
for device in factory.get_all_devices():
factory.new_client(device.JID)
@factory.event(ConnectedEv)
async def on_connected(client: NewAClient, event: ConnectedEv):
print("⚡ Client connected")
@factory.event(MessageEv)
async def on_message(client: NewAClient, event: MessageEv):
if event.Message.conversation == "ping":
await client.reply_message("pong!", event)
async def main():
await factory.run()
await factory.idle_all()
asyncio.run(main())
|
Event Loop
Always use asyncio.run() as your entry point for async examples.\n Neonize captures the running loop internally via asyncio.get_running_loop().
For complete, runnable examples, check the examples in the repository:
Need More Examples?