DEV Community

Cover image for Building a WhatsApp Bot with Python and Neonize: A Simple Guide
krypton byte (puja)
krypton byte (puja)

Posted on

Building a WhatsApp Bot with Python and Neonize: A Simple Guide

Creating a WhatsApp Bot Using Python with Neonize Library: A Step-by-Step Guide

WhatsApp automation has never been easier with the Neonize library in Python. Neonize simplifies the process of automating tasks on WhatsApp, allowing users to send messages, process received messages, and execute specific actions effortlessly. In this article, we'll walk you through the process of creating a WhatsApp bot using the Neonize library, from installation to writing simple and understandable example code.

Installation:

The first step is to install the Neonize library using pip:

pip install neonize
Enter fullscreen mode Exit fullscreen mode

Initialization and Configuration:

Once installed, initializing and configuring Neonize for your WhatsApp account is a breeze. The library is designed for simplicity and ease of use.

from neonize.client import NewClient

client = NewClient("your_database_file.sqlite3")
Enter fullscreen mode Exit fullscreen mode

Example Code:

Let's explore some simple examples to demonstrate the capabilities of Neonize. Below is a basic script showcasing various functionalities:

from neonize.client import NewClient
from neonize.events import ConnectedEv, MessageEv, PairStatusEv, event
from neonize.types import MessageServerID
from datetime import timedelta

client = NewClient("your_database_file.sqlite3")

@client.event(ConnectedEv)
def on_connected(_: NewClient, __: ConnectedEv):
    print("⚡ Connected")

@client.event(MessageEv)
def on_message(client: NewClient, message: MessageEv):
    handler(client, message)

# Define your custom message handler function
def handler(client: NewClient, message: MessageEv):
    text = message.Message.conversation or message.Message.extendedTextMessage.text
    chat = message.Info.MessageSource.Chat

    # Example scenarios
    if text == "ping":
        client.reply_message(chat, "pong", message)
    elif text == "_sticker":
        client.send_sticker(chat, "sticker_url_here")

# Add more scenarios as needed...

# Connect to WhatsApp
client.connect()
Enter fullscreen mode Exit fullscreen mode

Contributing to Neonize:

If you're interested in contributing to the Neonize project, follow these steps:

  1. Fork the repository.
  2. Create a new branch: git checkout -b your-branch-name.
  3. Make desired changes.
  4. Commit changes: git commit -m 'Your commit message'.
  5. Push to the branch: git push origin your-branch-name.
  6. Send a pull request.

Local Development:

To run the project locally, follow these steps:

  1. Clone the repository: git clone git@github.com:krypton-byte/neonize.git.
  2. Install dependencies: poetry install --with dev (customize to the project).
  3. Run the project: python examples/basic.py (customize to the project).

License:

Neonize is licensed under Apache-2.0. Refer to the LICENSE file for more details.

Start building your WhatsApp bot today with the power of Neonize and Python! Feel free to customize the examples provided based on your specific automation needs. Happy coding!

Top comments (0)