This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
74 lines (55 loc) · 1.47 KB
/
handler.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
import boto3
import json
import random
import urllib.request
with open('./config.json') as f:
config = json.loads(f.read())
url = config['url']
members = config['members']
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('OrderTable')
def main(event=None, context=None):
member = get_member()
if not len(member):
save_order(members)
member = get_member()
delete_member(member)
msg = f"今日のふぁしり <@{member['name']}>"
post_slack(msg)
def save_order(members):
random.shuffle(members)
with table.batch_writer() as batch:
for i in range(len(members)):
batch.put_item(
Item={
'order': i+1,
'name': members[i],
}
)
def get_member():
res = table.scan()
items = res['Items']
if not len(items):
return {}
sorted_items = sorted(items, key=lambda i: i['order'])
return sorted_items[0]
def delete_member(member):
order = member['order']
table.delete_item(
Key={
'order': order
}
)
def post_slack(msg):
method = 'POST'
request_headers = {'Content-Type': 'application/json; charset=utf-8'}
body = json.dumps({'text': msg}).encode("utf-8")
request = urllib.request.Request(
url=url,
data=body,
method=method,
headers=request_headers
)
urllib.request.urlopen(request)
if __name__ == '__main__':
main()