[docs]defclean_body(body:str)->str:"""Clean body of a message or event."""try:frombs4importBeautifulSouptry:# Remove HTMLsoup=BeautifulSoup(str(body),"html.parser")body=soup.get_text()# Remove return charactersbody="".join(body.splitlines())# Remove extra spacesbody=" ".join(body.split())returnstr(body)exceptException:returnstr(body)exceptImportError:returnstr(body)
[docs]defauthenticate()->Account:"""Authenticate using the Microsoft Graph API"""try:fromO365importAccountexceptImportErrorase:raiseImportError("Cannot import 0365. Please install the package with `pip install O365`.")fromeif"CLIENT_ID"inos.environand"CLIENT_SECRET"inos.environ:client_id=os.environ["CLIENT_ID"]client_secret=os.environ["CLIENT_SECRET"]credentials=(client_id,client_secret)else:logger.error("Error: The CLIENT_ID and CLIENT_SECRET environmental variables have not ""been set. Visit the following link on how to acquire these authorization ""tokens: https://learn.microsoft.com/en-us/graph/auth/")returnNoneaccount=Account(credentials)ifaccount.is_authenticatedisFalse:ifnotaccount.authenticate(scopes=["https://graph.microsoft.com/Mail.ReadWrite","https://graph.microsoft.com/Mail.Send","https://graph.microsoft.com/Calendars.ReadWrite","https://graph.microsoft.com/MailboxSettings.ReadWrite",]):print("Error: Could not authenticate")# noqa: T201returnNoneelse:returnaccountelse:returnaccount
UTC_FORMAT="%Y-%m-%dT%H:%M:%S%z""""UTC format for datetime objects."""