abc-messenger/mail.py
Andrew Gundersen ae6bf0f96d messenger v0.9
2020-11-29 08:48:56 -06:00

45 lines
No EOL
994 B
Python

# mail.py
import typs
import mailroom
class Mail:
"""Session interface for mailroom.
Two main IO methods. Will translate intents to mail and vice versa.
Should be a parent class of Session.
"""
def __init__(self):
self.crimata_id = False
# Receive intent.
async def mailbox_recv(self):
mail = await mailroom.get_mail(self.crimata_id)
intent = self.__decode(mail)
return intent
# Send intent.
def mailbox_send(self, intent):
mail = self.__encode(intent)
print(f"Putting message into outbox {self.crimata_id}")
mailroom.put_mail(self.crimata_id, mail)
def __encode(self, intent: typs.Intent) -> typs.Mail:
p = intent.params
owner = self.crimata_id
target = p.get("target")
text = p.get("text")
mail = typs.Mail(owner, target, text)
return mail
@staticmethod
def __decode(mail: typs.Mail) -> typs.Intent:
name = "message"
params = {
"owner": mail.owner,
"text": mail.text #dev only
}
intent = typs.Intent(name, params)
return intent