Skip to content

Commit 23de500

Browse files
committed
Updated gmail with master
1 parent 5edc27a commit 23de500

File tree

9 files changed

+519
-16
lines changed

9 files changed

+519
-16
lines changed

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## Purpose
2+
This is a script that connects to Google's Gmail Service through its API. Collects a select set of emails. Then based on the contents of these emails. Sends LINE message Notifications. This could be modified to send messages to other services, Discord, Telegram and more.
3+
4+
## Package Installation
5+
6+
If you are not interested in using the line_notify module. Remove it before using pip.
7+
8+
```
9+
pip3 install -r requirements.txt
10+
```
11+
12+
## Create some required files.
13+
14+
### constants.py
15+
Give these constants slightly more memorable / understandable names.
16+
This file simplifies the management when processing emails from several different sources.
17+
```python
18+
FROM_1 = "local@domain"
19+
FROM_2 = "local@domain2"
20+
21+
SUB_1 = "Subject Line"
22+
SUB_2 = "Subject Line"
23+
24+
SUBJECTS = [SUB_1, SUB_2]
25+
```
26+
27+
### secrets.py
28+
This file is where you might keep some of your secrets. Though I would probably recommend that you move these to being environment variables instead.
29+
**Note**: You should have this file listed in your .gitignore file. It should not be uploaded to GitHub.
30+
31+
```python
32+
SEARCH_STRING = "GMail search String"
33+
34+
# Notifier laben ID
35+
LABEL_ID = "Gmail Label ID String"
36+
37+
LINE_TOKEN = "My Secret Token for LINE"
38+
39+
NAME = "A Name"
40+
```
41+
## Running the script
42+
If you found this code first on github, I would direct you to [this article series.](https://dev.to/basman/connecting-to-gmail-api-with-python-546b) This will walk you through setting things up with Google.
43+
44+
After going to the Google Quick start. You should have downloaded your `credentials.json` file. Run the gmail-sample.py
45+
### First Run
46+
This will confirm that you are connecting to gmail successfully. If you do not see a list of labels. Work through the google trouble shooting.
47+
48+
If successful delete the following code:
49+
```python
50+
# Stage 1 Check Connection
51+
list_labels_on_setup(service)
52+
sys.exit(0)
53+
# Delete from service = get_service() to this line
54+
```
55+
### Second Run
56+
In the code you will need to set the varible `name` to some label name you wish to use.
57+
Then run the script. This will report the label_id that google assigned to this new label. For simplicity, don't manually create the label.
58+
Add the label_id to the secrets.py file.
59+
60+
Delete the next section of code:
61+
```python
62+
# Stage 2 Create New Label
63+
if secrets.LABEL_ID == "":
64+
print("We are creating a new label.")
65+
# Replace "" with the label name you wish to use.
66+
name = ""
67+
if len(name) == 0:
68+
print("You have not yet set name See 'name = ""' in the code above")
69+
print("Do that now and run the script again")
70+
sys.exit(0)
71+
new_label = define_label(name)
72+
new_label = add_label_to_gmail(service, new_label, logger)
73+
print(f"Your new label ID is: {get_new_label_id(new_label)}")
74+
print("Set this in secrets.py")
75+
sys.exit(0)
76+
# Delete from service = get_service() to this line
77+
```
78+
79+
At this point you should have a working sample code.
80+
81+
## Customising the script.
82+
You will need to update the code in the following section:
83+
```python
84+
if notifier == "NOTE1":
85+
logger.debug("Note1")
86+
# Your custom code goes here. Bot / Line
87+
processed = True
88+
elif notifier == "NOTE2":
89+
logger.debug("Note2")
90+
# Your custom code goes here. Bot / Line
91+
processed = True
92+
```
93+
If you are confused. Take a look at gmail.py which is my current working implementation.
94+
95+
## Automating Script Execution
96+
You can either use good old Cron or systemd.
97+
I have included a copy of the two files for systemd which should be located in the `/etc/systemd/system/` directory on your system.
98+
You will need to modify the path details and the timers to your own needs.

constants.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
"""
4+
5+
6+
ENTER = "に入室しました"
7+
LEAVE = "に退室しました"
8+
9+
FROM_BUS = "[email protected]"
10+
FROM_KIDZDUO = "[email protected]"
11+
FROM_GATE = "キッズセキュリティ ミマモルメ <[email protected]>"
12+
13+
BUS_SUB = "エキッズ"
14+
KIDZDUO_SUB = "入退室情報のお知らせ"
15+
GATE_SUB = "【キッズセキュリティミマモルメ】通過のお知らせ"
16+
17+
SUBJECTS = [BUS_SUB, KIDZDUO_SUB, GATE_SUB]

0 commit comments

Comments
 (0)