email_source.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from dataclasses import dataclass, asdict
  2. from typing import Optional, List, Dict, Union
  3. from dacite import from_dict
  4. from email.utils import getaddresses, parseaddr
  5. import json
  6. import requests
  7. import messaging_support.types as hm_types
  8. class RmConversationContext:
  9. def add_routed_message (self, rm):
  10. references = eml.get('References')
  11. return
  12. class EmailConversationContext:
  13. def add_email (self, eml):
  14. # the first reference is the conversation starter
  15. # the last is the message this is a reply to
  16. # the middle are immediate replies, up to about 10 deep--
  17. # ie. some of the chain may be missing; we can get the oldest message
  18. # and recursively rebuild upward
  19. references = eml.get('References')
  20. return
  21. def addr_to_ra (addr):
  22. return {'name': addr[0], 'address': addr[1]}
  23. class EmailSource:
  24. def __init__ (self, host, user, password):
  25. self.host = host
  26. self.user = user
  27. self.password = password
  28. def get_page (self, page=0, limit=10):
  29. resp = requests.get(f'http://localhost:5010/data/emails/imap?page={page}')
  30. if resp.status_code != 200:
  31. raise Exception(f'error: {resp.status_code}')
  32. resp_json = json.loads(resp.text)
  33. print(resp_json)
  34. # check_types=False because the string "None" creates a problem...
  35. root_key = resp_json['metaData']['root']
  36. fields = resp_json['metaData']['fields']
  37. rows = resp_json[ root_key ]
  38. data = []
  39. for i, row in enumerate(rows):
  40. msg_id = 'imap-' + str(i + (limit * page))
  41. record = {'msg_id': msg_id, **{k: row[j] for j, k in enumerate(fields)}}
  42. record['conversation_id'] = msg_id
  43. record['to'] = list(map(addr_to_ra, getaddresses([record['to']])))
  44. record['from_'] = list(map(addr_to_ra, getaddresses([record['from']])))[0]
  45. del record['from']
  46. rm = from_dict(data=record, data_class=hm_types.RoutedMessage)
  47. data.append(rm)
  48. routed_messages = data
  49. return routed_messages
  50. def get_page2 (self, page=0, limit=10):
  51. token = json.dumps({
  52. 'host': self.host,
  53. 'user': self.user,
  54. 'password': self.password
  55. })
  56. headers = {
  57. 'Authorization': token
  58. }
  59. resp = requests.get(f'http://localhost:5010/data/emails/imap2?page={page}', headers=headers)
  60. if resp.status_code != 200:
  61. raise Exception(f'error: {resp.status_code}')
  62. resp_json = json.loads(resp.text)
  63. routed_messages = list(map(lambda rm_dict: from_dict(data=rm_dict, data_class=hm_types.RoutedMessage), resp_json))
  64. return routed_messages
  65. def send_message (self, to, subject, content):
  66. token = json.dumps({
  67. 'host': self.host,
  68. 'user': self.user,
  69. 'password': self.password
  70. })
  71. headers = {
  72. 'Authorization': token
  73. }
  74. form_data = {
  75. 'to': to,
  76. 'subject': subject,
  77. 'text': content
  78. }
  79. resp = requests.post('http://localhost:5010/api/send-message', data=form_data, headers=headers)
  80. return resp