├── config-example.ini ├── .gitignore ├── README.md └── outlook.py /config-example.ini: -------------------------------------------------------------------------------- 1 | [Outlook] 2 | private = some_folder 3 | last_update = 4 | 5 | [Telegram] 6 | access_token = telegram_token 7 | chat_id = my_chat_id 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | config.ini 2 | *.db 3 | *.mo 4 | 5 | *.pub 6 | 7 | .idea/ 8 | .idea/* 9 | 10 | coverage/ 11 | 12 | lib/ 13 | lib/* 14 | 15 | *.py[cod] 16 | *.pyo 17 | 18 | bin/ 19 | bin/* 20 | 21 | share/ 22 | share/* 23 | 24 | local/ 25 | local/* 26 | 27 | include/ 28 | include/* 29 | 30 | env/ 31 | venv/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | pre install: 2 | 3 | - python (tested with 3.5.2) 4 | - win32com https://sourceforge.net/projects/pywin32/ 5 | 6 | pip install pypiwin32 7 | - python-telegram-bot 8 | 9 | pip install python-telegram-bot 10 | ----------------------------------- 11 | Interop Outlook Mailitem Properties: 12 | 13 | https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mailitem_properties.aspx 14 | 15 | -------------------------------------------------------------------------------- /outlook.py: -------------------------------------------------------------------------------- 1 | from win32com.client import Dispatch 2 | from telegram import Bot 3 | import configparser 4 | import logging 5 | from datetime import datetime, timedelta 6 | 7 | 8 | # Enable logging 9 | logging.basicConfig(filename='outlook.txt', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', 10 | level=logging.INFO) 11 | logger = logging.getLogger(__name__) 12 | logger.warning('restart') # log startup 13 | 14 | 15 | class Outlook: 16 | def __init__(self, pers_folder, update_time): 17 | # "6" refers to the index of a folder 18 | self.inbox = Dispatch("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(6) 19 | self.personal_folder = self.inbox.Folders(pers_folder) 20 | self.messages = self.personal_folder.Items 21 | self.time = update_time 22 | 23 | def last_messages(self, number): 24 | emails = [] 25 | if isinstance(self.time, str): 26 | self.time = float(self.time) 27 | for i in range(len(self.messages) - number, len(self.messages)): 28 | if self.messages[i].CreationTime.timestamp() > self.time: 29 | emails.append((self.messages[i].SenderName, self.messages[i].Subject)) 30 | self.time = self.messages[i].CreationTime.timestamp() 31 | return emails 32 | 33 | def updated(self): 34 | return str(self.time) 35 | 36 | 37 | class Telegram: 38 | def __init__(self): 39 | self.bot = Bot(token=bot_access_token) 40 | 41 | def send_message(self, msg_text): 42 | self.bot.sendMessage(chat_id=chat_id, text=msg_text) 43 | 44 | 45 | class Controller: 46 | pass 47 | 48 | 49 | if __name__ == '__main__': 50 | # load config from file 51 | config = configparser.ConfigParser() 52 | config.read('./config.ini') 53 | bot_access_token = config['Telegram']['access_token'] 54 | chat_id = config['Telegram']['chat_id'] 55 | outlook_folder = config['Outlook']['private'] 56 | # if last update is emtpy then we going to use the default value 57 | last_update = config['Outlook']['last_update'] or (datetime.now() - timedelta(1)).timestamp() 58 | 59 | # outlook 60 | outlook = Outlook(outlook_folder, last_update) 61 | mails = outlook.last_messages(3) 62 | text = ('\n'.join([str(m)[1:-2] for m in mails])) 63 | print(text) 64 | 65 | # update config 66 | config['Outlook']['last_update'] = outlook.updated() 67 | 68 | with open('./config.ini', 'w') as configfile: 69 | config.write(configfile) 70 | 71 | # telegram 72 | tele = Telegram() 73 | tele.send_message(text) 74 | 75 | --------------------------------------------------------------------------------