-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
389 lines (345 loc) · 16.6 KB
/
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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
""" This module implements Webex bot functionality. Imported from web.py """
import os
from flask import request, url_for
import webexteamssdk
from webexteamssdk import WebexTeamsAPI
from webexteamssdk.models.cards.card import AdaptiveCard
from webexteamssdk.models.cards.inputs import * # pylint: disable=wildcard-import,unused-wildcard-import
from webexteamssdk.models.cards.components import * # pylint: disable=wildcard-import,unused-wildcard-import
from webexteamssdk.models.cards.container import * # pylint: disable=wildcard-import,unused-wildcard-import
from webexteamssdk.models.cards.actions import * # pylint: disable=wildcard-import,unused-wildcard-import
from webexteamssdk.models.cards.options import * # pylint: disable=wildcard-import,unused-wildcard-import
import schedule
from param_store import (
getSharepointParams,
saveSharepointParams,
getWebexIntegrationToken
)
def init(webAppPublicUrl):
""" initializes this module, called immediately after importing """
global botApi
# initialize Webex Teams bot control object
try:
botApi = WebexTeamsAPI(os.getenv("WEBEX_BOT_TOKEN"), wait_on_rate_limit=False) # this will not raise an exception, even if bot token isn't correct, so,
# need to make an API call to check if API is functional
assert botApi.people.me()
except Exception:
print("Could not initialize Webex bot API object.")
raise SystemExit()
webhookTargetUrl = webAppPublicUrl + "/webhook"
# delete ALL "Sharepoint-Webex bot..." current webhooks - this bot is supposed to be used only with one instance of the app
try:
for wh in botApi.webhooks.list():
if wh.name.startswith("Sharepoint-Webex bot"):
botApi.webhooks.delete(wh.id)
except Exception:
print("Could not clean up Webex bot API webhooks.")
raise SystemExit()
# create new webhooks
try:
botApi.webhooks.create(
name="Sharepoint-Webex bot - attachmentActions",
targetUrl=webhookTargetUrl,
resource="attachmentActions",
event="created",
filter="roomId=" + os.getenv("WEBEX_BOT_ROOM_ID")
)
except Exception:
print("Could not create a Webex bot API webhook.")
try:
botApi.webhooks.create(
name="Sharepoint-Webex bot - messages",
targetUrl=webhookTargetUrl,
resource="messages",
event="created",
filter="roomId=" + os.getenv("WEBEX_BOT_ROOM_ID")
)
except Exception:
print("Could not create a Webex bot API webhook.")
# @application.route("/webhook", methods=['GET', 'POST'])
def webhook():
""" Handles all incoming Webex webhooks """
# print ("Webhook arrived.")
# print(request)
webhookJson = request.json
# check if the received webhook is properly formed and relevant
try:
assert webhookJson['resource'] in ("messages", "attachmentActions")
assert webhookJson['event'] == "created"
assert webhookJson['data']['roomId'] == os.getenv("WEBEX_BOT_ROOM_ID")
except Exception:
print("The arrived webhook is malformed or does not indicate an actionable event in the log and control room")
return "Webhook processed."
# will need our own name
me = botApi.people.me()
# static adaptive card - greeting
greetingCard = AdaptiveCard(
fallbackText=f"Hi, I am {me.nickName}, I automatically create Webex Webinar sessions based on information in a Sharepoint Lists folder. Adaptive cards feature is required to use me.",
body=[
TextBlock(
text="Sharepoint to Webex Webinar automation",
weight=FontWeight.BOLDER,
size=FontSize.MEDIUM,
),
TextBlock(
text=f"Hi, I am {me.nickName}, I automatically create Webex Webinar sessions based on information in a Sharepoint Lists folder.",
wrap=True,
)
],
actions=[
Submit(title="Schedule now", data={'act': "schedule now"}),
Submit(title="Set Sharepoint", data={'act': "set sharepoint"}),
Submit(title="Authorize Webex", data={'act': "authorize webex"}),
Submit(title="?", data={'act': "help"}),
]
)
# if webhook indicates a message sent by us (the bot itself), ignore it
if webhookJson['data']['personId'] == me.id:
return "Webhook processed."
# received a text message
if webhookJson['resource'] == "messages":
# retrieve the new message details
# message = botApi.messages.get(webhookJson['data']['id'])
# print(message)
# respond with the greeting card to any message
botApi.messages.create(
text=greetingCard.fallbackText,
roomId=os.getenv("WEBEX_BOT_ROOM_ID"),
attachments=[greetingCard]
)
# received a card action
elif webhookJson['resource'] == "attachmentActions":
# retrieve the new attachment action details
action = botApi.attachment_actions.get(webhookJson['data']['id'])
# print("Action:\n", action)
# print("actionInputs", actionInputs)
# "?" (help) action
if action.type == "submit" and action.inputs['act'] == "help":
botApi.messages.create(
markdown="""
Sharepoint and Webex Automation creates webinars in Webex Webinar based on information in Sharepoint Lists.
It is easy to use:
1. Collaborate with your team on webinar planning in a Sharepoint List. Use one folder per webinar series, one list item per webinar. When ready for creation, check **Create**.
2. Click **Schedule Now** button to start webinar scheduling process.
3. Webinars are created.
Features and basic usage: https://github.com/zhenyamorozov/sharepoint-webex#tbd
How to set up and get started: https://github.com/zhenyamorozov/sharepoint-webex/blob/master/docs/get_started.rst#tbd
""",
roomId=os.getenv("WEBEX_BOT_ROOM_ID")
)
# resend greeting card
botApi.messages.create(
text=greetingCard.fallbackText,
roomId=os.getenv("WEBEX_BOT_ROOM_ID"),
attachments=[greetingCard]
)
# "Schedule now" action
if action.type == "submit" and action.inputs['act'] == "schedule now":
try:
actor = botApi.people.get(personId=webhookJson['actorId'])
botApi.messages.create(
markdown=f"Webinar scheduling requested by <@personId:{actor.id}|{actor.firstName}>. Will start the process. It will take a minute.",
roomId=os.getenv("WEBEX_BOT_ROOM_ID")
)
except Exception:
botApi.messages.create(
markdown="Webinar scheduling requested. Will start the process. It will take a minute.",
roomId=os.getenv("WEBEX_BOT_ROOM_ID")
)
# invoke the webinar scheduling process
schedule.run()
# send reduced greeting card - only action buttons
botApi.messages.create(
text=greetingCard.fallbackText,
roomId=os.getenv("WEBEX_BOT_ROOM_ID"),
attachments=[AdaptiveCard(fallbackText=greetingCard.fallbackText, actions=greetingCard.actions)]
)
# "Set Sharepoint" action
if action.type == "submit" and action.inputs['act'] == "set sharepoint":
try:
# load current Sharepoint parameters from parameter store
spSiteURL, spListName, spFolderName = getSharepointParams()
except Exception:
spSiteURL = spListName = spFolderName = ""
card = AdaptiveCard(
fallbackText="Adaptive cards feature is required to use me.",
body=[
TextBlock(
text="Sharepoint setting",
weight=FontWeight.BOLDER,
size=FontSize.MEDIUM,
),
TextBlock(
text="Information for scheduled webinars is taken from a Sharepoint Lists folder. Here you can change the Sharepoint Site URL, the name of the List, and the name of the list folder.",
wrap=True,
),
FactSet(
facts=[
Fact(
title="Sharepoint site URL",
value=spSiteURL if spSiteURL else "_none_"
),
Fact(
title="List name",
value=spListName if spListName else "_none_"
),
Fact(
title="Folder name",
value=spFolderName if spFolderName else "_none_"
),
]
)
],
actions=[
ShowCard(
title="Change",
card=AdaptiveCard(
body=[
TextBlock(
text="Change the Sharepoint Lists parameters here. The list should belong to the site. The folder must be located in the root of the list. Nested folders are not supported.",
wrap=True,
),
Text('spSiteURL', value=spSiteURL, placeholder="New Sharepoint site URL", isMultiline=False),
Text('spListName', value=spListName, placeholder="New Sharepoint List name", isMultiline=False),
Text('spFolderName', value=spFolderName, placeholder="New Sharepoint Lists folder name", isMultiline=False),
],
actions=[
Submit(title="OK", data={'act': "save sharepoint"}),
]
)
),
# no need for template support
# Submit(title="Create Template", data={'act': "create sharepoint template"})
]
)
# print(card.to_json())
botApi.messages.create(
text="Could not send the action card",
roomId=os.getenv("WEBEX_BOT_ROOM_ID"),
attachments=[card]
)
# "Save Sharepoint" action
if action.type == "submit" and action.inputs['act'] == "save sharepoint":
# print(action)
if 'spSiteURL' not in action.inputs or not action.inputs['spSiteURL'].strip():
botApi.messages.create(
text="Sharepoint site URL cannot be empty.",
roomId=os.getenv("WEBEX_BOT_ROOM_ID")
)
elif 'spListName' not in action.inputs or not action.inputs['spListName'].strip():
botApi.messages.create(
text="Sharepoint List name cannot be empty.",
roomId=os.getenv("WEBEX_BOT_ROOM_ID")
)
elif 'spFolderName' not in action.inputs or not action.inputs['spFolderName'].strip():
botApi.messages.create(
text="Sharepoint Lists folder name cannot be empty.",
roomId=os.getenv("WEBEX_BOT_ROOM_ID")
)
else:
try:
spSiteURL = action.inputs['spSiteURL'].strip()
spListName = action.inputs['spListName'].strip()
spFolderName = action.inputs['spFolderName'].strip()
# TODO check if any verification of the provided parameters is needed before saving
try:
saveSharepointParams(spSiteURL, spListName, spFolderName)
# send confirmation message
botApi.messages.create(
markdown=f"New Sharepoint parameters are set:\nSite URL: ``{spSiteURL}``\nList name: ``{spListName}``\nFolder name: ``{spFolderName}``",
roomId=os.getenv("WEBEX_BOT_ROOM_ID")
)
# resend greeting card
botApi.messages.create(
text=greetingCard.fallbackText,
roomId=os.getenv("WEBEX_BOT_ROOM_ID"),
attachments=[greetingCard]
)
except Exception:
botApi.messages.create(
text="Could not save new Sharepoint parameters to Parameter Store. Check local AWS configuration.",
roomId=os.getenv("WEBEX_BOT_ROOM_ID")
)
except Exception:
botApi.messages.create(
text="That Sharepoint Lists folder name did not work. Try again.",
roomId=os.getenv("WEBEX_BOT_ROOM_ID")
)
# "Authorize Webex" action
if action.type == "submit" and action.inputs['act'] == "authorize webex":
try:
# get a fresh Webex Integration access token
access_token = getWebexIntegrationToken(
webex_integration_client_id=os.getenv("WEBEX_INTEGRATION_CLIENT_ID"),
webex_integration_client_secret=os.getenv("WEBEX_INTEGRATION_CLIENT_SECRET")
)
# get information about the current authorized Webex user
webexApi = webexteamssdk.WebexTeamsAPI(access_token)
webexMe = webexApi.people.me()
webexEmail = webexMe.emails[0]
webexDisplayName = webexMe.displayName
except Exception:
# haven't been authorized yet
webexEmail = ""
webexDisplayName = "Not authorized yet"
if os.getenv("FLASK_ENV") == "development":
# dev
authUrl = "http://localhost:5000" + "/auth"
else:
# prod
authUrl = url_for("auth", _external=True)
card = AdaptiveCard(
fallbackText="Adaptive cards feature is required to use me.",
body=[
TextBlock(
text="Webex integration authorization",
weight=FontWeight.BOLDER,
size=FontSize.MEDIUM,
),
TextBlock(
text="Webex integration is used to create Webex Webinar sessions. This is the user currently authorized to create sessions. You can update authorization here.",
wrap=True,
),
FactSet(
facts=[
Fact(
title="Name",
value=webexDisplayName
),
Fact(
title="Email",
value=webexEmail
)
]
)
],
actions=[
ShowCard(
title="Change",
card=AdaptiveCard(
body=[
TextBlock(
text="Click the button below and complete authorization process in your browser.",
wrap=True,
),
],
actions=[
OpenUrl(title="Authorize", url=authUrl),
ShowCard(
title="Copy URL",
card=AdaptiveCard(
body=[
TextBlock(
text=authUrl,
wrap=True,
),
],
)
)
]
)
)
]
) # /AdaptiveCard
botApi.messages.create(text="Could not send the action card", roomId=os.getenv("WEBEX_BOT_ROOM_ID"), attachments=[card])
return "webhook accepted"