11import sys
22import requests
33from base64 import b64encode
4- from typing import List , Dict
4+ from typing import List , Dict , Union
55from collections import namedtuple
66
77
88class NextSms (object ):
99
10- BASE_URL = ' https://messaging-service.co.tz/api/sms/v1/test/text/single'
10+ BASE_URL_SINGLE = 'https://messaging-service.co.tz/api/sms/v1/test/text/single'
11+ BASE_URL_MULTIPLE = 'https://messaging-service.co.tz/api/sms/v1/test/text/multi'
1112
1213 User = namedtuple ('User' , 'username password secret_key' )
1314
@@ -30,7 +31,7 @@ def create_user(self, username, password) -> User:
3031 secret_key = b64encode (f'{ username } :{ password } ' .encode ()).decode ()
3132 )
3233
33- def create_header (self ):
34+ def create_header (self ) -> Dict :
3435 if not self ._user :
3536 raise Exception (
3637 '''
@@ -46,24 +47,41 @@ def create_header(self):
4647 'Authorization' : f'Basic { self ._user .secret_key } '
4748 }
4849
49- def send_sms (self , message : str , recipient : str , sender_id : str = "NEXTSMS" ):
50+ def send_sms (self , message : str , recipients : Union [ str , List ], sender_id : str = "NEXTSMS" ) -> Dict :
5051 if not isinstance (sender_id , str ):
5152 raise TypeError (
5253 f"sender_id should of type <class 'str'> not { type (sender_id )} " )
53- if not isinstance (recipient , str ):
54+ if not isinstance (recipients , ( str , list ) ):
5455 raise TypeError (
55- f"recipient should be of type <class 'str'> not { type (recipient )} " )
56+ f"recipient should be of type <class 'str'> or <class 'list'> not { type (recipients )} " )
5657 if not isinstance (message , str ):
5758 raise TypeError (
5859 f"message should be of type <class 'str'> not { type (message )} " )
5960
6061 return requests .post (
61- self .BASE_URL ,
62+ self .BASE_URL_SINGLE ,
6263 headers = self .create_header (),
6364 json = {
6465 'from' : sender_id ,
65- 'to' : recipient ,
66+ 'to' : recipients ,
6667 'text' : message
68+ }).json ()
69+
70+ def send_bulk (self , messages : List , sender_id : str = "NEXTSMS" ):
71+
72+ if not isinstance (sender_id , str ):
73+ raise TypeError (
74+ f"sender_id should be of type <class 'str'> not { type (sender_id )} " )
75+
76+ if not isinstance (messages , list ):
77+ raise TypeError (
78+ f"messages should be of type <class 'list'> not type { type (messages )} " )
79+
80+ return requests .post (
81+ self .BASE_URL_MULTIPLE ,
82+ headers = self .create_header (),
83+ json = {
84+ 'messages' : messages
6785 }
6886 ).json ()
6987
0 commit comments