about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/vendor/slack-snippets.el
blob: 6bf933cfb86d9a30358e8c6dc158dd2f3d353240 (plain) (blame)
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
;;; private/grfn/slack-snippets.el -*- lexical-binding: t; -*-

(require 's)
(require 'json)
(require 'dash)
(require 'dash-functional)
(require 'request)
(require 'subr-x)

;;;
;;; Configuration
;;;

(defvar slack/token nil
  "Legacy (https://api.slack.com/custom-integrations/legacy-tokens) access token")

(defvar slack/include-public-channels 't
  "Whether or not to inclue public channels in the list of conversations")

(defvar slack/include-private-channels 't
  "Whether or not to inclue public channels in the list of conversations")

(defvar slack/include-im 't
  "Whether or not to inclue IMs (private messages) in the list of conversations")

(defvar slack/include-mpim nil
  "Whether or not to inclue multi-person IMs (multi-person private messages) in
  the list of conversations")

;;;
;;; Utilities
;;;

(defmacro comment (&rest _body)
  "Comment out one or more s-expressions"
  nil)

(defun ->list (vec) (append vec nil))

(defun json-truthy? (x) (and x (not (equal :json-false x))))

;;;
;;; Generic API integration
;;;

(defvar slack/base-url "https://slack.com/api")

(defun slack/get (path params &optional callback)
  "params is an alist of query parameters"
  (let* ((params-callback (if (functionp params) `(() . ,params) (cons params callback)))
         (params (car params-callback)) (callback (cdr params-callback))
         (params (append `(("token" . ,slack/token)) params))
         (url (concat (file-name-as-directory slack/base-url) path)))
    (request url
             :type "GET"
             :params params
             :parser 'json-read
             :success (cl-function
                       (lambda (&key data &allow-other-keys)
                         (funcall callback data))))))

(defun slack/post (path params &optional callback)
  (let* ((params-callback (if (functionp params) `(() . ,params) (cons params callback)))
         (params (car params-callback)) (callback (cdr params-callback))
         (url (concat (file-name-as-directory slack/base-url) path)))
    (request url
             :type "POST"
             :data (json-encode params)
             :headers `(("Content-Type"  . "application/json")
                        ("Authorization" . ,(format "Bearer %s" slack/token)))
             :success (cl-function
                       (lambda (&key data &allow-other-keys)
                         (funcall callback data))))))


;;;
;;; Specific API endpoints
;;;

;; Users

(defun slack/users (cb)
  "Returns users as (id . name) pairs"
  (slack/get
   "users.list"
   (lambda (data)
     (->> data
          (assoc-default 'members)
          ->list
          (-map (lambda (user)
                  (cons (assoc-default 'id user)
                        (assoc-default 'real_name user))))
          (-filter #'cdr)
          (funcall cb)))))

(comment
 (slack/get
  "users.list"
  (lambda (data) (setq response-data data)))

 (slack/users (lambda (data) (setq --users data)))

 )

;; Conversations

(defun slack/conversation-types ()
  (->>
   (list (when slack/include-public-channels  "public_channel")
         (when slack/include-private-channels "private_channel")
         (when slack/include-im               "im")
         (when slack/include-mpim             "mpim"))
   (-filter #'identity)
   (s-join ",")))

(defun channel-label (chan users-alist)
  (cond
   ((json-truthy? (assoc-default 'is_channel chan))
    (format "#%s" (assoc-default 'name chan)))
   ((json-truthy? (assoc-default 'is_im chan))
    (let ((user-id (assoc-default 'user chan)))
      (format "Private message with %s" (assoc-default user-id users-alist))))
   ((json-truthy? (assoc-default 'is_mpim chan))
    (->> chan
         (assoc-default 'purpose)
         (assoc-default 'value)))))

(defun slack/conversations (cb)
  "Calls `cb' with (id . '((label . \"label\") '(topic . \"topic\") '(purpose . \"purpose\"))) pairs"
  (slack/get
   "conversations.list"
   `(("types"            . ,(slack/conversation-types))
     ("exclude-archived" . "true"))
   (lambda (data)
     (setq --data data)
     (slack/users
      (lambda (users)
        (->> data
             (assoc-default 'channels)
             ->list
             (-filter
              (lambda (chan) (channel-label chan users)))
             (-map
              (lambda (chan)
                (cons (assoc-default 'id chan)
                      `((label   . ,(channel-label chan users))
                        (topic   . ,(->> chan
                                         (assoc-default 'topic)
                                         (assoc-default 'value)))
                        (purpose . ,(->> chan
                                         (assoc-default 'purpose)
                                         (assoc-default 'value)))))))
             (funcall cb)))))))

(comment
 (slack/get
  "conversations.list"
  '(("types" . "public_channel,private_channel,im,mpim"))
  (lambda (data) (setq response-data data)))

 (slack/get
  "conversations.list"
  '(("types" . "im"))
  (lambda (data) (setq response-data data)))

 (slack/conversations
  (lambda (convos) (setq --conversations convos)))

 )

;; Messages

(cl-defun slack/post-message
    (&key text channel-id (on-success #'identity))
  (slack/post "chat.postMessage"
              `((text    . ,text)
                (channel . ,channel-id)
                (as_user . t))
              on-success))

(comment

 (slack/post-message
  :text "hi slackbot"
  :channel-id slackbot-channel-id
  :on-success (lambda (data) (setq resp data)))

 (-map (lambda (chan) (let ((label (assoc-default 'label (cdr chan)))
                            (id (car chan)))
                        (propertize label 'channel-id id)))
            --conversations)

 )

;;;
;;; Posting code snippets to slack
;;;

(defun prompt-for-channel (cb)
  (slack/conversations
   (lambda (conversations)
     (setq testing (-map (lambda (chan) (let ((label (assoc-default 'label (cdr chan)))
                            (id (car chan)))
                        (propertize label 'channel-id id)))
            conversations))
     (ivy-read
      "Select channel: "
      ;; TODO want to potentially use purpose / topic stuff here
      (-map (lambda (chan) (let ((label (assoc-default 'label (cdr chan)))
                            (id (car chan)))
                        (propertize label 'channel-id id)))
            conversations)
      :history 'slack/channel-history
      :action (lambda (selected)
                (let ((channel-id (get-text-property 0 'channel-id selected)))
                  (funcall cb channel-id)
                  (message "Sent message to %s" selected))))))
  nil)

(defun slack-send-code-snippet (&optional snippet-text)
  (interactive)
  (when-let ((snippet-text (or snippet-text
                               (buffer-substring-no-properties (mark) (point)))))
    (prompt-for-channel
     (lambda (channel-id)
       (slack/post-message
        :text       (format "```\n%s```" snippet-text)
        :channel-id channel-id)))))