class FarazSMS(adaptor.Client): def __init__(self, excel_or_list, service, from_sender, to_receivers, subject, content, templates, name): super().__init__(self, excel_or_list, service, from_sender, to_receivers, subject, content, templates, name) def create(self): # self.sms = FarazClient(os.environ.get('FARAZ_API_KEY')) # self.from_sender = os.environ.get('FARAZ_NUMBER') # print(os.environ.get('FARAZ_API_KEY')) # print(os.environ.get('FARAZ_NUMBER')) self.sms = FarazClient("lE4WSCxzQlexOHiNj7Cdr1piDhpPE8vKFhUuSIeUSyM=") self.name = self.name self.content = self.content if type(self.to_receivers) is list: self.to_receivers = self.to_receivers else: self.to_receivers = [self.to_receivers] def send_with_service(self): print(self.to_receivers) print(self.content) try: bulk_id = self.sms.send(originator=self.from_sender, recipients=self.to_receivers, message=self.content) print('messages send successfully', bulk_id) except Error as e: print("Error handled => code: %s" % e.code) def create_template(self): self.template_for_all_services = self.template_for_all_services def send_with_template(self): print(self.to_receivers) print(self.name) for receiver, content, name in zip(self.to_receivers, self.template_for_all_services, self.name): try: bulk_id = self.sms.send(originator=self.from_sender, recipients=[receiver], message=content) print(f'messages send successfully to {name} -->bulk_id :{bulk_id}') except Error as e: print("Error handled => code: %s" % e.code)
def test_parse_exception(self): http_client = Mock() http_client.post.return_value = Response( json.loads(r''' { "status": "OK", "code": "OK", "message": "Ok", "data": { "bulk_idd": 70671101 } } ''')) sms = Client("", http_client) with self.assertRaises(ValueError): sms.send("9810001", ["98912xxxxxxx", "98913xxxxxxx"], "Hello")
def test_send_api_call(self): http_client = Mock() http_client.post.return_value = Response( json.loads(r''' { "status": "OK", "code": "OK", "message": "Ok", "data": { "bulk_id": 70671101 } } ''')) sms = Client("", http_client) sms.send("9810001", ["98912xxxxxxx", "98913xxxxxxx"], "Hello") http_client.post.assert_called_once_with( "/v1/messages", { "originator": "9810001", "recipients": ["98912xxxxxxx", "98913xxxxxxx"], "message": "Hello", })
def processParagraph(): client = Client("mSlhtsSZUVW0KdquNmHHewZLEpu33K0ZQ_DFx2R3acI=") try: bulk_id = client.send("+983000505", ["+989124499659"], "Hello from python client!") print(bulk_id) except Error as e: print("Error handled => code: %s, message: %s" % (e.code, e.message)) if e.code == ResponseCode.ErrUnprocessableEntity.value: for field in e.message: print("Field: %s , Errors: %s" % (field, e.message[field])) except HTTPError as e: print("Error handled => code: %s" % (e)) return client
def test_send_response_parse(self): http_client = Mock() http_client.post.return_value = Response( json.loads(r''' { "status": "OK", "code": "OK", "message": "Ok", "data": { "bulk_id": 70671101 } } ''')) sms = Client("", http_client) bulk_id = sms.send("9810001", ["98912xxxxxxx", "98913xxxxxxx"], "Hello") self.assertEqual(bulk_id, 70671101)
from ippanel import Client, Error, HTTPError, ResponseCode client = Client("YOUR-API-KEY") try: bulk_id = client.send("+9810001", ["+98912xxxxxxx"], "Hello from python client!") print(bulk_id) except Error as e: print("Error handled => code: %s, message: %s" % (e.code, e.message)) if e.code == ResponseCode.ErrUnprocessableEntity.value: for field in e.message: print("Field: %s , Errors: %s" % (field, e.message[field])) except HTTPError as e: print("Error handled => code: %s" % (e))