-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslack_msg.py
291 lines (266 loc) · 10.1 KB
/
slack_msg.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import copy
import django
import os
from slackclient import SlackClient
# TODO double check that you need this setup step when sending tweets from inside streambot.py
os.environ["DJANGO_SETTINGS_MODULE"] = "openchat.settings"
django.setup()
import openspaces.secrets as s
from openspaces.bot_utils import slack_utils
from openspaces.models import OutgoingTweet
slack_client = SlackClient(s.BOT_TOKEN)
attachments_json_drop_down = {
"text": "Tweet body here",
"fallback": "If you could read this message, you'd be choosing something fun to do right now.",
"color": "#3AA3E3",
"attachment_type": "default",
"title": "User name here: fake-user",
"callback_id": "message_type|12345",
"actions": [
{
"name": "slack_date",
"text": "Choose date",
"type": "select",
"option_groups": [
{
"text": "Event date",
"options": [
{
"text": "5/11",
"value": "5/11"
},
{
"text": "5/12",
"value": "5/12"
},
{
"text": "5/13",
"value": "5/13"
}
]
}
]
},
{
"name": "slack_time",
"text": "Choose event time",
"type": "select",
"option_groups": [
{
"text": "Time of event",
"options": [
{
"text": "08:00",
"value": "08:00"
},
{
"text": "09:00",
"value": "09:00"
},
{
"text": "10:00",
"value": "10:00"
},
{
"text": "11:00",
"value": "11:00"
},
{
"text": "12:00",
"value": "12:00"
},
{
"text": "13:00",
"value": "13:00"
},
{
"text": "14:00",
"value": "14:00"
},
{
"text": "15:00",
"value": "15:00"
},
{
"text": "16:00",
"value": "16:00"
},
{
"text": "17:00",
"value": "17:00"
},
{
"text": "18:00",
"value": "18:00"
}
]
}
]
},
{
"name": "slack_room",
"text": "Choose room",
"type": "select",
"option_groups": [
{
"text": "Event room",
"options": [
{
"text": "Room 09",
"value": "09"
},
{
"text": "Room 10",
"value": "10"
},
{
"text": "Room 14",
"value": "14"
},
{
"text": "Room 15",
"value": "15"
},
{
"text": "Room 16",
"value": "16"
},
{
"text": "Room 19",
"value": "19"
},
{
"text": "Room 20",
"value": "20"
},
{
"text": "Room 21",
"value": "21"
},
{
"text": "Room 22",
"value": "22"
}
]
}
]
},
{
"name": "slack_approved",
"text": "Needs approval",
"type": "select",
"option_groups": [
{
"text": "Approve/deny tweet",
"options": [
{
"text": "Approve",
"value": "Approve"
},
{
"text": "Deny",
"value": "Deny"
}
]
}
]
},
{
"name": "slack_submit",
"text": "Submit",
"type": "button",
"style": "primary",
"value": "submit"
}
]
}
def send_slack_message(**kwargs):
"""
Take a tweet record and set the default values in message to
those in the tweet and send message to slack
"""
# make a copy of the default body
message_body = copy.deepcopy(attachments_json_drop_down)
if kwargs["tweet_created"]:
# lookup dict to change values from model to text representation
approved_lookup = {0: "Needs approval",
1: "Approved",
2: "Denied"}
tweet_instance = OutgoingTweet.objects.get(tweet_id=kwargs["tweet_id"])
event_time = tweet_instance.event_obj.start
date_val, time_val = slack_utils.utc_datetime_to_button_format(event_time)
room_val = tweet_instance.event_obj.location
approved_val = approved_lookup[tweet_instance.approved]
else:
date_val = None
time_val = None
room_val = None
approved_val = None
# set action message and icon in slack message
if approved_val is not None:
if approved_val == approved_lookup[1]:
message_body["footer"] = "auto approved by OpenSpacesBot"
message_body["footer_icon"] = "https://emojipedia-us.s3.amazonaws.com/thumbs/120/microsoft/106/heavy-check-mark_2714.png"
elif approved_val == approved_lookup[0]:
message_body["footer"] = "Tweet needs approval"
message_body["footer_icon"] = "https://d1nhio0ox7pgb.cloudfront.net/_img/g_collection_png/standard/256x256/eyeglasses.png"
else:
print("missing valid value for approval")
else:
message_body["footer"] = "Tweet needs information in order to be approved"
message_body["footer_icon"] = "https://d1nhio0ox7pgb.cloudfront.net/_img/g_collection_png/standard/256x256/eyeglasses.png"
screen_name = kwargs["screen_name"]
user_id = kwargs["user_id"]
message_body["title"] = f"tweet from: {screen_name}, twitter id: {user_id}"
message_body["text"] = kwargs["tweet"]
message_body["callback_id"] = "incoming_tweet|{}|{}".format(kwargs["tweet_id"], kwargs["screen_name"])
message_body["actions"][0]["text"] = date_val if date_val is not None else "Choose date"
message_body["actions"][1]["text"] = time_val if time_val is not None else "Choose time"
message_body["actions"][2]["text"] = room_val if room_val is not None else "Choose room"
message_body["actions"][3]["text"] = approved_val if approved_val is not None else "Needs approval"
try:
channel = kwargs["channel"]
except KeyError:
# defaults to outgoing-tweets channel if not conflict
channel = "outgoing-tweets"
if channel == "conflict":
channel_id = "CAKSUH0RG"
else:
# id for outgoing-tweets channel
channel_id = "CAKSJ1PLG"
slack_client.api_call("chat.postMessage",
channel=channel_id,
text=kwargs["slack_msg"],
attachments=[message_body])
return
if __name__ == '__main__':
import pytz
import datetime
from openspaces.bot_utils import db_utils
from random import randint
user_name = "user 1"
user_id="123456789"
tweet_id = str(randint(0, 1000000))
screen_name = "screen_name1"
tweet = "April 1st tweet room C123"
def create_sample_tweet(tweet, tweet_id, screen_name):
"""helper func to create a tweet instance in db for manual testing"""
event_obj = db_utils.create_event(description="fake event description",
start=datetime.datetime(2018, 5, 10, 1, tzinfo=pytz.utc),
location="A123",
creator="5/29 tweeter")
tweet_instance = db_utils.save_outgoing_tweet(tweet_id=tweet_id,
tweet=tweet, # for testing this is ok, in reality it will be the embeded tweet
original_tweet=tweet,
screen_name=screen_name,
approved=1,
event_obj=event_obj)
tweet_created = False
if tweet_created:
create_sample_tweet(tweet, tweet_id, screen_name)
send_slack_message(user_id=user_id,
tweet_id=tweet_id,
screen_name=screen_name,
tweet_created=tweet_created,
tweet=tweet,
slack_msg=tweet)