0

I'm building a Flask API, and one of its use cases consists on sending a WhatsApp message to a requested phone number. So far, I've been testing this feature through Twilio's sandbox & phone number in a trial account.

This is my use case code:

def send_greetings(order_id):
    try:
        phone = format_phone_number(
            retrieve_target_phone(order_id)
        )  # Retrieves target phone number and formats it (removes whitespace, etc)

        twilio_client.messages.create(
            from_=f"whatsapp:{current_app.config['TWILIO_WHATSAPP_SENDER']}",
            to=f"whatsapp:{phone}",
            body=build_message(order_id),  # Returns an f-string
        )
    except:
        raise

The code above fails to submit the message, but doesn't raise an exception. However, if I change the body argument from the call to build_message to a regular string, the message is sent. If I change the same parameter to a variable containing an f-string, the message won't be submitted.

It's noteworthy that the message I try to submit doesn't really match any defined template. This is the code for the build_message function:

from flask import current_app

def build_message(order_id: str) -> str:
    return f"¡Hola. Has recibido un regalo y junto con el, un saludo especial. Ve a {current_app.config['FRONTEND_DOMAIN']}/destinatario?order_id={order_id} para revisarlo!"

So why is it that when the parameter is a regular string the message is sent, even although it doesn't match any of the 3 predefined templates, but when it's an f-string it's not submitted?

LeperAffinity666
  • 364
  • 4
  • 14

1 Answers1

3

There's nothing magical about f-strings. The bad behavior must be caused by something else.

There is absolutely no difference in the return values of these two functions:

def hello():
    return "Hello John"

def hello_f():
    name = "John"
    return f"Hello {name}"

Anyone calling these functions would see exactly the same return value: a plain old string. The caller would have absolutely no way of knowing that the string was generated using an f-string template.

So there must be some actual difference in the content of the regular string you're using, vs. the output of the build_message() function.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • That's what I thought. Checking my string using `pdb` shows that its content is "¡Hola. Has recibido un regalo y junto con el, un saludo especial. Ve a http://localhost:3000/destinatario?order_id=418 para revisarlo!'", of type `str`. So it's a regular, normal string. Could maybe the URL cause the problem? – LeperAffinity666 Dec 02 '22 at 15:26
  • Browsing through the logs & docs, I found this https://www.twilio.com/docs/api/errors/63030 article. It turns out that a message may fail if there's "No URL to support preview". Evidently, a localhost URL might not be available. Changing the url solved the problem. But still, it was a problem related with the string contents, not the f string. – LeperAffinity666 Dec 02 '22 at 15:46