1- import re
21import boto3
32
43from django .conf import settings
@@ -22,60 +21,6 @@ class SNSSMSBackend(SMSBackend):
2221 'SENDER_ID' : None ,
2322 }
2423
25- def _clean_sender_name (self , sender_name : str ) -> str | None :
26- """
27- Clean sender name to comply with AWS SNS requirements:
28- - Maximum 11 alphanumeric or hyphen (-) characters
29- - At least one letter
30- - No spaces
31- - Must start and end with an alphanumeric character
32- """
33- if not sender_name or not sender_name .strip ():
34- return None
35-
36- # First pass: preserve hyphens and convert to CamelCase
37- # Split by spaces and non-alphanumeric except hyphens
38- parts = re .split (r'[^\w-]+' , sender_name )
39-
40- # Filter out empty parts and create CamelCase while preserving hyphens
41- camel_case_parts = []
42- for part in parts :
43- if part :
44- # Handle parts with hyphens
45- if '-' in part :
46- # Keep hyphen structure but capitalize each sub-part
47- subparts = part .split ('-' )
48- camel_subparts = [sub .capitalize () if sub else '' for sub in subparts ]
49- camel_case_parts .append ('-' .join (camel_subparts ))
50- else :
51- # Regular word - keep original case for acronyms like "OTP"
52- if part .isupper () and len (part ) <= 4 : # Likely acronym
53- camel_case_parts .append (part )
54- else :
55- camel_case_parts .append (part .capitalize ())
56-
57- cleaned = '' .join (camel_case_parts )
58-
59- # Remove any remaining non-alphanumeric characters except hyphens
60- cleaned = re .sub (r'[^a-zA-Z0-9-]' , '' , cleaned )
61-
62- # Ensure it starts and ends with alphanumeric
63- cleaned = re .sub (r'^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$' , '' , cleaned )
64-
65- # If empty after cleaning, return None
66- if not cleaned :
67- return None
68-
69- # Ensure at least one letter is present
70- if not re .search (r'[a-zA-Z]' , cleaned ):
71- # If no letter, add "Msg" suffix (truncate if needed)
72- if len (cleaned ) > 8 :
73- cleaned = cleaned [:8 ]
74- cleaned += 'Msg'
75-
76- # Final truncation to ensure we don't exceed 11 characters
77- return cleaned [:11 ] if len (cleaned ) > 11 else cleaned
78-
7924 def _get_sns_client (self ):
8025 """
8126 Connect to the SNS service
@@ -100,20 +45,15 @@ def publish_message(self, message):
10045 'PhoneNumber' : str (message .recipient ),
10146 'Message' : message .content ,
10247 }
103-
104- # Use sender from message (template.sender_name) or fallback to config SENDER_ID
105- sender_name = message .sender or self .config ['SENDER_ID' ]
106- if sender_name :
107- cleaned_sender = self ._clean_sender_name (sender_name )
108- if cleaned_sender :
109- publish_kwargs .update ({
110- 'MessageAttributes' : {
111- 'AWS.SNS.SMS.SenderID' : {
112- 'DataType' : 'String' ,
113- 'StringValue' : cleaned_sender ,
114- }
48+ if self .config ['SENDER_ID' ]:
49+ publish_kwargs .update ({
50+ 'MessageAttributes' : {
51+ 'AWS.SNS.SMS.SenderID' : {
52+ 'DataType' : 'String' ,
53+ 'StringValue' : self .config ['SENDER_ID' ],
11554 }
116- })
55+ }
56+ })
11757 try :
11858 sns_client .publish (** publish_kwargs )
11959 self ._update_message_after_sending (message , state = OutputSMSMessageState .SENT , sent_at = timezone .now ())
0 commit comments