def test_successful_create_message(self, messaging_client): """Create valid request to send an SMS using the Messaging API. Args: messaging_client: Contains the basic auth credentials needed to authenticate. """ message_body = MessageRequest() message_body.application_id = BW_MESSAGING_APPLICATION_ID message_body.to = [USER_NUMBER] message_body.mfrom = BW_NUMBER message_body.text = "Python Monitoring" create_response = messaging_client.create_message(BW_ACCOUNT_ID, message_body) create_response_body = create_response.body print(vars(create_response)) assert (create_response.status_code == 202) assert len(create_response_body.id) == 29 # asserts `messageId` returned and matches expected length (29) # asserts `owner` matches `mfrom` number and `BW_NUMBER` assert create_response_body.owner == create_response_body.mfrom == BW_NUMBER assert create_response_body.application_id == BW_MESSAGING_APPLICATION_ID # asserts the date string is valid ISO assert dateutil.parser.isoparse(str(create_response_body.time)) assert type(create_response_body.segment_count) is int assert create_response_body.to == [USER_NUMBER] assert create_response_body.media == message_body.media assert create_response_body.text == message_body.text assert create_response_body.tag == message_body.tag assert create_response_body.priority == message_body.priority
def handle_inbound_media_mms(to, from_, media): """ Takes information from a Bandwidth inbound message callback that includes media and responds with a text message containing the same media sent through Bandwidth's media resource. :param list<str> to: The list of phone numbers that received the message :param str from_: The phone number that sent the message :param list<str> media: The list of media sent in the message :returns: None """ downloaded_media_files = download_media_from_bandwidth(media) upload_media_to_bandwidth(downloaded_media_files) remove_files(downloaded_media_files) body = MessageRequest() body.application_id = MESSAGING_APPLICATION_ID body.to = [from_] body.mfrom = to body.text = "Rebound!" #Build the media URL by taking the media ids (that doubled as the file names) and appending them to #the bandwidth media base url body.media = [ BANDWIDTH_MEDIA_BASE_ENDPOINT + media_file for media_file in downloaded_media_files ] try: messaging_client.create_message(MESSAGING_ACCOUNT_ID, body) except Exception as e: print(e) return None
def create_message(): data = json.loads(request.data) body = MessageRequest() body.to = [data["to"]] body.mfrom = data["from"] body.text = data["text"] body.application_id = MESSAGING_APPLICATION_ID messaging_client.create_message(MESSAGING_ACCOUNT_ID, body=body) return "Send a text message"
def handle_voice_callback_status(): data = json.loads(request.data) #data["tag"] contains the full recording url, if present #Format: https://voice.bandwidth.com/api/v2/accounts/123/calls/c-id/recordings/r-id/media if "tag" in data.keys(): call_id = data["tag"].split("/")[-4] recording_id = data["tag"].split("/")[-2] #Download media from voice API media_content = voice_client.get_stream_recording_media(VOICE_ACCOUNT_ID, call_id, recording_id).body #Upload media to messaging API messaging_client.upload_media(MESSAGING_ACCOUNT_ID, recording_id, str(len(media_content)), body=media_content) #Send text body = MessageRequest() body.application_id = MESSAGING_APPLICATION_ID body.mfrom = data["to"] body.to = [data["from"]] body.text = "Attached is your recorded message" body.media = ["https://messaging.bandwidth.com/api/v2/users/{account_id}/media/{recording_id}".format(account_id=MESSAGING_ACCOUNT_ID, recording_id=recording_id)] messaging_client.create_message(MESSAGING_ACCOUNT_ID, body=body) return ""
def handle_inbound_sms(to, from_): """ Take information from a Bandwidth inbound message callback and responds with a text message with the current date and time :param list<str> to: The list of phone numbers that received the message :param str from_: The phone number that sent the text message :returns: None """ body = MessageRequest() body.application_id = MESSAGING_APPLICATION_ID body.to = [from_] body.mfrom = to body.text = "The current date-time is: " + str( time.time() * 1000) + " milliseconds since the epoch" try: messaging_client.create_message(MESSAGING_ACCOUNT_ID, body) except Exception as e: print(e) return None
def test_failed_create_message(self, messaging_client): """Create invalid request to send an SMS using the Messaging API. Args: messaging_client: Contains the basic auth credentials needed to authenticate. """ with pytest.raises(MessagingException): # asserts that a messaging exception is raised message_body = MessageRequest() message_body.application_id = BW_MESSAGING_APPLICATION_ID message_body.to = ["+1invalid"] message_body.mfrom = BW_NUMBER message_body.text = "Python Monitoring" create_response = messaging_client.create_message(BW_ACCOUNT_ID, message_body) create_response_body = create_response.body print(vars(create_response)) assert create_response.status_code == 400 assert type(create_response_body.type) == "request-validation" assert type(create_response_body.description) is str