Python Text Messaging Bot using Flask, Twilio, Ngrok sending double text messages

Summary

The issue at hand involves a Python text messaging bot using Flask, Twilio, and Ngrok. When sending text messages only, the bot functions correctly. However, when sending text and image URLs in a text message, the text message is sent to the correct phone number, while the images are sent to the same phone number but without the country code. Key issue: The Twilio client is treating the text and image messages as separate entities, resulting in inconsistent phone number formatting.

Root Cause

The root cause of this problem lies in how Twilio handles media URLs in text messages. When a media URL is included in a message, Twilio creates a separate message for the media, which may not inherit the same formatting as the original message. Causes:

  • Inconsistent phone number formatting
  • Twilio’s handling of media URLs in text messages

Why This Happens in Real Systems

This issue occurs in real systems due to the complexities of handling multimedia messages (MMS) versus standard text messages (SMS). Reasons:

  • Different protocols and handling for MMS and SMS
  • Inconsistent implementation of phone number formatting across different message types
  • Unintended consequences of using third-party APIs like Twilio

Real-World Impact

The real-world impact of this issue is that users may receive multiple, seemingly separate messages, which can cause confusion and frustration. Impacts:

  • User experience degradation
  • Potential for missed or lost messages
  • Inefficient use of messaging resources

Example or Code

# create twilio client using the account_sid, auth_token
client = Client(account_sid, auth_token)
# send text message only
message = client.messages.create(
    to=from_number,  # to phone number
    from_=to_number,  # from phone number
    body=return_body  # return_body is the text to send
)
# sending a text message and image URLs
message = client.messages.create(
    to=from_number,  # to phone number
    from_=to_number,  # from phone number
    body=return_body,  # return_body is the text to send
    media_url=['https://www.example.com/images/logo.jpg', 'https://www.example.com/bot/Help-Desk.jpg']  # send the image Url's
)

How Senior Engineers Fix It

To fix this issue, senior engineers would normalize the phone number formatting before sending the message, ensuring that the country code is consistently included or excluded. Steps:

  • Validate and standardize the phone number formatting
  • Use a consistent approach to handling media URLs in text messages
  • Implement error checking and logging to detect and debug similar issues

Why Juniors Miss It

Junior engineers may miss this issue due to lack of experience with Twilio’s API and insufficient testing of edge cases. Reasons:

  • Limited understanding of the complexities of handling multimedia messages
  • Inadequate testing of different phone number formats and message types
  • Failure to anticipate and account for the potential inconsistencies in Twilio’s handling of media URLs