-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathios-reviews-bot.py
executable file
·108 lines (89 loc) · 2.66 KB
/
ios-reviews-bot.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
#!/usr/bin/env python3
import boto3
import json
import redis
import requests
import time
def getSSMparam(SSMparam):
client = boto3.client("ssm")
response = client.get_parameter(Name=SSMparam, WithDecryption=True)
return response["Parameter"]["Value"]
def slackSend(
SSMParameter,
SlackWebhook,
SlackChannel,
SlackUsername,
SlackEmoji,
reviewcontent,
author,
rating,
label,
version,
):
# Convert rating to emoji stars
stars = ""
for i in range(0, int(rating)):
stars = stars + ":star:"
# Set bar colour based on rating
barcolor = "good"
if int(rating) < 3:
barcolor = "danger"
elif int(rating) == 3:
barcolor = "warning"
payload = {
"channel": SlackChannel,
"username": SlackUsername,
"icon_emoji": SlackEmoji,
"attachments": [
{
"author_name": author,
"color": barcolor,
"title": label,
"text": reviewcontent + "\n\n" + stars + "\nVersion " + version,
}
],
}
response = requests.post(headers={"Content-Type": "application/json"})
def getreviews(endpoint):
req = requests.get(endpoint)
data = json.loads(req.text)
return data
def redisconnect(host, port, password):
return redis.Redis(host=host, port=port, password=password)
def handler(event, context):
RedisSSMParameter = "redispw-SSMparam"
redispw = getSSMparam(RedisSSMParameter)
r = redisconnect("redis-host", 12345, redispw)
SlackSSMParameter = "slackwebhook-SSMparam"
SlackChannel = "@denis.khoshaba"
SlackUsername = "iOS Reviews Bot"
SlackEmoji = ":iphone:"
SlackWebhook = getSSMparam(SlackSSMParameter)
# Itunes application Id
AppId = "XXXXXXXXXX"
endpoint = (
"https://itunes.apple.com/AU/rss/customerreviews/id="
+ AppId
+ "/sortBy=mostRecent/json"
)
data = getreviews(endpoint)
for review in data["feed"]["entry"][1:-1]:
if not r.get(review["id"]["label"]):
r.set(review["id"]["label"], str(int(time.time())))
reviewcontent = review["content"]["label"]
author = review["author"]["name"]["label"]
rating = review["im:rating"]["label"]
label = review["title"]["label"]
version = review["im:version"]["label"]
slackSend(
SSMParameter,
SlackWebhook,
SlackChannel,
SlackUsername,
SlackEmoji,
reviewcontent,
author,
rating,
label,
version,
)