def handle_messaging_callback(): # raw_data = json.loads(request.data).pop() raw_data = APIHelper.json_deserialize(request.data).pop() messaging_callback: BandwidthCallbackMessage = BandwidthCallbackMessage.from_dictionary( raw_data) message: BandwidthMessage = messaging_callback.message is_dlr = message.direction.lower().strip() == 'out' if is_dlr: log_message = 'Callback Received for: MessageId: %s, status: %s' print(log_message % (message.id, messaging_callback.description)) return 'Received Callback' owner = message.owner to_numbers = message.to.copy() to_numbers.remove(owner) to_numbers.append(message.mfrom) message_request = MessageRequest( application_id=BANDWIDTH_MSG_APPLICATION_ID, to=to_numbers, mfrom=owner) message_text = message.text.lower().strip() is_dog = message_text == 'dog' if is_dog: message_request.text = '🐶' message_request.media = ['https://bw-demo.s3.amazonaws.com/dog.jpg'] else: message_request.text = '👋 Hello From bandwidth!' messaging_client: APIController = bandwidth_client.messaging_client.client api_response: ApiResponse = messaging_client.create_message( BANDWIDTH_ACCOUNT_ID, message_request) message_response: BandwidthMessage = api_response.body log_message = 'Sent message with MessageId: %s' print(log_message % message_response.id) return "Handle messaging callback"
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
def handle_callback(): # Grab the incoming callback information raw_data = APIHelper.json_deserialize(request.data).pop() messaging_callback: BandwidthCallbackMessage = BandwidthCallbackMessage.from_dictionary( raw_data) message: BandwidthMessage = messaging_callback.message # Check the direction of the message - if we are receiving a callback saying our outbound message was sent, # we dont want to reply with another generated trivia question is_dlr = message.direction.lower().strip() == 'out' if is_dlr: """Determine if the message is inbound or outbound and handle it""" log_message = 'Callback Received for: MessageId: %s, status: %s' print(log_message % (message.id, messaging_callback.description)) return 'Outbound Message' question_type = '' question_difficulty = '' question_category = '' question = '' question_correct_answer = '' question_short_answer = '' question_incorrect_answers = '' # Parse the trivia question trivia_question = set_trivia_question() question_type = trivia_question['type'] question_category = trivia_question['category'] question = trivia_question['question'] question_correct_answer = trivia_question['correct_answer'] question_incorrect_answers = trivia_question['incorrect_answers'] if trivia_question['difficulty'] == 'easy': question_difficulty = 1 elif trivia_question['difficulty'] == 'medium': question_difficulty = 2 elif trivia_question['difficulty'] == 'hard': question_difficulty = 3 # Grab the message details and determine if user is new or existing, grab corresponding db record owner = message.owner respondents = message.mfrom database_user = determine_new_user(respondents) database_user.time = datetime.utcnow() # print('Owner', owner) # print('Respondents:', respondents) message_request = MessageRequest( application_id=BANDWIDTH_MSG_APPLICATION_ID, to=respondents, mfrom=owner) # Generate a question/answer set for multiple choice and format True/False if question_type == 'multiple': answers_list = question_incorrect_answers.copy() answers_list.append(question_correct_answer) random.shuffle(answers_list) answers_dict = { 'A': answers_list[0], 'B': answers_list[1], 'C': answers_list[2], 'D': answers_list[3] } # Match the correct letter answer from the dict key with the correct answer value after randomization for key in answers_dict: if question_correct_answer == answers_dict[key]: question_short_answer = str(key) else: pass question_text = str(question) + ' (' + str(question_difficulty) + ' pts.)' + '\n\n' + \ 'A. ' + str(answers_list[0]) + '\n' + 'B. ' + str(answers_list[1]) + '\n' + \ 'C. ' + str(answers_list[2]) + '\n' + 'D. ' + str(answers_list[3]) elif question_type == 'boolean': question_text = 'True or False? (' + str( question_difficulty) + ' pts.)' + '\n\n' + str(question) answers_dict = {'T': 'True', 'F': 'False'} for key in answers_dict: if question_correct_answer == answers_dict[key]: question_short_answer = str(key) else: pass # If user is existing, see if they are sending an answer attempt and match it to the correct answer # held in the database if message.text.lower().strip() == 'new': # If user is requesting a new question, clear their answer field and generate a new question database_user.currentQuestion = question database_user.currentAnswer = question_correct_answer database_user.currentShortAnswer = question_short_answer db.session.commit() message_request.text = str(question_text) + '\n\n' + 'Total Points: ' + str(database_user.points) + \ '\nText \'Help\' for help.\nText \'new\' to generate a new question. ' \ '\nText \'delete\' to permanently delete your account. \nThanks for playing Text Trivia!' messaging_client: APIController = bandwidth_client.messaging_client.client api_response: ApiResponse = messaging_client.create_message( BANDWIDTH_ACCOUNT_ID, message_request) message_response: BandwidthMessage = api_response.body return 'New Question Generated' elif message.text.lower().strip() == 'delete': # If user is requesting account deletion - delete db info TriviaUser.query.filter_by(phoneNumber=respondents).delete() db.session.commit() message_request.text = str( 'Account successfully deleted. ' 'Simply text us again if you\'d like to make a new one!') messaging_client: APIController = bandwidth_client.messaging_client.client api_response: ApiResponse = messaging_client.create_message( BANDWIDTH_ACCOUNT_ID, message_request) message_response: BandwidthMessage = api_response.body return 'Account Deleted' elif message.text.lower().strip() == 'help': # Send a help message with the available message commands message_request.text = 'Text \'new\' to receive a new question.\n\nText \'delete\' to permanently delete ' \ 'your account.\n\nYou can respond to multiple choice questions by either responding ' \ 'with the corresponding letter choice or texting the full answer.\nTrue/False ' \ 'questions can be answered by responding with either T, F, True, or False.' \ '\n\nCurrent Points: ' + str(database_user.points) + '\nMax Points: ' + \ str(database_user.maxPoints) + '\nLives Remaining: ' + str(database_user.lives) messaging_client: APIController = bandwidth_client.messaging_client.client api_response: ApiResponse = messaging_client.create_message( BANDWIDTH_ACCOUNT_ID, message_request) message_response: BandwidthMessage = api_response.body return 'Help is on the way!' elif message.text.lower().strip() == database_user.currentAnswer.lower().strip() or message.text.lower().strip() \ == database_user.currentShortAnswer.lower().strip(): answer_confirmation_text = 'Correct!' # Award points for giving the correct answer database_user.points += int(question_difficulty) if database_user.points > database_user.maxPoints: database_user.maxPoints = int(database_user.points) # set the answer field in the database record to match the newly generated answer database_user.currentQuestion = question database_user.currentAnswer = question_correct_answer database_user.currentShortAnswer = question_short_answer db.session.commit() else: if database_user.currentAnswer == '': answer_confirmation_text = 'Welcome to Text Trivia!' else: answer_confirmation_text = 'Incorrect. The correct answer was ' + \ str(database_user.currentAnswer).strip() + '.' database_user.lives -= 1 if database_user.lives == 0: # Reset current point streak if user runs out of lives database_user.points = 0 database_user.lives = 5 answer_confirmation_text = answer_confirmation_text + '\n\nYou are out of lives! ' \ 'Your points have been reset.' database_user.currentQuestion = question database_user.currentAnswer = question_correct_answer database_user.currentShortAnswer = question_short_answer db.session.commit() new_message_text = str( str(answer_confirmation_text) + '\n\n' + str(question_text) + '\n\n\n' + 'Total Points: ' + str(database_user.points) + '\nLives: ' + str(database_user.lives) + '\nBest Score: ' + str(database_user.maxPoints) + '\n\nThanks for playing Text Trivia!\nText \'Help\' for help.') # Create and send our message using Bandwidht's API message_request.text = new_message_text messaging_client: APIController = bandwidth_client.messaging_client.client api_response: ApiResponse = messaging_client.create_message( BANDWIDTH_ACCOUNT_ID, message_request) message_response: BandwidthMessage = api_response.body return ''