Quick Start¶
This guide will help you create your first WhatsApp bot using Neonize in just a few minutes.
Your First Bot¶
Let's create a simple bot that responds to messages:
Step 1: Create a Python File¶
Create a new file called bot.py:
Step 2: Run Your Bot¶
Execute your bot:
| Bash | |
|---|---|
Step 3: Authenticate¶
When you run the bot for the first time, it will display a QR code in your terminal:
- Open WhatsApp on your phone
- Go to Settings → Linked Devices
- Tap Link a Device
- Scan the QR code displayed in your terminal
Connected!
Once authenticated, you'll see "✅ Bot connected successfully!" in your terminal.
Step 4: Test Your Bot¶
Send a message to your bot:
- Send
ping→ Bot replies:pong! 🏓 - Send
hello→ Bot replies:Hello! 👋 How can I help you?
Understanding the Code¶
Let's break down what each part does:
1. Import Required Modules¶
| Python | |
|---|---|
NewClient- Main client class for WhatsApp connectionMessageEv- Event triggered when receiving messagesConnectedEv- Event triggered when bot connects successfullyevent- Threading event to keep the bot running
2. Create the Client¶
| Python | |
|---|---|
This creates a new client instance with the session name "my_first_bot". The session data will be stored in my_first_bot.db.
3. Event Handlers¶
| Python | |
|---|---|
The @client.event(EventType) decorator registers event handlers. You pass the event class (e.g., ConnectedEv, MessageEv) to specify which event the handler receives.
4. Message Processing¶
| Python | |
|---|---|
Messages can be either simple text (conversation) or extended text messages. This line handles both cases.
5. Start the Bot¶
connect()- Establishes connection to WhatsAppevent.wait()- Keeps the bot running indefinitely
Async Version¶
Neonize also supports async/await syntax:
Understanding the Async Event Loop¶
Neonize uses asyncio.run() as the standard entry point. When you call
await client.connect(), the library internally calls
asyncio.get_running_loop() to capture the current event loop. All internal
event dispatching (events from Go callbacks via run_coroutine_threadsafe) is
automatically wired to this same loop.
Deprecated Patterns — Do NOT Use
The following patterns are deprecated since Python 3.10 and raise
DeprecationWarning or RuntimeError on Python 3.12+:
Correct Modern Pattern
asyncio.run() creates a fresh event loop, runs the coroutine to
completion, then cleans up. Inside main(), you can use
asyncio.get_running_loop() to access the loop when needed.
Database Configuration¶
By default, Neonize uses SQLite. You can specify a custom database:
SQLite (Default)¶
| Python | |
|---|---|
PostgreSQL (Production)¶
| Python | |
|---|---|
Common Patterns¶
Handling Different Message Types¶
Sending Different Message Types¶
Replying to Messages¶
| Python | |
|---|---|
Next Steps¶
Now that you have a working bot, explore more features:
- Authentication Methods - Learn about pairing codes
- Event System - Handle different events
- Sending Messages - Send various message types
- Media Handling - Work with images, videos, and more
- Examples - Browse more code examples
Best Practices
- Use meaningful session names for different bots
- Handle errors gracefully with try-except blocks
- Use environment variables for sensitive configuration
- Enable logging for debugging:
logging.basicConfig(level=logging.DEBUG)
Rate Limiting
WhatsApp has rate limits to prevent spam. Avoid sending too many messages in a short time to prevent temporary bans.