Search code examples
twiliotwilio-programmable-chat

Interactive message using python


I'm trying to implement a Twilio webhook in my Flask API to send interactive messages to WhatsApp users..

however it creates error in twilio_webhook response.message("Option 1").quick_reply("option1") AttributeError: 'Message' object has no attribute 'quick_reply'

I want to know whether we can send button, list from our api if yes then can anyone guide me how to do it.

I was trying to create interactive messages from api.

@app.route('/twilio/webhook', methods=['POST'])
def twilio_webhook():
    # Parse the incoming request data from Twilio
    data = request.form
    user_message = data.get('Body', '').strip().lower()
    from_number = data.get('From')
    
    # Check if the user sent 'hi' message
    if user_message == 'hi':
        # Send quick replies to the user
        response = MessagingResponse()
        response.message("Hello! Please select an option:")
        response.message("Option 1").quick_reply("option1")
        response.message("Option 2").quick_reply("option2")
        response.message("Option 3").quick_reply("option3")
        return str(response)

Solution

  • The MessagingResponse has no such functions that you described here. Hence, you get this error. What you can do instead, is using the Content Template API to create a template:

    curl -X POST 'https://content.twilio.com/v1/Content' \
    -H 'Content-Type: application/json' \
    -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN \
    -d '{
        "friendly_name": "flight_replies",
        "language": "en",
        "variables": {"1":"Owl Air Customer"},
        "types": {
            "twilio/quick-reply": {
                        "body": "Hi, {{1}} 👋 \nThanks for contacting Owl Air Support. How can I help?",
                        "actions": [
                            {
                                "title": "Check flight status",
                                "id": "flightid1"
                            },
                            {
                                "title": "Check gate number",
                                "id": "gateid1"
                            },
                            {
                                "title": "Speak with an agent",
                                "id": "agentid1"
                            }
                        ]
                    },
            "twilio/text": {
                "body": "Hi, {{1}}. \n Thanks for contacting Owl Air Support. How can I help?."
            }
        }
    }' 
    

    And then in the second step, use this template with the regular Twilio messaging API. Note that you cannot return TwiML to use the template sid but need to return an empty TwiML.

    client.messages.create(
        content_sid='HXXXXXXXXX',
        from_='MGXXXXXXXX',
        content_variables=json.dumps({
            '1': 'Name'
        }),
        to='whatsapp:+18551234567'
    )
    response = MessagingResponse()
    return str(response)