-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgmail_checker.py
56 lines (39 loc) · 1.68 KB
/
gmail_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
import logging
import os
from simplegmail import Gmail
from email.header import decode_header
from kalliope.core.NeuronModule import NeuronModule, MissingParameterException
logging.basicConfig()
logger = logging.getLogger("kalliope")
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
class Gmail_checker(NeuronModule):
def __init__(self, **kwargs):
super(Gmail_checker, self).__init__(**kwargs)
self.client_secret_file = kwargs.get('client_secret_file', None)
# check if parameters have been provided
if self._is_parameters_ok():
# prepare a returned dict
returned_dict = dict()
gmail = Gmail(client_secret_file=self.client_secret_file)
# Unread messages in your inbox
unread = gmail.get_unread_inbox()
returned_dict["unread"] = len(unread)
if len(unread) > 0:
# add a list of subject
subject_list = list()
for email in unread:
subject = email.subject
subject_list.append(subject)
returned_dict["subjects"] = subject_list
logger.debug("gmail neuron returned dict: %s" % str(returned_dict))
self.say(returned_dict)
def _is_parameters_ok(self):
"""
Check if received parameters are ok to perform operations in the neuron
:return: true if parameters are ok, raise an exception otherwise
.. raises:: MissingParameterException
"""
if self.client_secret_file is None:
raise MissingParameterException("client_secret_file parameter required")
return True