0

I'm trying to save both messages that get sent out and received in a database, including message sid, message body, so that I can process them using some logic.

class OutgoingMessage(models.Model):
    ''' Table of SMS messages that are sent out to users by Twilio '''

    outgoing_sid = models.CharField(max_length=40)
    sent_date = models.DateTimeField()
    sender = models.ForeignKey(TwilioNumber, related_name='outgoing_messages')
    recipient = models.ForeignKey(Caller, related_name='outgoing_messages')
    message_body = models.TextField(max_length=1600)



class IncomingMessage(models.Model):
    ''' Table of SMS messages received by Twilio from users '''

    incoming_sid = models.CharField(max_length=40)
    sent_date = models.DateTimeField()
    sender = models.ForeignKey(Caller, related_name='incoming_messages')
    recipient = models.ForeignKey(TwilioNumber, related_name='incoming_messages')
    message_body = models.TextField(max_length=1600)

Is there a straightforward way of getting the message sid from a message Twilio sends out, immediately after it gets sent? Getting the sid from an incoming message is pretty easy but the other way around is not very clear.

I'm trying to look for an alternative to using cookies, as suggested in this post

Community
  • 1
  • 1
655321
  • 411
  • 4
  • 26

1 Answers1

1

I found the answer here

# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import TwilioRestClient

# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "AC1ab0bb368322688c3d6cbb1355aeef9b"
auth_token  = "{{ auth_token }}"
client = TwilioRestClient(account_sid, auth_token)


message = client.messages.create(body="Jenny please?! I love you <3",
    to="+15558675309",
    from_="+14158141829",
    media_url="http://www.example.com/hearts.png")
print message.sid

However, this doesn't seem to work very well with django_twilio views.

655321
  • 411
  • 4
  • 26