Removed helpers to make routes self contained.
This commit is contained in:
parent
01b2299472
commit
e7786593e1
4 changed files with 94 additions and 103 deletions
|
|
@ -1,10 +1,10 @@
|
|||
from flask import Blueprint, request, jsonify
|
||||
import random, time
|
||||
import requests
|
||||
import xml.etree.ElementTree as ET
|
||||
from datetime import datetime
|
||||
|
||||
from config import VOIPMS_ENDPOINT, ENDPOINT_OBSCURITY
|
||||
from routes.helpers import generate_message_id, parse_bearer_token, build_soap_envelope
|
||||
|
||||
bp = Blueprint('yeastar', __name__)
|
||||
|
||||
|
|
@ -118,4 +118,54 @@ def handle_yeastar_outbound():
|
|||
"title": "Message Failed",
|
||||
"detail": f"VOIP.ms returned status: {status or 'unknown'}"
|
||||
}]
|
||||
}), 400
|
||||
}), 400
|
||||
|
||||
def generate_message_id():
|
||||
return f"{random.randint(100000, 999999)}{int(time.time())}"
|
||||
|
||||
def parse_bearer_token(auth_header):
|
||||
"""
|
||||
Expect: Authorization: Bearer username|||password
|
||||
"""
|
||||
if not auth_header.startswith("Bearer "):
|
||||
return None, None
|
||||
token = auth_header.split("Bearer ")[1].strip()
|
||||
parts = token.split("|||")
|
||||
if len(parts) == 2:
|
||||
return parts[0], parts[1]
|
||||
return None, None
|
||||
|
||||
def build_soap_envelope(
|
||||
username, password, from_number, to_number,
|
||||
message_text, media_urls=None
|
||||
):
|
||||
media_urls = media_urls or []
|
||||
is_mms = len(media_urls) > 0
|
||||
soap_method = "sendMMS" if is_mms else "sendSMS"
|
||||
|
||||
envelope = f"""
|
||||
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
|
||||
xmlns:wsd="https://voip.ms/api/wsdl"
|
||||
xmlns:xsd1="https://voip.ms/api/schema">
|
||||
<soapenv:Header/>
|
||||
<soapenv:Body>
|
||||
<wsd:{soap_method}>
|
||||
<wsd:params>
|
||||
<xsd1:api_username>{username}</xsd1:api_username>
|
||||
<xsd1:api_password>{password}</xsd1:api_password>
|
||||
<xsd1:did>{from_number}</xsd1:did>
|
||||
<xsd1:dst>{to_number}</xsd1:dst>
|
||||
<xsd1:message>{message_text}</xsd1:message>
|
||||
"""
|
||||
|
||||
# Add media tags if MMS, max 3 allowed
|
||||
if is_mms:
|
||||
for i, url in enumerate(media_urls[:3]):
|
||||
envelope += f" <xsd1:media{i+1}>{url}</xsd1:media{i+1}>\n"
|
||||
|
||||
envelope += f""" </wsd:params>
|
||||
</wsd:{soap_method}>
|
||||
</soapenv:Body>
|
||||
</soapenv:Envelope>"""
|
||||
|
||||
return envelope.strip()
|
||||
Loading…
Add table
Add a link
Reference in a new issue