Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SMSMessage(Base):
- """Inbound/outbound SMS message."""
- __tablename__ = "sms_message"
- id = Column(Integer, autoincrement=True, primary_key=True)
- #: When this review was created
- created_at = Column(UTCDateTime, default=now)
- # Who is the customer we are having a dialog with
- customer_id = Column(ForeignKey("users.id"))
- customer = relationship(User, primaryjoin=customer_id == User.id, backref="sms_messages")
- sms_message_type = Column(Enum('incoming', 'outgoing', name="sms_message_type"), nullable=False)
- message = Column(String(1600), nullable=True, default=None)
- # Attachents are handled on file-system, not on database, but we record a count here so we can read them
- attachment_count = Column(Integer, nullable=False, default=0)
- # Any Twilio location data from celltower/phone number
- location_data = sqlalchemy.Column(JSONB, default={}, nullable=True)
- @classmethod
- def handle_incoming(cls, phone_number, message, attachments:typing.List=None, registry=None, basic_auth_username=None, basic_auth_password=None):
- """
- :param phone_number:
- :param message:
- :param attachments: List of media URLs
- :return:
- """
- user, created = User.get_or_create_sms_user(phone_number)
- if not user.id:
- DBSession.flush()
- assert user.id
- sms = SMSMessage(customer=user, sms_message_type="incoming", message=message)
- DBSession.add(sms)
- if attachments:
- DBSession.flush()
- sms.attachment_count = len(attachments)
- for a in attachments:
- try:
- UserMedia.fetch_from_url(registry, a, user=user, sms=sms, basic_auth_username=basic_auth_username, basic_auth_password=basic_auth_password)
- except Exception as e:
- logger.error("Could not get attachment %s", a)
- logger.exception(e)
- return user, sms, created
- def get_attachments(self):
- return self.attached_medias
- Index('sms_message_customer_index', SMSMessage.customer_id)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement