Sending Messages

Hello,

I do not understand how to create this mailing system.

The function send_message takes the string parameter receiver which contains the email address, the string parameter sender which contains the email address of the sender, and a string parameter mail which contains the text of the email message.

This function will send the mail by using the following function system.post_message , which also takes two string parameters for the email addresses and a string parameter for the message, as described below:

system.post_message(receiver:string, sender:string, mail:string)->string

And the system.post_message returns a string with the following format:

  • ‘Message to [receiver] successfully sent’ if the email has been successfully sent.
  • ‘Message from [sender] not sent’ if the email has not been sent.

If the message was not sent, then your function send_message will attempt to send the email again until it is sent successfully.

Once the email has been sent, the function send_message will return the following message:

‘The message from [sender] to [receiver] has been sent after [x] attempts’


The code below I used as import variable:

import random

class system:

def __init__(self):
    self.reset_messages()

def reset_messages(self):
    """
    Normally text like this would be saved in a text file outside of the code
    But I don't want to overload the students with files
    """
    bob = 'bob.co@google'
    yuri = 'yur1_bal1@uvtue'
    frank = 'frank.start@lego'
    self.messages = [(1, yuri, bob, 'Hey Yuri!'),
                     (1, bob, yuri, 'Hi bob'),
                     (1, yuri, bob, 'Hi Yuri, can you take a look at my work?'),
                     (1, yuri, bob, 'Yuri?'),
                     (1, yuri, frank, 'Can you believe this guy?'),
                     (1, yuri, bob, 'Can you please help me? I am stuck'),
                     (1, bob, yuri, 'Sure, I can help you in a few minutes'),
                     (2, bob, frank, 'Bob, have you got your work done yet?'),
                     (2, frank, bob, 'I am working on it boss! It is almost done'),
                     (2, bob, frank, 'Did you just ask Yuri to do it?'),
                     (2, bob, frank, 'Yuri has his own tasks, go do your work'),
                     (2, bob, bob, 'I better start working on it immediately'),
                     (3, yuri, frank, 'Yuri, do your own work, it has to be done by today'),
                     (3, frank, yuri, "Yes sir, of course sir, it's going well so far"),
                     (3, yuri, frank, 'Good to hear'),
                     (3, frank, yuri, 'I am done now')]

def get_message(self):
    return self.messages.pop(0)

def is_empty(self):
    return len(self.messages) == 0

@staticmethod
def post_message(receiver: str, sender: str, mail: str) -> str:
    """ Simulation function that simulates messages being sent

    :param receiver: String with email of receiver mail
    :param sender: String with email of the sender of mail
    :param mail: String with the email
    :return: A string saying if the mail has been send or not
    """
    # There is a 5% chance the mail is successfully sent
    if random.randint(1, 100) > 95:
        return 'Message to {} successfully sent'.format(receiver)
    return 'Message from {} not sent'.format(sender)