DEV Community

Cover image for Send and Receive message in WhatsApp using twilio & python
Raunak Tamang
Raunak Tamang

Posted on

Send and Receive message in WhatsApp using twilio & python

Previously, i made WhatsApp automation using python selenium, this time i thought to do more with it and used twilio to send and receive messages from WhatsApp using twilio and flask


$ pip install flask
$ pip install twilio
install ngrok
make twilio trial account

first for most integrate your number with twilio sandbox

packagae to import :

from flask import Flask, request ,render_template
from twilio.twiml.messaging_response import MessagingResponse 

from import Client 

For sending message

@app.route("/" , methods = ['GET' , 'POST'])

def index():

    if request.method == "POST":
        fname = request.form['fname']
        lname = request.form['lname']
        pname = request.form['pname']

        sid = 'account_sid'
        auth = 'auth_token'

        client = Client(sid,auth)

        message = client.messages.create(
             body='hello ' +  str(fname) + " " +  str(lname) +  ' welcome to Tanzible',   

        return 'registration successfully'

    return render_template('index.html')

for receiving message

first we have to configure ngrok to put localhost to on public address

so download ngrok and install it via command line where is you ngrok file

./ngrok http 5000 

5000 is by default port given by flask

def msg(request):
    if request.method == 'POST':
        resp = MessagingResponse()
        resp.message("Thank you for contacting us! Our team will shortly in touch with you ")
        return str(resp)

That's all folk !

check code in my GitHub

Discussion (0)