无法在普通Lisp中发布松弛的Webhook网址

时间:2019-02-25 14:50:52

标签: common-lisp

我想发布松弛的Webhook,但出现错误。

这有效:

(defun post-slack ()
  (drakma:http-request "https://hooks.slack.com/services/xxx"
      :method :post
      :content-type "application/json"
      :parameters '(("payload" . "{\"username\":\"bot\",\"icon_emoji\":\":hatching_chick:\",\"text\":\"name: ~A \n email: ~A \n content: ~A \",\"as_user\":true}"))))

但这不是:

(defun post-slack (name email text)
    (setq *payload* (format nil "{\"username\":\"bot\",\"icon_emoji\":\":hatching_chick:\",\"text\":\"<@U7RM4J8MR> name: ~A \n email: ~A \n text: ~A \",\"as_user\":true}" name email text))
    (drakma:http-request "https://hooks.slack.com/services/xxx"
        :method :post
        :content-type "application/json"
        :parameters '(("payload" . *payload*))))

这是我的错误:

The value
  CAVEMAN-STUDY.CONTROLLER::*PAYLOAD*
is not of type
  LIST
when binding SB-C::FAST
   [Condition of type TYPE-ERROR]

Restarts:
 0: [RETRY] Retry SLIME REPL evaluation request.
 1: [*ABORT] Return to SLIME's top level.
 2: [ABORT] abort thread (#<THREAD "new-repl-thread" RUNNING {1008826443}>)

Backtrace:
  0: (FIND-IF-NOT #<FUNCTION (LAMBDA (DRAKMA::THING) :IN DRAKMA:HTTP-REQUEST) {22D088AB}> ("payload" . CAVEMAN-STUDY.CONTROLLER::*PAYLOAD*) :KEY #<FUNCTION CDR>)
  1: (DRAKMA:HTTP-REQUEST #<PURI:URI https://hooks.slack.com/services/xxxx> :METHOD :POST :CONTENT-TYPE "application/json" :PARAMETERS ("payload" . CAVEMAN-STUDY.CON..
  2: (SB-INT:SIMPLE-EVAL-IN-LEXENV (CAVEMAN-STUDY.CONTROLLER:POST-SLACK "aaa" "bbb" "ccc") #<NULL-LEXENV>)
  3: (EVAL (CAVEMAN-STUDY.CONTROLLER:POST-SLACK "aaa" "bbb" "ccase"))
 --more--

请告诉我如何解决。谢谢!

2 个答案:

答案 0 :(得分:4)

您正在将(("payload" . *payload*))传递给drakma:http-request, 并且*payload*在这里进行评估,即它以symbol的形式传递, 不是它的价值。

您可能想要的是类似的东西

(defun post-slack (name email text)
  (let ((payload (format nil "..." ...)))
    (drakma:http-request "https://hooks.slack.com/services/xxx"
                         :method :post
                         :content-type "application/json"
                         :parameters `(("payload" . ,payload)))))

请注意,我将let替换为backquote

PS。如果您想避免使用{{3}},也可以使用(list (cons "payload" *payload*))代替`(("payload" . ,payload))

答案 1 :(得分:1)

因为您在谈论Slack:

还有更多的Slack机器人。

我用quicksearch发现了这些东西(我不知道这是多么方便)。