-
Notifications
You must be signed in to change notification settings - Fork 117
/
slack-message-sender.el
609 lines (562 loc) · 35.6 KB
/
slack-message-sender.el
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
;;; slack-message-sender.el --- slack message concern message sending -*- lexical-binding: t; -*-
;; Copyright (C) 2015 yuya.minami
;; Author: yuya.minami <[email protected]>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'eieio)
(require 'json)
(require 'slack-util)
(require 'slack-room)
(require 'slack-im)
(require 'slack-group)
(require 'slack-message)
(require 'slack-channel)
(require 'slack-conversations)
(require 'slack-usergroup)
(require 'slack-mrkdwn)
(defvar slack-completing-read-function)
(defvar slack-buffer-function)
(defvar slack-current-buffer)
(defconst slack-channel-mention-regex "\\(<#\\([A-Za-z0-9]+\\)>\\)")
(defconst slack-user-mention-regex "\\(<@\\([A-Za-z0-9]+\\)>\\)")
(defconst slack-usergroup-mention-regex "\\(<!subteam^\\([A-Za-z0-9]+\\)>\\)")
(defconst slack-special-mention-regex "\\(<!\\(here\\|channel\\|everyone\\)>\\)")
(defconst slack-file-upload-complete-url "https://slack.com/api/files.completeUpload")
(cl-defun slack-message-send-internal (message room team &key (on-success nil) (on-error nil) (payload nil) (files nil))
(when (slack-string-blankp message)
(error "Empty message"))
(if (and (slack-channel-p room)
(not (oref room is-member)))
(slack-conversations-join
room team
#'(lambda (_data) (slack-message-send-internal message
room
team
:on-success on-success
:on-error on-error
:payload payload
:files files)))
(if files
(slack-message-upload-files team
files
:on-error on-error
:on-success #'(lambda (files) (let ((message-payload (append (apply #'list
(cons "channel" (oref room id))
(with-temp-buffer
(insert message)
(slack-create-blocks-from-buffer)))
payload)))
(slack-files-upload-complete team
files
message-payload
:on-success on-success
:on-error on-error))))
(let ((message-payload (append (apply #'list
(cons "type" "message")
(cons "channel" (oref room id))
(with-temp-buffer
(insert message)
(slack-create-blocks-from-buffer)))
payload)))
(slack-chat-post-message team
message-payload
:on-success on-success
:on-error on-error)))))
(cl-defun slack-chat-post-message (team message &key (on-success nil) (on-error nil))
(cl-labels
((success (&key data &allow-other-keys)
(if (eq t (plist-get data :ok))
(and on-success (funcall on-success))
(if on-error
(funcall on-error data)
(slack-log (format "Failed to post message. Error: %s, meta: %s"
(plist-get data :error)
(when (plist-get data :response_metadata)
(mapconcat 'identity
(plist-get (plist-get data :response_metadata)
:messages)
"\n")))
team
:level 'error)))))
(slack-request
(slack-request-create
"https://slack.com/api/chat.postMessage"
team
:type "POST"
:data (json-encode message)
:headers (list (cons "Content-Type"
"application/json;charset=utf-8"))
:success #'success))))
(defun slack-message-room-list (team)
(append (slack-group-names team)
(slack-im-names team)
(slack-channel-names team)))
(defun slack-message-embed-channel ()
(interactive)
(slack-if-let* ((buf slack-current-buffer)
(team (slack-buffer-team buf)))
(slack-select-from-list
((slack-channel-names team) "Select Channel: ")
(slack-insert-channel-mention (oref selected id)
(format "#%s" (slack-room-name selected team))))))
(defun slack-insert-channel-mention (channel-id display)
(insert (slack-propertize-mention-text 'slack-message-mention-face
display
(format "<#%s>" channel-id))))
(defun slack-insert-user-mention (user-id display)
(insert (slack-propertize-mention-text 'slack-message-mention-face
display
(format "<@%s>" user-id))))
(defun slack-insert-usergroup-mention (usergroup-id display)
(insert (slack-propertize-mention-text 'slack-message-mention-keyword-face
display
(format "<!subteam^%s>" usergroup-id))))
(defun slack-insert-keyword-mention (keyword display)
(insert (slack-propertize-mention-text 'slack-message-mention-keyword-face
display
(format "<!%s>" keyword))))
(defun slack-message-embed-mention ()
(interactive)
(slack-if-let* ((buf slack-current-buffer)
(team (slack-buffer-team buf)))
(let* ((keyworkds (list (list "here" :name "here" :type 'keyword)
(list "channel" :name "channel" :type 'keyword)
(list "everyone" :name "everyone" :type 'keyword)))
(usergroups (mapcar #'(lambda (e) (list (oref e handle)
:id (oref e id)
:name (oref e handle)
:type 'usergroup))
(cl-remove-if #'slack-usergroup-deleted-p
(oref team usergroups))))
(alist (append keyworkds (slack-user-names team) usergroups)))
(slack-select-from-list
(alist "Select User: ")
(cl-case (plist-get selected :type)
(keyword
(slack-insert-keyword-mention (plist-get selected :name)
(concat "@" (plist-get selected :name))))
(usergroup
(slack-insert-usergroup-mention (plist-get selected :id)
(concat "@" (plist-get selected :name))))
(t
(slack-insert-user-mention (plist-get selected :id)
(concat "@" (slack-user--name selected team)))))))))
(defvar slack-enable-wysiwyg)
(defun slack-enable-wysiwyg ()
(when slack-enable-wysiwyg
(add-hook 'after-change-functions
'slack-wysiwyg-after-change nil t)))
(defun slack-wysiwyg-enabled-p ()
(and slack-enable-wysiwyg
(or (eq 'slack-message-compose-buffer-mode
major-mode)
(eq 'slack-message-edit-buffer-mode
major-mode))))
(defun slack-wysiwyg-after-change (_beg _end _length)
(when (slack-wysiwyg-enabled-p)
(save-excursion
(save-restriction
(put-text-property (point-min) (point-max) 'face nil)
(put-text-property (point-min) (point-max) 'invisible nil)
(put-text-property (point-min) (point-max) 'slack-code-block-type nil)
(put-text-property (point-min) (point-max) 'display nil)
(remove-overlays (point-min) (point-max))
(slack-mrkdwn-add-face)
(mapc #'(lambda (regex)
(goto-char (point-min))
(while (re-search-forward regex (point-max) t)
(unless (slack-mrkdwn-inside-code-p (match-beginning 0))
(let* ((beg (match-beginning 0))
(end (match-end 0))
(props (get-text-property beg 'slack-mention-props)))
(when props
(let ((properties (append (plist-get props :props) nil)))
(while (< 0 (length properties))
(put-text-property beg end
(pop properties)
(pop properties)))))))))
(list slack-user-mention-regex
slack-usergroup-mention-regex
slack-channel-mention-regex
slack-special-mention-regex))))))
(defun slack-put-block-props (beg end value)
(put-text-property beg end 'slack-block-props value))
(defun slack-put-section-block-props (beg end value)
(put-text-property beg end 'slack-section-block-props value))
(defun slack-mark-bold ()
(goto-char (point-min))
(while (re-search-forward slack-mrkdwn-regex-bold (point-max) t)
(unless (slack-mark-inside-code-p (match-beginning 1))
(slack-put-block-props (match-beginning 1)
(match-end 4)
(list :type 'bold
:text (match-string 3))))))
(defun slack-mark-italic ()
(goto-char (point-min))
(while (re-search-forward slack-mrkdwn-regex-italic (point-max) t)
(unless (slack-mark-inside-code-p (match-beginning 1))
(slack-put-block-props (match-beginning 1)
(match-end 4)
(list :type 'italic
:text (match-string 3))))))
(defun slack-mark-strike ()
(goto-char (point-min))
(while (re-search-forward slack-mrkdwn-regex-strike (point-max) t)
(unless (slack-mark-inside-code-p (match-beginning 1))
(slack-put-block-props (match-beginning 1)
(match-end 4)
(list :type 'strike
:text (match-string 3))))))
(defun slack-mark-code ()
(goto-char (point-min))
(while (re-search-forward slack-mrkdwn-regex-code (point-max) t)
(unless (slack-mark-inside-code-p (match-beginning 1))
(slack-put-block-props (match-beginning 2)
(match-end 4)
(list :type 'code
:text (match-string 3))))))
(defun slack-mark-code-block ()
(goto-char (point-min))
(while (re-search-forward slack-mrkdwn-regex-code-block (point-max) t)
(slack-put-section-block-props (match-beginning 0)
(match-end 0)
(list :section-type 'code-block
:end (+ 3 (match-end 0))
:element-beg (match-beginning 2)
:element-end (match-end 2)))))
(defun slack-mark-blockquote ()
(goto-char (point-min))
(while (re-search-forward slack-mrkdwn-regex-blockquote (point-max) t)
(unless (slack-mark-inside-code-p (match-beginning 0))
(slack-put-section-block-props (match-beginning 0)
(match-end 0)
(list :section-type 'blockquote
:element-beg (match-beginning 3)
:element-end (match-end 3))))))
(defun slack-mark-list ()
(goto-char (point-min))
(while (re-search-forward slack-mrkdwn-regex-list (point-max) t)
(unless (slack-mark-inside-code-p (match-beginning 0))
(let* ((list-sign (match-string 2))
(list-style (if (or (string= "-" list-sign)
(string= "*" list-sign))
"bullet"
"ordered"))
(list-indent (length (match-string 1))))
(slack-put-section-block-props (match-beginning 0)
(match-end 0)
(list :section-type 'list
:style list-style
:indent list-indent
:element-beg (match-beginning 4)
:element-end (match-end 4)))))))
(defun slack-mark-mentions ()
(goto-char (point-min))
(while (re-search-forward slack-user-mention-regex (point-max) t)
(unless (slack-mark-inhibit-mention-p (match-beginning 1))
(slack-put-block-props (match-beginning 1)
(match-end 1)
(list :type 'user
:user-id (match-string 2)))))
(goto-char (point-min))
(while (re-search-forward slack-usergroup-mention-regex (point-max) t)
(unless (slack-mark-inhibit-mention-p (match-beginning 1))
(slack-put-block-props (match-beginning 1)
(match-end 1)
(list :type 'usergroup
:usergroup-id (match-string 2)))))
(goto-char (point-min))
(while (re-search-forward slack-channel-mention-regex (point-max) t)
(unless (slack-mark-inhibit-mention-p (match-beginning 1))
(slack-put-block-props (match-beginning 1)
(match-end 1)
(list :type 'channel
:channel-id (match-string 2)))))
(goto-char (point-min))
(while (re-search-forward slack-special-mention-regex (point-max) t)
(unless (slack-mark-inhibit-mention-p (match-beginning 1))
(slack-put-block-props (match-beginning 1)
(match-end 1)
(list :type 'broadcast
:range (match-string 2))))))
(defun slack-mark-emojis ()
(goto-char (point-min))
(while (re-search-forward ":\\([a-z0-9_-]+\\):" (point-max) t)
(unless (slack-mark-inside-code-p (match-beginning 0))
(slack-put-block-props (match-beginning 0)
(match-end 0)
(list :type 'emoji
:name (match-string 1))))))
(defun slack-mark-links ()
(goto-char (point-min))
(let ((regex (regexp-opt thing-at-point-uri-schemes)))
(while (re-search-forward regex (point-max) t)
(unless (slack-mark-inside-code-p (match-beginning 0))
(let ((bounds (bounds-of-thing-at-point 'url)))
(when bounds
(slack-put-block-props (car bounds)
(cdr bounds)
(list :type 'link
:url (buffer-substring-no-properties (car bounds)
(cdr bounds))))))))))
(defun slack-mark-inhibit-mention-p (point)
(or (slack-mark-inside-code-p point)
(slack-mark-inside-bold-p point)))
(defun slack-mark-inside-code-p (point)
(slack-if-let* ((props (or (get-text-property point 'slack-block-props)
(get-text-property point 'slack-section-block-props))))
(or (eq 'code (plist-get props :type))
(eq 'code-block (plist-get props :section-type)))))
(defun slack-mark-inside-bold-p (point)
(slack-if-let* ((props (get-text-property point 'slack-block-props)))
(eq 'bold (plist-get props :type))))
(defun slack-mark-rich-text-elements ()
(slack-mark-bold)
(slack-mark-italic)
(slack-mark-strike)
(slack-mark-code)
(slack-mark-mentions)
(slack-mark-emojis)
(slack-mark-links))
(defun slack-create-blocks-from-buffer ()
(interactive)
(with-current-buffer (current-buffer)
(slack-mark-rich-text-elements)
(slack-mark-code-block)
(slack-mark-blockquote)
(slack-mark-list)
(cl-labels ((with-ranges (ranges cb &optional before-mark)
(let ((str (mapconcat #'(lambda (range)
(buffer-substring-no-properties
(car range)
(cdr range)))
(reverse ranges)
"\n")))
(with-temp-buffer
(insert str)
(when before-mark
(funcall before-mark))
(slack-mark-rich-text-elements)
(funcall cb))))
(create-elements-from-ranges (ranges &optional before-mark)
(when (< 0 (length ranges))
(with-ranges ranges
#'(lambda () (create-elements (point-min)
(point-max)))
before-mark)))
(create-section-elements-from-ranges (ranges)
(when (< 0 (length ranges))
(with-ranges ranges
#'(lambda ()
(create-section-elements (point-min)
(point-max))))))
(create-section-elements (start end)
(let* ((cur-point start)
(elements nil)
(section-elements nil)
(preformatted-ranges nil)
(blockquote-ranges nil)
(list-style nil)
(list-indent nil)
(list-ranges nil))
(cl-labels ((commit-block (type block-elements &rest props)
(when (< 0 (length block-elements))
(let ((e (list (cons "type" type)
(cons "elements" block-elements))))
(dolist (prop props)
(push prop e))
(push e elements))))
(commit-section-block ()
(when (commit-block "rich_text_section"
(reverse section-elements))
(setq section-elements nil)))
(commit-preformatted-block ()
(when (commit-block "rich_text_preformatted"
(create-elements-from-ranges
preformatted-ranges
#'(lambda ()
(slack-put-section-block-props (point-min) (point-max)
(list :section-type 'code-block)))))
(setq preformatted-ranges nil)))
(commit-blockquote-block ()
(when (commit-block "rich_text_quote"
(create-elements-from-ranges
blockquote-ranges))
(setq blockquote-ranges nil)))
(commit-list-block ()
(when (commit-block "rich_text_list"
(cl-mapcan #'(lambda (range)
(create-section-elements-from-ranges
(list range)))
(reverse list-ranges))
(cons "style" list-style)
(cons "indent" list-indent))
(setq list-ranges nil)
(setq list-style nil)
(setq list-indent nil))))
(while (and cur-point (< cur-point end))
(let* ((block-props (get-text-property cur-point 'slack-section-block-props))
(section-type (and block-props (plist-get block-props :section-type)))
(end (or (next-single-property-change cur-point 'slack-section-block-props)
end)))
(cl-case section-type
(code-block (progn
(commit-section-block)
(commit-blockquote-block)
(commit-list-block)
(push (cons (plist-get block-props :element-beg)
(plist-get block-props :element-end))
preformatted-ranges)))
(blockquote (progn
(commit-section-block)
(commit-preformatted-block)
(commit-list-block)
(push (cons (plist-get block-props :element-beg)
(plist-get block-props :element-end))
blockquote-ranges)
;; Skip newline
(setq end (1+ end))
))
(list (progn
(commit-section-block)
(commit-preformatted-block)
(commit-blockquote-block)
(push (cons (plist-get block-props :element-beg)
(plist-get block-props :element-end))
list-ranges)
(setq list-style (plist-get block-props :style))
(setq list-indent (plist-get block-props :indent)))
;; Skip newline
(setq end (1+ end)))
(t (progn
(commit-preformatted-block)
(commit-blockquote-block)
(commit-list-block)
(dolist (e (create-elements cur-point end))
(push e section-elements)))))
(setq cur-point end)))
(commit-section-block)
(commit-preformatted-block)
(commit-blockquote-block)
(commit-list-block))
(reverse elements)))
(create-elements (start end)
(save-excursion
(save-restriction
(narrow-to-region start end)
(let* ((cur-point (point-min))
(elements nil))
(cl-labels ((create-text-element (text &optional style)
(cl-remove-if #'null
(list (cons "type" "text")
(cons "text" text)
(when style
(cons "style" style))))))
(while (and cur-point (< cur-point (point-max)))
(let* ((block-props (get-text-property cur-point 'slack-block-props))
(block-type (and block-props (plist-get block-props :type)))
(block-text (and block-props (plist-get block-props :text)))
(next-change-point (or (next-single-property-change cur-point 'slack-block-props)
(point-max)))
(element (progn
(cl-case block-type
(bold (create-text-element block-text (list (cons "bold" t))))
(italic (create-text-element block-text (list (cons "italic" t))))
(strike (create-text-element block-text (list (cons "strike" t))))
(code (create-text-element block-text (list (cons "code" t))))
(text (create-text-element block-text))
(user (list (cons "type" "user")
(cons "user_id" (plist-get block-props :user-id))))
(usergroup (list (cons "type" "usergroup")
(cons "usergroup_id" (plist-get block-props :usergroup-id))))
(channel (list (cons "type" "channel")
(cons "channel_id" (plist-get block-props :channel-id))))
(broadcast (list (cons "type" "broadcast")
(cons "range" (plist-get block-props :range))))
(emoji (list (cons "type" "emoji")
(cons "name" (plist-get block-props :name))))
(link (list (cons "type" "link")
(cons "url" (plist-get block-props :url))))
(t (create-text-element
(buffer-substring-no-properties cur-point
next-change-point)))))))
;; (message "props: %s, element: %s" block-props element)
(when element
(push element elements))
(let* ((n (min (or next-change-point end))))
;; (message "cur: %s, end: %s, %s" cur-point n (buffer-substring-no-properties cur-point n))
(setq cur-point n)))))
(reverse elements))))))
(let ((elements (create-section-elements (point-min) (point-max))))
(let ((blocks (list (cons "blocks" (list (list (cons "type" "rich_text")
(cons "elements" elements)))))))
;; (message "elements: %s, blocks: %s" elements blocks)
;; (let ((buf (get-buffer-create "emacs-slack blocks")))
;; (with-current-buffer buf
;; (delete-region (point-min) (point-max))
;; (insert (json-encode-list blocks))
;; (json-mode)
;; (json-pretty-print-buffer))
;; (switch-to-buffer-other-window buf))
blocks)))))
(cl-defun slack-message-upload-files (team files &key on-success on-error)
(let ((files-count (length files))
(result nil)
(timer nil)
(failed-p nil))
(cl-labels
((on-upload (success-p &optional file-id)
(if success-p
(push file-id result)
(setq failed-p t))))
(dolist (file files)
(slack-upload-file file team #'on-upload))
(setq timer (run-at-time t 1 #'(lambda ()
(slack-log (format "Uploading files... (%s/%s)" (length result) files-count)
team)
(when failed-p
(funcall on-error)
(cancel-timer timer))
(when (<= files-count (length result))
(funcall on-success files)
(cancel-timer timer))))))))
(cl-defun slack-files-upload-complete (team files message-payload &key (on-success nil) (on-error nil))
(cl-labels ((on-complete (&key data &allow-other-keys)
(slack-request-handle-error
(data "slack-files-upload-complete"
#'(lambda (err)
(slack-log (format "Failed to files upload complete. FILES: %s, ERROR: %s"
(mapcar #'(lambda (file) (oref file filename))
files)
err)
team)
(when (functionp on-error)
(funcall on-error))))
(when (functionp on-success)
(funcall on-success)))))
(slack-request
(slack-request-create
slack-file-upload-complete-url
team
:type "POST"
:data (json-encode (append (list (cons "files" (mapcar #'(lambda (file)
(list (cons "id" (oref file id))
(cons "title" (oref file filename))))
files)))
message-payload))
:headers (list (cons "Content-Type" "application/json;charset=utf-8"))
:success #'on-complete))))
(provide 'slack-message-sender)
;;; slack-message-sender.el ends here