1

I am having trouble using Twilio's MessagingResponse class to send and receive messages using a webhook. I am using flask and ngrok to create a temporary URL to test my app, but I am getting a 502 error and 11200 warning with this new implementation.

I can confirm that the number I am attempting to message back is verified in my Twilio account. Here is the new implementation that uses the Twilio MessagingResponse to create the response message instead of sending it directly using the Twilio REST API:

import os
from flask import Flask, request, session, make_response
from twilio.twiml.messaging_response import MessagingResponse
from twilio.rest import Client
from chatbot import ask, append_interaction_to_chat_log
from secret_key import secret_key

app = Flask(__name__)
app.config['SECRET_KEY'] = secret_key

account_sid = os.environ.get('ACCOUNT_SID')
auth_token = os.environ.get('AUTH_TOKEN')
client = Client(account_sid, auth_token)

@app.route('/bot', methods=['POST'])
def bot():
    incoming_msg = request.values['Body']
    print(incoming_msg)
    chat_log = session.get('chat_log')

    answer = ask(incoming_msg, chat_log)
    session['chat_log'] = append_interaction_to_chat_log(incoming_msg, answer, chat_log)

    r = MessagingResponse()
    r.message = answer
    return make_response(r)

I have been able to successfully send and receive messages using a message object and explicitly stating the phone number I am sending to using this implementation:

import os
from flask import Flask, request, session
from twilio.twiml.messaging_response import MessagingResponse
from twilio.rest import Client
from chatbot import ask, append_interaction_to_chat_log
from secret_key import secret_key

app = Flask(__name__)
app.config['SECRET_KEY'] = secret_key

account_sid = os.environ.get('ACCOUNT_SID')
auth_token = os.environ.get('AUTH_TOKEN')
client = Client(account_sid, auth_token)

@app.route('/', methods=['POST'])
def bot():
    incoming_msg = request.values['Body']
    print(incoming_msg)
    chat_log = session.get('chat_log')

    answer = ask(incoming_msg, chat_log)
    session['chat_log'] = append_interaction_to_chat_log(incoming_msg, answer, chat_log)

    # use the incoming message to generate the response here
       
    message = client.messages.create(
        body=answer,
        from_='+12232107883',  #Twilio number you purchased or verified
        to='+19143182181' # The phone number you want to send the message to
     )

    print(message.sid)
    return 'message sent'

Attached is a photo of my implementation of the ngrok URL to configure the webhook.

ngrok to initiate webhook

Essentially, I am trying to implement the new structures in hopes of creating a more secure and scalable bot. Any ideas here? I realize it could be something in my Twilio settings, but I haven't found the solution. Thank you all.

enter image description here

I thought my problem was from not having an ngrok account:

enter image description here

But that did not seem to resolve the issue:

enter image description here

  • Seems there is no attachment in this question. Also, can you check if you find a hint in the [error and message logs](https://www.twilio.com/docs/sms/debugging-tools) and add them as well? – IObert Jan 30 '23 at 09:35
  • @IObert I have attached the ngrok uri (one of many I've used) and screen shots of my error logs. I created an ngrok account and authenticated my agent with my token thinking this was the issue (per error logs) but it has not fixed the problem. – Kyle Massimilian Feb 09 '23 at 03:11
  • It seems like the Twilio webhook gets an ngrok error message as a response, which it cannot handle. So I'd assume the problem is in your ngrok configuration somewhere. – IObert Feb 11 '23 at 12:00

0 Answers0