Building the chatbot

First, we need to obtain the message entered by the user. The payload of the 'POST' request with a key of 'Body' contains the message. It can be accessed using Flask's request object as follows:

from flask import request
incoming_msg = request.values.get('Body', '').lower()

Now we need to create a response that contains text and media components. This can be done easily using the Twilio helper library for Python that can make it easy to create a response without using XML directly.

from twilio.twiml.messaging_response import MessagingResponse

resp = MessagingResponse()
msg = resp.message()
msg.body('this is the response text')
msg.media('https://example.com/path/image.jpg')

Now that we are set to respond, let us define the chatbot logic!

 

 

 
Discussion

5

0