猫でもわかるWebプログラミングと副業

本業エンジニアリングマネージャー。副業Webエンジニア。Web開発のヒントや、副業、日常生活のことを書きます。

EmacsからSlack分報に投稿するやつについてメモ

元記事

これを読んで試しに導入してみようかと思いました

niku.name

元のコード

元コードはこんな感じなのですが、json.elが必要だったり、アイコンがデフォルトだったりしたのでちょっと変更します。 incoming webhookのURLについてはここでは説明しません。ぐぐって :bow:

(defun times-niku (s)
  "Post to #times-niku channel on the slack"
  (interactive "sText:")
  (with-temp-buffer
    (let ((url                       "https://hooks.slack.com/services/path/to/your/incoming/webhook")
          (url-request-method        "POST")
          (url-request-extra-headers `(("Content-Type" . "application/x-www-form-urlencoded")))
          (url-request-data          (format "payload={\"text\": %s}" (json-encode-string s)))
          (mycallback                (lambda (x) (message (format "%s" x)))))
      (url-retrieve url mycallback))))

(define-key global-map (kbd "C-c C-M-p") 'times-niku)

el-getを使ってjson.elを導入

先程も言ったように、このコードで使われている json-encode-stringjson.elのコードだと思われるため、これを導入します。 el-getの使い方もここでは説明しません。各々自分の使っているパッケージ管理プラグイン等で入れて下さい。

(el-get-bundle thorstadt/json.el)
(require 'json)

(defun times-niku (s)
  "Post to #times-niku channel on the slack"
  (interactive "sText:")
  (with-temp-buffer
    (let ((url                       "https://hooks.slack.com/services/path/to/your/incoming/webhook")
      (url-request-method        "POST")
      (url-request-extra-headers `(("Content-Type" . "application/x-www-form-urlencoded")))
          (url-request-data          (format "payload={\"text\": %s}" (json-encode-string s)))
      (mycallback                (lambda (x) (message (format "%s" x)))))
      (url-retrieve url mycallback))))

(define-key global-map (kbd "C-c C-M-p") 'times-niku)

アイコンとユーザー名を指定

アイコンとユーザー名を指定します

(el-get-bundle thorstadt/json.el)
(require 'json)

(defun times-niku (s)
  "Post to #times-niku channel on the slack"
  (interactive "sText:")
  (with-temp-buffer
    (let ((url                       "https://hooks.slack.com/services/path/to/your/incoming/webhook")
      (url-request-method        "POST")
      (url-request-extra-headers `(("Content-Type" . "application/x-www-form-urlencoded")))
      (url-request-data          (format "payload={\"text\": %s, \"username\": \"utakata_emacs\", \"icon_emoji\": \":y_oshiki_utakata:\"}" (json-encode-string s)))
      (mycallback                (lambda (x) (message (format "%s" x)))))
      (url-retrieve url mycallback))))

(define-key global-map (kbd "C-c C-M-p") 'times-niku)

関数名とか整理

最後に関数名とかを整理して終わり

(el-get-bundle thorstadt/json.el)
(require 'json)
(defun slack-times (s)
  "Post to times channel on the slack."
  (interactive "sText:")
  (with-temp-buffer
    (let ((url                       "https://hooks.slack.com/services/path/to/your/incoming/webhook")
      (url-request-method        "POST")
      (url-request-extra-headers `(("Content-Type" . "application/x-www-form-urlencoded")))
      (url-request-data          (format "payload={\"text\": %s, \"username\": \"utakata_emacs\", \"icon_emoji\": \":y_oshiki_utakata:\"}" (json-encode-string s)))
      (mycallback                (lambda (x) (message (format "%s" x)))))
      (url-retrieve url mycallback))))

(define-key global-map (kbd "C-c C-M-p") 'slack-times)

おわり