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?
CodePudding user response:
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.