/
index.html
482 lines (436 loc) · 18.5 KB
/
index.html
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Practical Pedestal/Swagger: Slack integration</title>
<meta name="description" content="How the combination of Pedestal, Swagger and other useful patterns allowed the low friction implementation of a RESTful extension for the messaging service Slack to provide Memes-as-a-Service">
<meta name="author" content="Oliver Hine">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<link rel="stylesheet" href="css/slacky-pres.css">
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h2>Practical Pedestal/Swagger: Slack integration</h2>
<img style="max-height: 250px" src="images/doge-patterns-useful.jpg"/>
<p style="margin-top: 0;">
<a class="slacky-link" target="_blank" href="https://slacky-server.herokuapp.com/">
<img class="logo" src="images/slacky-icon.png"/>
slacky-server.herokuapp.com
</a>
<br/>
<a class="github" target="_blank" href="https://github.com/oliyh/slacky">
<img class="logo" src="images/github.png"/>
oliyh/slacky
</a>
</p>
<p>
<small>
Created by <a target="_blank" href="http://oliy.co.uk">Oliver Hine</a> / <a target="_blank" href="http://twitter.com/oliyh">@oliyh</a>
</small>
</p>
<aside class="notes">
<ul>
<li>Oliver Hine</li>
<li>JUXTer</li>
<li>Background in banks, OnTheMarket</li>
<li>Patterns for keeping things simple in a small REST service</li>
</ul>
</aside>
</section>
<section>
<h2>Slack</h2>
<p>
A developer-friendly chat service
</p>
<img class="stretch" src="images/slack-chat.png"/>
<aside class="notes">
<ul>
<li>Introduce Slack</li>
<li>Developer-friendly chat service</li>
<li>Can do everything with a keyboard</li>
<li>Inline code, images, videos, link previews</li>
<li>API for extensions</li>
<li>Many extensions already exist for Twitter, Github etc but one missing...</li>
</ul>
</aside>
</section>
<section>
<h2>Memes!</h2>
<p>
<em>"an idea, behavior, style, or usage that spreads from person to person within a culture"</em>
</p>
<img src="images/simple-made-complex-meme.png"/>
<aside class="notes">
<ul>
<li>Previously in Hipchat had a bot which allowed inline meme generation</li>
<li>Moved to Slack for other reasons but found we missed the memes</li>
<li>Previous slide was top secret JUXT conversation about logging</li>
<li>Frankie wanted to remind us of Rich Hickey's catchphrase "Simple made complex"</li>
<li>So what did he do?</li>
</ul>
</aside>
</section>
<section>
<h2>The Slack "Slash" command</h2>
<img src="images/slack-slash-command.png"/>
<pre><code> POST
token=gIkuvaNzQIHg97ATvDxqgjtO
team_id=T0001
team_domain=juxt
channel_id=C2147483705
channel_name=general
user_id=U2147483697
user_name=frankie
command=/meme
text=rich hickey | simple | made complex</code></pre>
<span><a target="_blank" href="https://api.slack.com/slash-commands">api.slack.com/slash-commands</a></span>
<aside class="notes">
<ul>
<li>One type of extension is called a Slash command - like IRC</li>
<li>Captures command and sends to HTTP endpoint</li>
<li>Documentation tells you what it will look like</li>
<li>Want to implement this doc now - not all the bits in between (deserialisation, validation etc)</li>
</ul>
</aside>
</section>
<section>
<h2>pedestal-swagger</h2>
<p>
Deserialisation, coercion and related error handling
</p>
<div class="col2">
<div style="width: 43%">
<pre><code class="hljs" data-trim>
(s/defschema SlackRequest
{(req :token) s/Str
(req :team_id) s/Str
(req :team_domain) s/Str
(req :channel_id) s/Str
(req :channel_name) s/Str
(req :user_id) s/Str
(req :user_name) s/Str
(req :command) s/Str
(req :text) s/Str})</code></pre>
</div>
<div style="width: 57%">
<pre><code class="hljs" data-trim>
(swagger/defhandler slack-meme
{:summary "Responds asynchonously to
Slash commands from Slack"
:parameters {:formData SlackRequest}
:responses {200 {:schema MemeUrl}}}
[{:keys [form-params] :as request}]
(let [text (:text form-params)]
{:status 200
:body (meme/generate-meme text)))
</code></pre>
</div>
</div>
<a class="github" target="_blank" href="https://github.com/frankiesardo/pedestal-swagger">
<img class="logo" src="images/github.png"/>
frankiesardo/pedestal-swagger
</a>
<aside class="notes">
<ul>
<li>pedestal-swagger lets me do just that</li>
<li>Define schema of request and attach to a handler</li>
<li>pedestal-swagger does everything in between to ensure the Swagger contract is met</li>
<li>Handler only knows about happy path</li>
<li>Handler not complicated by error handling</li>
<li>Nicely documented in code</li>
<li>Saves 75% of code <em>(made that up)</em></li>
</ul>
</aside>
</section>
<section>
<h2>Routing and interceptors</h2>
<pre class="stretch"><code class="hljs" data-trim>
(swagger/defroutes api-routes
{:info {:title "Slacky"
:description "Memes and more for Slack"
:externalDocs {:description "Find out more"
:url "https://github.com/oliyh/slacky"}
:version "2.0"}
:tags [{:name "meme"
:description "All the memes!"}]}
[[["/api" ^:interceptors [(swagger/body-params)
bootstrap/json-body
(swagger/coerce-request)
(swagger/validate-response)]
["/slack"
["/meme" ^:interceptors [(annotate {:tags ["meme"]})]
{:post slack-meme}]]
["/swagger.json" {:get [(swagger/swagger-json)]}]
["/*resource" {:get [(swagger/swagger-ui)]}]]]])
</code></pre>
<aside class="notes">
<ul>
<li>Routing table</li>
<li>Batteries included interceptors that deal with unpleasant bits</li>
<li>What is all that metadata for?</li>
</ul>
</aside>
</section>
<section>
<h2>Swagger UI</h2>
<img class="stretch no-border" src="images/swagger-ui.png"/>
<aside class="notes">
<ul>
<li>Swagger UI</li>
<li>Driven by JSON schema</li>
<li>Brilliant for documenting APIs and trying them out</li>
<li>JSON schema also useful for offshore teams / 3rd parties to code against</li>
<li>Provided for free by pedestal-swagger</li>
</ul>
</aside>
</section>
<section>
<section data-transition="none">
<h2>Meme generation</h2>
<img class="stretch no-border" src="images/meme-handling-1.png"/>
<aside class="notes">
<ul>
<li>Quickly cover what we do with a meme request</li>
<li>Parse using regex</li>
<li>Send search term to google to find template image</li>
</ul>
</aside>
</section>
<section data-transition="none">
<h2>Meme generation</h2>
<img class="stretch no-border" src="images/meme-handling-2.png"/>
<aside class="notes">
<ul>
<li>Send image url to MemeCaptain which returns us a template id</li>
</ul>
</aside>
</section>
<section data-transition="none">
<h2>Meme generation</h2>
<img class="stretch no-border" src="images/meme-handling-3.png"/>
<aside class="notes">
<ul>
<li>Ask MemeCaptain to generate meme using template id, upper and lower text</li>
<li>Returns a url to meme</li>
<li>Complicated enough!</li>
</aside>
</section>
</section>
<section>
<h2>Responding to Slack</h2>
<img class="stretch no-border" src="images/authentication-requirement.png"/>
<span><a target="_blank" href="https://api.slack.com/incoming-webhooks">api.slack.com/incoming-webhooks</a></span>
<aside class="notes">
<ul>
<li>Need a webhook url to send a message to a particular channel/team in Slack</li>
<li>Can map the unique token in Slash command post to a webhook url for response</li>
<li>We'll need something to deal with matching the token to the right webhook url</li>
</ul>
</aside>
</section>
<section>
<h2>Authentication interceptor</h2>
<pre class="stretch"><code class="hljs" data-trim>
(swagger/defbefore authenticate-slack-call
{:description "Ensures caller has registered an incoming webhook"
:parameters {:formData {:token s/Str}}
:responses {403 {}}}
[{:keys [request response] :as context}]
(let [db (:db-connection request)
token (get-in request [:form-params :token])
account (accounts/lookup-slack-account db token)]
(if-let [webhook-url (:key account)]
(update context :request merge {::slack-webhook-url webhook-url
::account-id (:id account)})
(-> (terminate context)
(assoc :response
{:status 403
:headers {}
:body (str "You are not permitted to use this service.\n"
"Please register your token '" token
"'at https://slacky-server.herokuapp.com")})))))
</code></pre>
<aside class="notes">
<ul>
<li>I need to either return a 403 unauthorised or provide the webhook url to my handler</li>
<li>I don't want to pollute my handler with this logic</li>
<li>This interceptor handles that flow completely and keeps my handler agnostic of databases, tokens etc</li>
<li>It all gets merged into the Swagger definition too so clients know a 403 might be returned</li>
</ul>
</aside>
</section>
<section>
<h2>Self-migrating database</h2>
<p>
Ensure database is migrated before use
</p>
<pre><code class="hljs" data-trim>
(defn- migrate-db [url]
(joplin/migrate-db
{:db {:type :jdbc :url url}
:migrator "migrators/jdbc"}))
(defn create-db-connection [url]
(migrate-db url)
(condp re-find url
#":postgresql:" (pool "org.postgresql.Driver" url))
#":h2:" (pool "org.h2.Driver" url))</code></pre>
<a class="github" href="https://github.com/juxt/joplin">
<img class="logo" src="images/github.png"/>
juxt/joplin
</a>
<aside class="notes">
<ul>
<li>So now I need a database, with at least some tables with names and columns that match my code</li>
<li>Sometimes for a new feature the tables and columns have to change, how do I keep my database and the code in sync?</li>
<li>Database migrations, run idempotently and handled by Joplin</li>
<li>Run them every time we connect to the database</li>
<li>No defensive code needed, no command line tools, release scripts etc</li>
<li>Code and database always in sync</li>
<li>Note code here deals with both H2 and postgres, one click clone and test</li>
<li>Joplin a JUXT library, used in production, handles many kinds of store inc ES, JDBC and more</li>
</ul>
</aside>
</section>
<section>
<h2>Extending to other services</h2>
<pre><code class="hljs" data-trim>
(defroutes routes
["/api" ^:interceptors [rate-limiter]
["/slack" ^:interceptors [slack-auth] ...]
["/hipchat" ^:interceptors [hipchat-auth] ...]])</code></pre>
<aside class="notes">
<ul>
<li>Consider extending Slacky to another chat service which authenticates a different way (hipchat-auth interceptor)</li>
<li>Consider also wanting a rate limiter which applies to all users of the API</li>
<li>The rate-limiter interceptor needs an account-id to track how much each account uses the service</li>
<li>Pedestal will run this interceptor before the authentication interceptors</li>
<li>It complects scope with order of execution</li>
</ul>
</aside>
</section>
<section>
<h2>Angel interceptor</h2>
<p>
Decomplect interceptor scope from ordering
</p>
<pre><code class="hljs" data-trim>
(require '[angel.interceptor :as angel])
(defroutes routes
["/api" ^:interceptors [(angel/requires rate-limiter :account)]
["/slack" ^:interceptors
[(angel/provides slack-auth :account)] ...]
["/hipchat" ^:interceptors
[(angel/provides hipchat-auth :account)] ...]])</code></pre>
<pre class="fragment"><code class="hljs" data-trim>
(def service
(angel/satisfy
{:io.pedestal.http/routes routes}))</code></pre>
<a class="github" href="https://github.com/oliyh/angel-interceptor">
<img class="logo" src="images/github.png"/>
oliyh/angel-interceptor
</a>
<aside class="notes">
<ul>
<li>Declare relationships between interceptors</li>
<li>angel/satisfy will reorder them without affecting their scope</li>
<li>Decomplects scope from order of execution</li>
<li>New library available on my Github</li>
</ul>
</aside>
</section>
<section>
<h2>Slacky</h2>
<!-- <img class="stretch no-border" src="images/slacky-logo.png"/> -->
<video id="demo-video" class="stretch" controls>
<source data-src="video/slacky-demo.mp4" type="video/mp4">
</video>
<br/>
<a class="slacky-link" target="_blank" href="https://slacky-server.herokuapp.com/">
<img class="logo" src="images/slacky-icon.png"/>
slacky-server.herokuapp.com
</a>
<aside class="notes">
<ul>
<li>What do we have now?</li>
<li>Simple service, self-contained functions</li>
<li>Easily extendable without having to rewrite interceptors</li>
<li>One click dev environment</li>
<li>One click deploy - Heroku deployed directly from Github</li>
<li>Existing functionality - other templates, and register custom templates</li>
<li>Show them demo from Slack (show help function / custom templates - introduce David via meme?)</li>
<li>Show them website?</li>
</ul>
</aside>
</section>
<section>
<h2>Epic memes</h2>
<video class="stretch" controls>
<source src="video/epic-memes.mp4" type="video/mp4">
</video>
<br/>
<a class="slacky-link" target="_blank" href="https://slacky-server.herokuapp.com/">
<img class="logo" src="images/slacky-icon.png"/>
slacky-server.herokuapp.com
</a>
<br/>
<a class="github" target="_blank" href="https://github.com/oliyh/slacky">
<img class="logo" src="images/github.png"/>
oliyh/slacky
</a>
<aside class="notes">
<ul>
<li>What's next?</li>
<li>Custom template rollout</li>
<li>Browser plugins on the way - should be easily incorporated</li>
<li>Available as a service - register your Slack at this link</li>
<li>Available on GitHub</li>
<li>TV advertising - endorsements from Jean-Claude and Enya</li>
</ul>
</aside>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>