Receive WhatsApp Media Messages Using Python

    Overview

    This guide shows how to receive WhatsApp text messages on a phone number registered against your WhatsApp business account. Typical use cases for receiving messages include customer support,  opt-in and opt-out messages and other customer interactions.

    You can start receiving WhatsApp text messages using our APIs. Follow the instructions below.

    Prerequisites

    To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. If this is your first time using Plivo APIs, follow our instructions to set up a Python development environment.

    Create a Flask application to receive messages

    Create a file called receive_whatsapp.py and paste into it this code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    from flask import Flask, request
    
    app = Flask(__name__)
    
    @app.route('/receive_whatsapp/', methods=['GET', 'POST'])
    def inbound_sms():
    
        from_number = request.values.get('From')
        to_number = request.values.get('To')
        media = request.values.get('Media1')
        print('Message received - From: %s, To: %s, Text: %s' %(from_number, to_number, media))
    
        return 'Message Recevived'
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', debug=True)
    

    Was this code helpful

    Configure Webhook URL in your WhatsApp Business Account

    As shown below, you can very easily add/update webhook url against a WhatsApp Business Account. Once this step is concluded, you are good to receive incoming messages.

    Test

    Send a text message to the Plivo number you specified using any phone.

    Topics Involved