add sending of email; fix misc. bugs
This commit is contained in:
parent
5ff6fc7ca2
commit
def5cd11ec
@ -1,8 +1,10 @@
|
|||||||
from pcomon.messages import MessageDeliverer, Message
|
from pcomon.messages import PCOMessageDeliverer, PCOMessage
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from subprocess import Popen, PIPE
|
||||||
|
|
||||||
read_messages = "/run/test/messages"
|
read_messages = "/run/test/messages"
|
||||||
|
|
||||||
def get_creds(filename: str) -> dict[str, str]:
|
def get_creds(filename: str) -> dict[str, str]:
|
||||||
@ -33,14 +35,16 @@ def write_read_messages(read_messages: list[str], from_file: str = read_messages
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def send_email(msg: Message) -> None:
|
def send_email(msg: PCOMessage) -> None:
|
||||||
print(msg)
|
p = Popen(["sendmail", "-t", "-oi"], stdin=PIPE)
|
||||||
|
p.communicate(msg.email.as_string().encode('utf-8', 'ignore'))
|
||||||
|
p.wait()
|
||||||
|
|
||||||
def entry():
|
def entry():
|
||||||
creds_file = sys.argv[1]
|
creds_file = sys.argv[1]
|
||||||
creds = get_creds(creds_file)
|
creds = get_creds(creds_file)
|
||||||
|
|
||||||
deliverer = MessageDeliverer(
|
deliverer = PCOMessageDeliverer(
|
||||||
app_id=creds["PCO_APP_ID"],
|
app_id=creds["PCO_APP_ID"],
|
||||||
app_secret=creds["PCO_APP_SECRET"],
|
app_secret=creds["PCO_APP_SECRET"],
|
||||||
person_id=creds["PCO_PERSON_ID"],
|
person_id=creds["PCO_PERSON_ID"],
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import requests
|
import requests
|
||||||
from requests.auth import HTTPBasicAuth
|
from requests.auth import HTTPBasicAuth
|
||||||
|
|
||||||
from email.parser import BytesParser
|
from email import message_from_string
|
||||||
from email.message import Message
|
from email.message import Message
|
||||||
from email.header import Header
|
from email.header import Header
|
||||||
from email.utils import formataddr
|
from email.utils import formataddr
|
||||||
@ -13,7 +13,7 @@ from typing import ClassVar
|
|||||||
api_domain = "https://api.planningcenteronline.com"
|
api_domain = "https://api.planningcenteronline.com"
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Message:
|
class PCOMessage:
|
||||||
eml_content: str
|
eml_content: str
|
||||||
message_id: str
|
message_id: str
|
||||||
from_name: str
|
from_name: str
|
||||||
@ -21,27 +21,30 @@ class Message:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def email(self):
|
def email(self):
|
||||||
parsed_email: Message = BytesParser(self.eml_content)
|
parsed_email: Message = message_from_string(self.eml_content)
|
||||||
email_text = parsed_email.get_body(
|
|
||||||
preference_list=("plain", "html")
|
for part in parsed_email.walk():
|
||||||
)
|
if part.get_content_type() == "text/plain":
|
||||||
|
email_text = part.get_payload(decode=True).decode('utf-8')
|
||||||
|
|
||||||
message = Message()
|
message = Message()
|
||||||
msg['From'] = 'pcomon@mail.beepboop.systems'
|
message['From'] = 'pcomon@mail.beepboop.systems'
|
||||||
msg['From'] = formataddr((
|
message['From'] = formataddr((
|
||||||
str(Header(self.from_name + " (via pcomon)", "utf-8")), "pcomon@mail.beepboop.systems"
|
str(Header(self.from_name + " (via pcomon)", "utf-8")), "pcomon@mail.beepboop.systems"
|
||||||
))
|
))
|
||||||
msg['To'] = 'ryan@beepboop.systems'
|
message['To'] = 'ryan@beepboop.systems'
|
||||||
msg['Subject'] = self.subject
|
message['Subject'] = self.subject
|
||||||
msg.add_header("Content-Type", "text")
|
message.add_header("Content-Type", "text")
|
||||||
msg.set_payload(email_text)
|
message.set_payload(email_text)
|
||||||
|
|
||||||
|
return message
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class MessageDeliverer:
|
class PCOMessageDeliverer:
|
||||||
app_id: str
|
app_id: str
|
||||||
app_secret: str
|
app_secret: str
|
||||||
person_id: str
|
person_id: str
|
||||||
messages: ClassVar[list[Message]] = []
|
messages: ClassVar[list[PCOMessage]] = []
|
||||||
has_fetched: ClassVar[bool] = False
|
has_fetched: ClassVar[bool] = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -60,7 +63,7 @@ class MessageDeliverer:
|
|||||||
msg_eml_content = requests.get(message["attributes"]["file"]["url"]).text
|
msg_eml_content = requests.get(message["attributes"]["file"]["url"]).text
|
||||||
|
|
||||||
self.messages.append(
|
self.messages.append(
|
||||||
Message(
|
PCOMessage(
|
||||||
eml_content=msg_eml_content,
|
eml_content=msg_eml_content,
|
||||||
message_id=message["id"],
|
message_id=message["id"],
|
||||||
from_name=message["attributes"]["from_name"],
|
from_name=message["attributes"]["from_name"],
|
||||||
@ -71,7 +74,7 @@ class MessageDeliverer:
|
|||||||
self.has_fetched = True
|
self.has_fetched = True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id_dictionary(self) -> dict[str, Message]:
|
def id_dictionary(self) -> dict[str, PCOMessage]:
|
||||||
if not self.has_fetched:
|
if not self.has_fetched:
|
||||||
return ValueError("you need to call self.fetch_messages() first")
|
return ValueError("you need to call self.fetch_messages() first")
|
||||||
|
|
||||||
@ -95,7 +98,7 @@ def main():
|
|||||||
app_secret = env["PCO_APP_SECRET"]
|
app_secret = env["PCO_APP_SECRET"]
|
||||||
person_id = env["PCO_PERSON_ID"]
|
person_id = env["PCO_PERSON_ID"]
|
||||||
|
|
||||||
deliverer = MessageDeliverer(app_id, app_secret, person_id)
|
deliverer = PCOMessageDeliverer(app_id, app_secret, person_id)
|
||||||
deliverer.fetch_messages()
|
deliverer.fetch_messages()
|
||||||
print(deliverer.messages)
|
print(deliverer.messages)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user