Simplify WhatsApp Alerts in Python with Wbiztool’s API: A Step-by-Step Guide

Introduction

In today’s fast-paced world, real-time alerts and notifications have become essential for businesses to keep their customers informed and engaged. With its widespread adoption and user-friendly interface, WhatsApp serves as an ideal platform for sending these alerts. In this article, we’ll explore how to integrate WhatsApp alerts into your Python applications using Wbiztool’s Send Message API.

Prerequisites

Before diving into the code, ensure you have the following:

  1. A Wbiztool account with an active subscription
  2. Your Wbiztool API credentials (client_id, api_key, and whatsapp_client)
  3. Python installed on your system
  4. The requests library installed (pip install requests)

Creating the WhatsApp Alert Function

Let’s create a Python function that sends WhatsApp alerts using Wbiztool’s API. Here’s the code:

import requests

def send_whatsapp_alert(phone, country_code, message):
    url = 'https://wbiztool.com/api/v1/send_msg/'

    data = {
        'client_id': 'your_client_id',
        'api_key': 'your_api_key',
        'whatsapp_client': 'your_whatsapp_client',
        'msg_type': 0,
        'msg': message,
        'phone': phone,
        'country_code': country_code
    }

    response = requests.post(url, data=data)

    if response.status_code == 200:
        result = response.json()
        if result['status'] == 1:
            print(f"WhatsApp alert sent successfully. Message ID: {result['msg_id']}")
        else:
            print(f"Failed to send WhatsApp alert. Error: {result['message']}")
    else:
        print(f"Request failed with status code {response.status_code}")

Let’s break down the function:

  1. We import the requests library to make HTTP requests to the Wbiztool API.
  2. We define the send_whatsapp_alert Function that takes three parameters:
  • phone: The recipient’s WhatsApp number.
  • country_code: The recipient’s country code.
  • message: The alert message to be sent.
  1. We specify the Wbiztool API endpoint URL for sending messages inside the function.
  2. We create a dictionary data that holds the necessary parameters for the API request, including the client_id, api_key, whatsapp_client, msg_type, msg, phone, and country_code.
  3. We send a POST request to the API endpoint using requests.post(), passing the URL and the data dictionary.
  4. We check the response status code. If it’s 200 (OK), we parse the JSON response using. response.json().
  5. If the status If the response is 1, it means the alert was sent successfully, and we print a success message along with the msg_id.
  6. If the status is not 1, we print an error message indicating that the alert failed to send, along with the error message from the API response.
  7. If the response status code is not 200, we print an error message indicating that the request failed, along with the status code.

Using the WhatsApp Alert Function

Now that we have the send_whatsapp_alert function, let’s see how to use it in your Python code:

# Example usage
send_whatsapp_alert('1234567890', '1', 'This is an important alert!')

In this example, we call the send_whatsapp_alert Function with the following arguments:

  • phone: ‘1234567890’ (replace with the actual recipient’s WhatsApp number)
  • country_code: ‘1’ (replace with the actual country code)
  • message: ‘This is an important alert!’ (replace with your desired alert message)

The function will send the WhatsApp alert and print the appropriate success or error message based on the API response.

Conclusion

Integrating WhatsApp alerts into your Python applications is straightforward with Wbiztool’s Send Message API. By creating a simple function like send_whatsapp_alert, you can easily send real-time notifications to your users’ WhatsApp numbers. This functionality is handy for sending important updates, reminders, or any other time-sensitive information.

Remember to replace 'your_client_id', 'your_api_key', and 'your_whatsapp_client' with your actual Wbiztool API credentials to make the code work with your account.

For more advanced use cases and detailed API documentation, refer to the official Wbiztool API documentation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top