This repository was archived by the owner on Aug 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
116 lines (94 loc) · 3.2 KB
/
Copy pathhandler.py
File metadata and controls
116 lines (94 loc) · 3.2 KB
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
import hashlib
import hmac
import json
import os
from botocore.vendored import requests
def lambda_handler(event, context):
post_headers = {
"Authorization": "Bearer {}".format(os.environ['FB_API_TOKEN']),
}
project_groups = os.environ.get("PROJECT_GROUP_IDS")
if project_groups:
project_groups = json.loads(project_groups)
headers = event["headers"]
body = event["body"]
computed_signature = hmac.new(
bytes(os.environ["CLUBHOUSE_WEBHOOK_SECRET"], "UTF-8"),
bytes(body, "UTF-8"),
hashlib.sha256,
).hexdigest()
signature = headers.get("Clubhouse-Signature")
if computed_signature != signature:
print("Invalid signature: ", computed_signature, signature)
return {"statusCode": 400, "body": "Invalid signature"}
body = json.loads(body)
print(body)
msg = None
story_id = None
title = None
url = None
# Get the story id, title, and url.
for action in body.get("actions"):
if action.get("entity_type") == "story":
story_id = action.get("id")
title = action.get("name")
url = action.get("app_url")
if not story_id:
return {"statusCode": 200, "body": "No story reference found."}
member_name = requests.get(
"https://api.clubhouse.io/api/v2/members/{}?token={}".format(
body.get("member_id"),
os.environ["CLUBHOUSE_API_TOKEN"],
),
).json().get('profile', {}).get('mention_name')
project_id = requests.get(
"https://api.clubhouse.io/api/v2/stories/{}?token={}".format(
story_id,
os.environ["CLUBHOUSE_API_TOKEN"],
),
).json().get('project_id')
project_name = None
if project_id:
project_name = requests.get(
"https://api.clubhouse.io/api/v2/projects/{}?token={}".format(
project_id,
os.environ["CLUBHOUSE_API_TOKEN"],
),
).json().get('name')
group_id = project_groups.get(project_name, os.environ['FB_GROUP_ID'])
for action in body.get("actions"):
a = action.get("action")
if a != "create":
print("Warning: Only create actions supported: ", action)
continue
if action.get("entity_type") == "story-comment":
verb = "commented on a story"
text = action.get("text")
elif action.get("entity_type") == "story":
verb = "requested a new story"
text = action.get("description")
else:
print("Warning: Unsupported entity type: ", action)
continue
if project_name:
project = "[{}] ".format(project_name)
msg = "## {}**{}** {}:\n[[#{}] {}]({})\n>{}".format(
project,
member_name,
verb,
story_id,
title,
url,
text,
)
post_url = "https://graph.facebook.com/{}/feed".format(
group_id,
)
if msg:
data = {
'formatting': 'MARKDOWN',
'message': msg,
}
requests.post(post_url, headers=post_headers, data=data)
print("Posted to group!")
return {"statusCode": 200, "body": "Victory!"}