41 lines
1.6 KiB
Python
41 lines
1.6 KiB
Python
async def send_sms(hass, user, password, sender_did, call):
|
|
"""Send SMS message using multipart form-data like MMS."""
|
|
_LOGGER = logging.getLogger(__name__)
|
|
recipient = call.data.get("recipient")
|
|
message = call.data.get("message")
|
|
|
|
if not recipient or not message:
|
|
_LOGGER.error("Recipient or message missing.")
|
|
return
|
|
|
|
# Build form data dictionary
|
|
form_data = {
|
|
'api_username': str(user),
|
|
'api_password': str(password),
|
|
'did': str(sender_did),
|
|
'dst': str(recipient),
|
|
'message': str(message),
|
|
'method': 'sendSMS'
|
|
}
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
with aiohttp.MultipartWriter("form-data") as mp:
|
|
for key, value in form_data.items():
|
|
part = mp.append(value)
|
|
part.set_content_disposition('form-data', name=key)
|
|
|
|
_LOGGER.error("voipms_sms: sending SMS: %s", mp)
|
|
async with session.post(REST_ENDPOINT, data=mp) as response:
|
|
response_text = await response.text()
|
|
if response.status == 200:
|
|
response_json = json.loads(response_text)
|
|
if response_json['status'] == "success":
|
|
_LOGGER.info("voipms_sms: SMS sent successfully: %s", response_text)
|
|
else:
|
|
_LOGGER.error("voipms_sms: SMS not sent: %s", response_text)
|
|
else:
|
|
_LOGGER.error("voipms_sms: Failed to send SMS. Status: %s, Response: %s", response.status, response_text)
|
|
|
|
|
|
|