4
4
from npiai import Context
5
5
from npiai .tools .outlook import Outlook
6
6
from npiai .tools .email_organizer import EmailOrganizer
7
- from azure .identity import InteractiveBrowserCredential
7
+ from azure .identity import (
8
+ InteractiveBrowserCredential ,
9
+ TokenCachePersistenceOptions ,
10
+ AuthenticationRecord ,
11
+ )
12
+
13
+ token_cache = ".cache/outlook_token_cache.json"
8
14
9
15
10
16
async def main ():
17
+ # authenticate
18
+ authentication_record = None
19
+
20
+ if os .path .exists (token_cache ):
21
+ with open (token_cache , "r" ) as f :
22
+ authentication_record = AuthenticationRecord .deserialize (f .read ())
23
+
11
24
creds = InteractiveBrowserCredential (
12
25
client_id = os .environ .get ("AZURE_CLIENT_ID" , None ),
13
26
# tenant_id=os.environ.get("AZURE_TENANT_ID", None),
14
27
tenant_id = "common" ,
28
+ cache_persistence_options = TokenCachePersistenceOptions (),
29
+ authentication_record = authentication_record ,
15
30
)
16
31
32
+ record = creds .authenticate (scopes = ["Mail.Read" , "Mail.Send" ])
33
+
34
+ os .makedirs (os .path .dirname (token_cache ), exist_ok = True )
35
+
36
+ with open (token_cache , "w" ) as f :
37
+ f .write (record .serialize ())
38
+
17
39
async with EmailOrganizer (provider = Outlook (creds )) as tool :
40
+ # list emails
18
41
email_list = [email async for email in tool .list_inbox_stream (limit = 10 )]
19
42
20
43
print ("Raw email list:" , json .dumps (email_list , indent = 4 , ensure_ascii = False ))
21
44
45
+ # filter invoice-like emails
22
46
filtered_emails = []
23
47
24
48
async for result in tool .filter_stream (
@@ -34,6 +58,7 @@ async def main():
34
58
f'Subject: { result ["email" ]["subject" ]} , Matched: { result ["matched" ]} '
35
59
)
36
60
61
+ # summarize invoice-like emails
37
62
async for item in tool .summarize_stream (
38
63
ctx = Context (),
39
64
email_or_id_list = filtered_emails ,
0 commit comments