Whatsapp BC
Whatsapp BC

Whatsapp BC

Februari 05, 2023
python logo

Automating whatsapp sending can save a lot of time and effort, especially if you have a list of customers or clients that you need to contact regularly. Using Python, we can create a script that will send whatsapp to a list of customers stored in an Excel file, so each mobile number will be sent individually. This article will guide you step by step on how to create such a script using the pandas library to read data from Excel and smtplib to send emails.

Setting Up the Environment

  1. Install Python: If not already installed, download and install Python from the official website.
  2. Install Required Libraries: Run the following command in the terminal or command prompt to install pandas and openpyxl:
    pip install pandas pywhatkit

Excel File Format

Ensure your Excel file has the correct format. A suggested format is as follows:

Title Name Mobile Number
Mr John Doe +6281234567890
Ms Jane Smith +6281234567890
Mr Bob Johnson +6281234567890

Python Script to Send Emails

Create a new Python file, for example, send_emails.py, and add the following code:

import pandas as pd
import pywhatkit as kit
import time

# Membaca data dari file Excel
excel_file = 'customer_numbers.xlsx'  # Ganti dengan nama file Excel Anda
data = pd.read_excel(excel_file)

# Pastikan kolom "Mobile number" adalah string
data['Mobile number'] = data['Mobile number'].astype(str)

# Template pesan
message_template = '''
Yth. {title} {name},

Kami dari [Nama Perusahaan] ingin memperkenalkan produk terbaru kami kepada Anda. [Deskripsi Produk].

Salam hormat,
[Nama Anda]
'''

# Mengirim pesan WhatsApp ke setiap pelanggan
for index, row in data.iterrows():
    mobile_number = row['Mobile number']  # Ganti 'Mobile number' dengan nama kolom yang sesuai di file Excel Anda
    title = row['Title']                  # Ganti 'Title' dengan nama kolom yang sesuai di file Excel Anda
    name = row['Name']                    # Ganti 'Name' dengan nama kolom yang sesuai di file Excel Anda
    message = message_template.format(title=title, name=name)
    
    # Validasi nomor telepon
    if not mobile_number.startswith('+'):
        mobile_number = '+' + mobile_number
    
    # Mengirim pesan WhatsApp
    try:
        kit.sendwhatmsg_instantly(mobile_number, message, 15, True, 2)
        print(f"Pesan terkirim ke {mobile_number}")
        time.sleep(10)  # Jeda waktu antar pesan (10 detik)
    except Exception as e:
        print(f"Failed to send message to {mobile_number}: {e}")

print("Semua pesan telah terkirim.")
python send_whatsapp.py