-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWS-Lambda-Function_votd-slack-2.py
64 lines (58 loc) · 1.98 KB
/
AWS-Lambda-Function_votd-slack-2.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
import requests
import json
import os
import re
SLACK_TOKEN = os.environ['SLACK_TOKEN']
SLACK_CHANNEL = '#viz-of-the-day'
def cleanhtml(raw_html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
return cleantext
votd_url = "https://public.tableau.com/api/gallery?page=0&count=1&galleryType=viz-of-the-day"
response = requests.get(votd_url).json()
title = response["items"][0]["title"]
author = response["items"][0]["authorName"]
image_src = response["items"][0]["screenshot"]
url_path = response["items"][0]["gallerySlug"]
view_count = response["items"][0]["workbook"]["viewCount"]
num_favorites = response["items"][0]["workbook"]["numberOfFavorites"]
#need to clean the html out of the description field
description = response["items"][0]["description"]
description = cleanhtml(description)
description = description.replace("\n","")
blocks = [{
"type": "section",
"text": {
"type": "mrkdwn",
"text": '*' + title + '*\nby ' + author + '\n:eyes: ' + str(f'{view_count:,}') + ' views\n:star: ' + str(f'{num_favorites:,}') + ' favorites\n' + description
}
},
{
"type": "image",
"image_url": image_src,
"alt_text": "inspiration"
},
{
"type": "divider"
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "See the viz"
},
"url": 'https://public.tableau.com/gallery/' + url_path + '?referrer=slack'
}
]
}
]
def post_votd_to_slack(event, context):
return requests.post('https://slack.com/api/chat.postMessage', {
'token': SLACK_TOKEN,
'channel': SLACK_CHANNEL,
'blocks': json.dumps(blocks) if blocks else None,
'text': title
}).json()