Wednesday, 7 August, 2019 UTC


Summary

This tutorial will help to consume business whatsapp api using python flask.You can handle all operation using rest api as like app.The WhatsApp is providing HTTP rest API to access his features.
I am just discovering WhatsApp api using python 3. WhatsApp is a fast, secure, and reliable way for businesses to reach their customers all over the world.
These API help to medium and large businesses to connect user with a simple, secure, and reliable way.
WhatsApp Business API Using Python
We will use WhatsApp Business API to get all group list.You can interact with their customers using api WhatsApp Business API.As like other REST wrapper, The WhatsApp Business API also use JSON format for request and response data.
I am assuming, You have WhatsApp Business API account and hosted WhatsApp docker client into machine.We just get hostname url of whatsapp client.
You can also Read :How To Consume CloudFlare API Using Python

Let’s install Python dependency

We will use requests package to handle HTTP request into this application.You can install request and flask package using following command –
pip install requests
pip install flask

Create API to Consume Whatsapp API

Let’s create whatsapp.py file and added below code into this file.
from flask import Flask
from flask_restful import Resource, Api
from flask_cors import CORS
import requests

app = Flask(__name__)
CORS(app) ## To allow direct AJAX calls

@app.route('/getGroups', methods=['GET'])
def home():
   try:        
    headers = {'Authorization': 'Bearer your-auth-token', 'Content-Type': 'application/json'}
    res = requests.post('https://your-webapp-hostname:your-webapp-port/v1/groups', headers=headers)

    return r.json()

   except Exception as e:
    print('unable to get groups.')
    print(e.strerror)

if __name__ == '__main__':
   app.run(debug = True)
The above HTTP request includes the following components:-
  • URL — The URL of the server along with the API endpoint for the request.
  • Method — The action being requested of the endpoint.You can use methods like POST, GET, DELETE, PATCH and PUT.
  • Headers — You can add Meta information about the request like request type, token etc.
  • Body — This contains the payload data.
The above call response with response object, The response object will have all available groups with ids.The response as per docs as like below –
{
    "groups": [
       {"id": "group-id1"},
       {"id": "group-id2"}
    ]
}
The Whatsapp Response Object will have following properties –
  • HTTP Status Code — Status codes are issued by a server in response to a request made to the server. See HTTP Status Codes for more information.
  • Meta — Contains generic information such as WhatsApp Business API Client version.
  • Payload — Only returned with a successful response. The payload object name and contents vary based on the request. In the above examples, the payload is contacts because we sent a request to the contacts endpoint.
  • Errors — Only returned with a failed request. Contains an array of error objects that are present when there is an error. See the Error Codes for more information.
The post How To Access WhatsApp API Using Python appeared first on Rest Api Example.