Python

As an outcome of this guide, you will be able to leverage Infobip WhatsApp API and send contact details of one or multiple people through a WhatsApp message, which will be delivered to your handset. Mind that you as a business are only able to share contact details if the customer you are sharing the details with has contacted you within the 24-hour period. You are not able to use a template message with this endpoint.


Prerequisites

Infobip account

• Working Python 3 environment

Difficulty level

This guide assumes basic knowledge of Python and basic familiarity with APIs.

Summary of the steps

• Install the Infobip API Python SDK.
• Import WhatsApp Channel to create an instance and add credentials.
• Share contact details in a WhatsApp message.
• Optionally, print out the response to track the progress of your message.

Install the Infobip API Python SDK

Use your terminal or command prompt to install the Infobip Python SDK.

pip install infobip-api-python-sdk

Create a WhatsApp Channel instance

In this section, you’ll learn how to create a WhatsAppChannel instance and add your credentials. You’ll also see how to access its methods, specifically the send_contact_message method you will use to share your contact details in a WhatsApp message.

Step 1. Import the WhatsAppChannel.

from infobip_channels.whatsapp.channel import WhatsAppChannel

Step 2. Create WhatsAppChannel and add your unique base_url and api_key that you can access either from your Infobip account or from the Infobip API landing page once logged in.

channel = WhatsAppChannel.from_auth_params({
    "base_url": "<your_base_url>",
    "api_key": "<your_api_key>"
})

Step 3. Use the send_contact_message method to add the WhatsApp contact payload. The code example below shows you how to add contact details of two people and send it in one request and as a single WhatsApp message.

Key points:

• The to field must include a number in the international format, e.g. 447415774332.
• If using a free trial account, the phone number you use as a recipient must be the same phone number you’ve registered on signup.

response = channel.send_contact_message({
  "from": "447860099299",
  "to": "447415774432",
  "messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
  "content": {
    "contacts": [
      {
        "addresses": [
          {
            "street": "Some Drive",
            "city": "Gateshead",
            "zip": "NE18 9QR",
            "country": "United Kingdom",
            "countryCode": "GB",
            "type": "WORK"
          },
          {
            "street": "Big Road",
            "city": "Gateshead",
            "zip": "NE8 2HR",
            "country": "United Kingdom",
            "countryCode": "GB",
            "type": "HOME"
          }
        ],
        "birthday": "2010-01-01",
        "emails": [
          {
            "email": "[email protected]",
            "type": "WORK"
          },
          {
            "email": "[email protected]",
            "type": "HOME"
          }
        ],
        "name": {
          "firstName": "John",
          "lastName": "Smith",
          "middleName": "B",
          "namePrefix": "Mr.",
          "formattedName": "Mr. John Smith"
        },
        "org": {
          "company": "Company Name",
          "department": "Department",
          "title": "Director"
        },
        "phones": [
          {
            "phone": "+441134979019",
            "type": "HOME",
            "waId": "441134943019"
          },
          {
            "phone": "+445534960000",
            "type": "WORK",
            "waId": "445534960000"
          }
        ],
        "urls": [
          {
            "url": "http://example.com/John.Smith",
            "type": "WORK"
          },
          {
            "url": "http://example.com/home/John.Smith",
            "type": "HOME"
          }
        ]
      },
      {
        "addresses": [
          {
            "street": "Bracken Street",
            "city": "Newcastle",
            "zip": "NE1 2QP",
            "country": "United Kingdom",
            "countryCode": "GB",
            "type": "WORK"
          },
          {
            "street": "Beep Road",
            "city": "Newcastle",
            "zip": "NE2 2PP",
            "country": "United Kingdom",
            "countryCode": "GB",
            "type": "HOME"
          }
        ],
        "birthday": "2019-10-31",
        "emails": [
          {
            "email": "[email protected]",
            "type": "WORK"
          },
          {
            "email": "[email protected]",
            "type": "HOME"
          }
        ],
        "name": {
          "firstName": "Dani",
          "lastName": "Superstar",
          "middleName": "B",
          "namePrefix": "Master",
          "formattedName": "Master Dani Superstar"
        },
        "org": {
          "company": "Company Name",
          "department": "Department Name",
          "title": "CEO"
        },
        "phones": [
          {
            "phone": "+441134960019",
            "type": "HOME",
            "waId": "441134960019"
          },
          {
            "phone": "+441134960000",
            "type": "WORK",
            "waId": "441134960000"
          }
        ],
        "urls": [
          {
            "url": "http://example.com/danielito.superstar",
            "type": "WORK"
          },
          {
            "url": "http://example.com/home/danielito",
            "type": "HOME"
          }
        ]
      }
    ]
  }
})

Add multiple recipients and multiple messages

You cannot send multiple messages to multiple recipients in one request with this endpoint. Typically, you would use a templated WhatsApp message to do it.

Monitor the progress of your WhatsApp message

Print the response variable to see whether the message has successfully left the Infobip platform. If you wish to know whether it’s been delivered to the recipient, you’ll need to set up a Delivery Report.

print(response)

Once executed, you should receive a WhatsApp message on your handset and see a 200 OK response.

{
  "to": "441134960001",
  "messageCount": 1,
  "messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
  "status": {
    "groupId": 1,
    "groupName": "PENDING",
    "id": 7,
    "name": "PENDING_ENROUTE",
    "description": "Message sent to next instance"
  }
}

For troubleshooting and analytics, Use the auto-generated messageId to view the message and its details.