I’m trying to send an image to multiple WhatsApp contacts using the Meta (Facebook) WhatsApp Cloud API. I have an Excel file with phone numbers and names, and I want to personalize each message by including the contact’s name in the message body. Here’s the Python code I’m using (simplified):
import pandas as pd
import requests
import time
ACCESS_TOKEN = ""
EXCEL_FILE = "contacts.xlsx"
PHONE_COLUMN = "phone"
NAME_COLUMN = "name"
IMAGE_LINK = "https://raw.githubusercontent.com/ghazdevlper-ux/Images/main/Independece.JPG"
DELAY_SECONDS = 5
df = pd.read_excel(EXCEL_FILE)
contacts = df[[PHONE_COLUMN, NAME_COLUMN]].dropna()
url = "https://graph.facebook.com/v22.0//messages"
headers = {
"Authorization": f"Bearer {ACCESS_TOKEN}",
"Content-Type": "application/json"
}
for _, row in contacts.iterrows():
phone = str(row[PHONE_COLUMN])
name = str(row[NAME_COLUMN])
payload = {
"messaging_product": "whatsapp",
"to": phone,
"type": "template",
"template": {
"name": "image_template",
"language": {"code": "en"},
"components": [
{
"type": "header",
"parameters": [
{
"type": "image",
"image": {"link": IMAGE_LINK}
}
]
},
{
"type": "body",
"parameters": [
{"type": "text", "text": name}
]
}
]
}
}
try:
response = requests.post(url, headers=headers, json=payload)
print(f"Sent to {phone}: {response.status_code} - {response.text}")
except Exception as e:
print(f"Error sending to {phone}: {e}")
time.sleep(DELAY_SECONDS)
I expected each contact to receive the image (and name in the message body), but the API either fails or does not send the image or it return message sent but receiver phone does not shows it.
What I’ve tried so far:
- Double-checked my
ACCESS_TOKENand phone number ID - Tried using a direct image URL from GitHub (publicly accessible)
- Verified that my message template has a header of type image