This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
156 lines (118 loc) · 3.89 KB
/
lambda_function.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import json
import requests
import urllib.parse
import re
# import boto3
from datetime import datetime
from pytz import timezone
AZURE_CLIENT_ID = os.environ.get('AZURE_CLIENT_ID')
AZURE_CLIENT_SECRET = os.environ.get('AZURE_CLIENT_SECRET')
AZURE_REFRESH_TOKEN = os.environ.get('AZURE_REFRESH_TOKEN')
BOOK_ID = os.environ.get('BOOK_ID')
AWS_TOKEN = os.environ.get('ACCESS_TOKEN')
AWS_TOKEN_SECRET = os.environ.get('ACCESS_TOKEN_SECRET')
AWS_S3_BUCKET_NAME = os.environ.get('AWS_S3_BUCKET_NAME')
redirect_uri = 'https://login.microsoftonline.com/common/oauth2/nativeclient'
scopes = 'User.Read Files.ReadWrite.All offline_access',
# s3 = boto3.resource('s3',
# aws_access_key_id=AWS_TOKEN,
# aws_secret_access_key=AWS_TOKEN_SECRET,
# )
def lambda_handler(event, context):
qs = urllib.parse.parse_qs(event['body-json'])
print(qs)
args = qs['text'][0].split(" ")
type = ""
spec_date = ""
spec_time = ""
for str in args:
if re.match("entry|exit", str):
type = str
elif re.match("[0-9]{2}\/[0-9]{2}\/[0-9]{2}", str):
spec_date = str
elif re.match("[0-9]{2}:[0-9]{2}", str):
spec_time = str
else:
print("")
user = qs['user_name'][0]
return update_work_sheet(type=type, spec_date=spec_date, spec_time=spec_time, user=user)
def get_access_token():
# tokens = read_token_csv()
# azure_refresh_token = tokens[1]
headers = {
'Accept' : 'application/json',
'Content-Type' : 'application/x-www-form-urlencoded'
}
payload = {
'client_id': AZURE_CLIENT_ID,
'client_secret': AZURE_CLIENT_SECRET,
'grant_type': 'refresh_token',
'refresh_token': AZURE_REFRESH_TOKEN,
'redirect_uri': redirect_uri,
'scope': scopes,
}
response = requests.post(
'https://login.microsoftonline.com/common/oauth2/v2.0/token',
headers = headers,
data = payload
)
json_obj = json.loads(response.text)
return json_obj["access_token"]
def update_work_sheet(type, spec_date=None, spec_time=None, user="someone"):
access_token = get_access_token()
headers = {
'Content-Type' : 'application/json',
'authorization' : 'Bearer ' + access_token,
}
JST = timezone('Asia/Tokyo')
now = datetime.now(JST)
if not spec_date:
date = now.strftime("%y/%m/%d")
else:
date = spec_date
if not spec_time:
time = now.strftime("%H:%M")
else:
time = spec_time
sheet_name = date[0:5].replace("/", "_")
col = date[6:8]
row_time = "B"
row_user = "C"
if type == "exit":
row_time = "D"
row_user = "E"
payload = {
'values': [
[time, user]
]
}
print("stating update...")
response = requests.patch(
'https://graph.microsoft.com/v1.0/me/drive/items/' + BOOK_ID + "/workbook/worksheets('" + sheet_name + "')/range(address='" + sheet_name + "!" + row_time + col + ":" + row_user + col + "')",
headers = headers,
data = json.dumps(payload)
)
print(response.text)
message = {
'text': '[{0}] {1} {2}'.format(type, date, time),
'response_type': "ephemeral"
}
return message
# def read_token_csv():
# body = s3.Object(AWS_S3_BUCKET_NAME, 'azure_token.csv').get()['Body'].read().decode('utf-8')
# tokens = body.split(',')
# return tokens
def select_work_sheet():
access_token = get_access_token()
headers = {
'Content-Type' : 'application/json',
'authorization' : 'Bearer ' + access_token,
}
response = requests.get(
'https://graph.microsoft.com/v1.0/me/drive/items/' + BOOK_ID + "/workbook/worksheets('" + sheet_name + "')/usedRange",
headers = headers,
)
json_obj = json.loads(response.text)['text']
for date, time in json_obj:
print(date + " " + time)