-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
137 lines (109 loc) · 3.21 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
from typing import List, Optional
from bs4 import BeautifulSoup
from dateutil import parser
from dotenv import load_dotenv
from exchangelib import (
Account,
Attendee,
CalendarItem,
Credentials,
EWSTimeZone,
Mailbox,
Message,
)
from fastapi import FastAPI, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from pydantic import BaseModel, EmailStr
load_dotenv()
app = FastAPI()
# Einloggen in den Account
creds = Credentials(os.environ.get("EMAIL"), os.environ.get("PASSWORD"))
account = Account(os.environ.get("EMAIL"), credentials=creds, autodiscover=True)
app.add_middleware(
CORSMiddleware,
allow_origins=["https://chat.openai.com"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class Email(BaseModel):
subject: str
body: str
email_address: EmailStr
class EmailData(BaseModel):
subject: str
body: str
author: EmailStr
name: str
class CalendarItemModel(BaseModel):
subject: str
body: str
location: Optional[str] = None
start_time: str
end_time: str
attendees: Optional[List[str]] = None
@app.post("/email/")
async def create_email(email: Email):
m = Message(
account=account,
folder=account.sent,
subject=email.subject,
body=email.body,
to_recipients=[
Mailbox(email_address=email.email_address),
],
)
m.send()
return {"email": email}
@app.post("/calendar/")
async def create_calendar_item(item: CalendarItemModel):
start_time = parser.parse(item.start_time)
end_time = parser.parse(item.end_time)
tz = EWSTimeZone("Europe/Berlin")
start_time = start_time.replace(tzinfo=tz)
end_time = end_time.replace(tzinfo=tz)
attendees = None
if item.attendees:
attendees = [Attendee(mailbox=att) for att in item.attendees]
cal_item = CalendarItem(
folder=account.calendar,
subject=item.subject,
start=start_time,
end=end_time,
body=item.body,
location=item.location,
required_attendees=attendees,
)
cal_item.save()
return {"calendar_item": item}
@app.get("/latest_email/", response_model=List[EmailData])
async def read_latest_emails():
latest_emails = account.inbox.all().order_by("-datetime_received")[:10]
email_list = []
for email in latest_emails:
soup = BeautifulSoup(email.body, "html.parser")
body = soup.get_text()
email_data = EmailData(
subject=email.subject,
body=body,
author=email.author.email_address,
name=email.author.name,
)
email_list.append(email_data)
return email_list
@app.get("/logo.png")
async def plugin_logo():
filename = "logo.png"
return FileResponse(filename, media_type="image/png")
@app.get("/.well-known/ai-plugin.json")
async def plugin_manifest():
with open("./.well-known/ai-plugin.json") as f:
text = f.read()
return Response(text, media_type="application/json")
@app.get("/openapi.yaml")
async def openapi_spec():
with open("openapi.yaml") as f:
text = f.read()
return Response(text, media_type="text/yaml")