Configuration

To use Twilio, we need a trial number, account sid, and auth token.

We can find our account-specific configuration credentials on the Dashboard page of the account console as shown below:

pip install twilio

import

from twilio.rest import Client

Next, modify your Score class in models.pyto look like the code below:


#defining a simple class
class Score(models.Model):
    #integer field
    test_result = models.PositiveIntegerField()

    #string representation
    def __str__(self):
        return str(self.test_result)

    #save method
    def save(self, *args, **kwargs):
        #if test_result is less than 80 execute this
        if self.test_result < 80:
            #twilio code
            account_sid = 'YOUR_ACCOUNT_SID'
            auth_token = 'YOUR_AUTH_TOKEN'
            client = Client(account_sid, auth_token)

            message = client.messages.create(
                                        body=f'Hi, your test result is {self.test_result}. Great job',
                                        from_='YOUR_TRIAL_NUMBER',
                                        to='VERIFIED_NUMBER' 
                                    )

            print(message.sid)
        return super().save(*args, **kwargs)

Last updated