abc-messenger/mail.py
Andrew Gundersen 2287ddd6f1 stable remote app
2020-12-11 15:23:59 -06:00

46 lines
No EOL
1 KiB
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")
audio = p.get("audio")
mail = typs.Mail(owner, target, text, audio)
return mail
@staticmethod
def __decode(mail: typs.Mail) -> typs.Intent:
name = "message"
params = {
"owner": mail.owner,
"text": mail.text, #dev only
"audio": mail.audio
}
intent = typs.Intent(name, params)
return intent