| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import logging
- import requests
- import requests.exceptions
- import tts
- import ears
- import base
- _RECIPIENTS = {
- 'martin': 'martin@gerner.co',
- 'pointy': 'martin@pointy.com',
- 'georgina': 'georgina@pointy.com',
- }
- _MAILGUN_API_URL = 'https://api.mailgun.net/v3/mg.gerner.co'
- _MAILGUN_API_KEY = 'key-80245476ae35aa626d782556419280f9'
- _MAILGUN_SENDER_EMAIL = 'noreply@mg.gerner.co'
- def _send_by_sendgrid(recipient, subject, email_body):
- auth = ('api', _MAILGUN_API_KEY)
- body = {
- 'from': _MAILGUN_SENDER_EMAIL,
- 'to': recipient,
- 'subject': subject,
- 'text': email_body
- }
- resp = requests.post(_MAILGUN_API_URL + '/messages', data=body, auth=auth)
- if resp.status_code != 200:
- resp.raise_for_status()
- class Email(base.BaseFunction):
- def preferred_phrases(self):
- return _RECIPIENTS.keys()
- def _send(self, recipient):
- logging.info('email _send: %s', recipient)
- tts.tts(recipient)
- lines, terminator = ears.listen_until_terminated()
- if terminator == 'done':
- body = '\n'.join(lines)
- logging.info('Sending email: %s: %s', _RECIPIENTS[recipient], lines)
- try:
- _send_by_sendgrid(_RECIPIENTS[recipient], 'snowbody email', body)
- tts.tts('done')
- except requests.exceptions.HTTPError, e:
- tts.tts('error')
- logging.error(e)
- else:
- tts.tts('cancelled')
- def call(self, additional_tokens=None, **_):
- logging.info('email: %s', additional_tokens)
- for t in additional_tokens:
- t = t.lower()
- if t in _RECIPIENTS:
- self._send(t)
- return
- tts.tts('to whom?')
- recipient = ears.listen(pause_threshold=1, preferred_phrases=self.preferred_phrases())
- if recipient in _RECIPIENTS:
- self._send(recipient)
|