Explorar o código

[refactor] send-response -> bot-send-message

Innocenty Enikeew %!s(int64=8) %!d(string=hai) anos
pai
achega
e7c6d03ff1
Modificáronse 9 ficheiros con 55 adicións e 44 borrados
  1. 10 10
      bot.lisp
  2. 1 2
      chatikbot.lisp
  3. 0 1
      common.lisp
  4. 1 1
      plugins/huiza.lisp
  5. 6 6
      plugins/ledger.lisp
  6. 1 1
      plugins/nalunch.lisp
  7. 5 5
      plugins/saver.lisp
  8. 1 1
      plugins/tumblr.lisp
  9. 30 17
      telegram.lisp

+ 10 - 10
bot.lisp

@@ -5,7 +5,7 @@
                 :set-setting)
   (:import-from :chatikbot.telegram
                 :telegram-get-updates
-                :send-response)
+                :bot-send-message)
   (:export :handle-update
            :*bot-user-id*
            :on-next-message))
@@ -39,19 +39,19 @@
 
 (defvar *start-message* "Hello" "Welcome message. Override it")
 (def-message-cmd-handler handle-start (:start)
-  (send-response chat-id *start-message*))
+  (bot-send-message chat-id *start-message*))
 
 (def-message-admin-cmd-handler handle-admin-settings (:settings)
-  (send-response chat-id
-                 (format nil "~{~{~A~@[ (~A)~]: ~A~}~^~%~}"
-                         (loop for symbol in *settings*
-                            collect (list symbol (documentation symbol 'variable) (symbol-value symbol))))))
+  (bot-send-message chat-id
+                    (format nil "~{~{~A~@[ (~A)~]: ~A~}~^~%~}"
+                            (loop for symbol in *settings*
+                               collect (list symbol (documentation symbol 'variable) (symbol-value symbol))))))
 
 (def-message-admin-cmd-handler handle-admin-setsetting (:setsetting)
   (let* ((*package* (find-package :chatikbot))
          (var (read-from-string (car args)))
          (val (read-from-string (format nil "~{~A~^ ~}" (rest args)))))
-    (send-response chat-id (format nil "OK, ~A" (set-setting var val)))))
+    (bot-send-message chat-id (format nil "OK, ~A" (set-setting var val)))))
 
 (defvar *chat-next-message-handlers* (make-hash-table) "Out-of-order chat message handler")
 (defmacro on-next-message (chat-id &body body)
@@ -67,7 +67,7 @@
            (handler-case (funcall handler message)
              (error (e)
                (log:error "~A" e)
-               (send-response chat-id
-                              (format nil "Ошибочка вышла~@[: ~A~]"
-                                      (when (member chat-id *admins*) e)))))
+               (bot-send-message chat-id
+                                 (format nil "Ошибочка вышла~@[: ~A~]"
+                                         (when (member chat-id *admins*) e)))))
         (remhash chat-id *chat-next-message-handlers*)))))

+ 1 - 2
chatikbot.lisp

@@ -19,8 +19,7 @@
                 :*telegram-token*
                 :telegram-get-me
                 :telegram-set-webhook
-                :telegram-send-message
-                :send-response)
+                :telegram-send-message)
   (:import-from :chatikbot.secrets
                 :*secret-ring*
                 :*secret-pass-store*

+ 0 - 1
common.lisp

@@ -78,7 +78,6 @@
            :telegram-reply-keyboard-markup
            :telegram-reply-keyboard-hide
            :telegram-force-reply
-           :send-response
            :bot-send-message
            :token-hmac
            :encode-callback-data

+ 1 - 1
plugins/huiza.lisp

@@ -50,7 +50,7 @@
   (let* ((*package* (find-package :chatikbot.plugins.huiza))
          (resp (eliza (read-from-string-no-punct text) *huiza-rules*)))
     (when resp
-      (send-response chat-id resp reply-id))))
+      (bot-send-message chat-id resp :reply-to reply-id))))
 
 (def-message-handler huiza-handler (message -1000)
   (log:info "handle-unknown-message" message)

+ 6 - 6
plugins/ledger.lisp

@@ -132,7 +132,7 @@
                                               (ledger/format-time ut)
                                               (ledger/get-hook-url chat-id))
                               :disable-web-preview t)))
-        (send-response chat-id "Добавь журнал: uri - /ledger <url>; git - /ledger <remote> <path>"))))
+        (bot-send-message chat-id "Добавь журнал: uri - /ledger <url>; git - /ledger <remote> <path>"))))
 
 (defun ledger/handle-set-info (chat-id info)
   (setf (gethash chat-id *ledger/chat-journals*) nil)
@@ -141,13 +141,13 @@
           (get-chat-journal-info chat-id info)
         (declare (ignore ut))
         (secret-set (list :ledger chat-id) info)
-        (send-response chat-id (format nil "Добавил журнал с ~D записями. Веб-хук для обновления: ~A"
+        (bot-send-message chat-id (format nil "Добавил журнал с ~D записями. Веб-хук для обновления: ~A"
                                        (length journal)
                                        (ledger/get-hook-url chat-id))))
     (pta-ledger:journal-failed (e)
-      (send-response chat-id (format nil "Не смог спарсить: ~A" e)))
+      (bot-send-message chat-id (format nil "Не смог спарсить: ~A" e)))
     (dex:http-request-failed (e)
-      (send-response chat-id (format nil "Не смог в урл: ~A" (dex:response-body e))))))
+      (bot-send-message chat-id (format nil "Не смог в урл: ~A" (dex:response-body e))))))
 
 
 (def-message-cmd-handler handler-ledger (:ledger)
@@ -162,7 +162,7 @@
            (destructuring-bind (,journal . ,updated) ,info
              (declare (ignorable ,journal ,updated))
              ,@body)
-           (send-response ,chat-id "Добавь журнал: uri - /ledger <url>; git - /ledger <remote> <path>")))))
+           (bot-send-message ,chat-id "Добавь журнал: uri - /ledger <url>; git - /ledger <remote> <path>")))))
 
 (defun ledger/handle-balance (chat-id query)
   (with-chat-journal (chat-id journal updated)
@@ -231,7 +231,7 @@
                                                   (spaced (subseq args 1))
                                                   (parse-float (car args))
                                                   (symbol-name cmd))))
-    (:otherwise (send-response chat-id (format nil "/~A <amount> <description>" cmd)))))
+    (:otherwise (bot-send-message chat-id (format nil "/~A <amount> <description>" cmd)))))
 
 (def-webhook-handler ledger/handle-webhook ("ledger")
   (when (= 2 (length paths))

+ 1 - 1
plugins/nalunch.lisp

@@ -110,7 +110,7 @@
             (when new
               (when (and old (not (equal (aget :balance old)
                                          (aget :balance new))))
-                (send-response chat-id (%nalunch/format new t)))
+                (bot-send-message chat-id (%nalunch/format new t)))
               (setf (gethash chat-id *nalunch/last-results*) new
                     (gethash chat-id *nalunch/jars*) cookie-jar)))
           (progn

+ 5 - 5
plugins/saver.lisp

@@ -314,10 +314,10 @@
           (saver/payment-next-time payment (get-universal-time))
           (handler-case
               (db/saver/add-payment chat-id payment)
-            (error () (send-response chat-id (format nil "Платёж '~A' уже есть!"
-                                                      (saver/payment-name payment)))))
+            (error () (bot-send-message chat-id (format nil "Платёж '~A' уже есть!"
+                                                        (saver/payment-name payment)))))
           (saver/send-info chat-id))
-        (send-response chat-id "Неправильно. /saver add <title> <amount> <cron> [started]"))))
+        (bot-send-message chat-id "Неправильно. /saver add <title> <amount> <cron> [started]"))))
 
 (defun saver/del-payment (chat-id args)
   (handler-case
@@ -333,7 +333,7 @@
                                        (/ (getf (saver/get-expense-info payment incomes) :saved-amount)
                                           100)))
                           :parse-mode "markdown"))
-    (error (e) (send-response chat-id (format nil "/saver del <idx> [~A]" e)))))
+    (error (e) (bot-send-message chat-id (format nil "/saver del <idx> [~A]" e)))))
 
 ;; Hooks
 (def-message-cmd-handler handler-cmd-save (:save :saver)
@@ -342,4 +342,4 @@
       (case (keyify (car args))
         (:add (saver/add-payment chat-id (rest args)))
         (:del (saver/del-payment chat-id (rest args)))
-        (t (send-response chat-id "Надо /saver add ... или /saver del <idx>")))))
+        (t (bot-send-message chat-id "Надо /saver add ... или /saver del <idx>")))))

+ 1 - 1
plugins/tumblr.lisp

@@ -60,4 +60,4 @@
          (resp (eliza (read-from-string-no-punct text) *tumblr-rules*)))
     (when resp
       (log:info resp)
-      (send-response chat-id resp))))
+      (bot-send-message chat-id resp))))

+ 30 - 17
telegram.lisp

@@ -29,7 +29,6 @@
            :telegram-reply-keyboard-markup
            :telegram-reply-keyboard-hide
            :telegram-force-reply
-           :send-response
            :bot-send-message))
 (in-package :chatikbot.telegram)
 
@@ -249,21 +248,35 @@
 
 ;; Simplified interface
 ;;
-(defun send-response (chat-id response &optional reply-id)
-  (if (consp response)
-      (if (keywordp (car response))
-          (case (car response)
-            (:text (telegram-send-message chat-id (cdr response) :reply-to reply-id))
-            (:voice (telegram-send-voice chat-id (cdr response) :reply-to reply-id))
-            (:sticker (telegram-send-sticker chat-id (cdr response) :reply-to reply-id)))
-          (mapc #'(lambda (r) (send-response chat-id r reply-id)) response))
-      (telegram-send-message chat-id response :reply-to reply-id)))
-
-(defun bot-send-message (chat-id text &key parse-mode disable-web-preview disable-notification reply-to reply-markup)
-  (handler-case (telegram-send-message chat-id text :parse-mode parse-mode
-                                       :disable-web-preview disable-web-preview
-                                       :disable-notification disable-notification
-                                       :reply-to reply-to
-                                       :reply-markup reply-markup)
+(defun bot-send-message (chat-id message &key parse-mode disable-web-preview disable-notification reply-to reply-markup duration)
+  (handler-case (if (consp message)
+                    (if (keywordp (car message))
+                        (case (car message)
+                          (:text (telegram-send-message chat-id (cdr message)
+                                                        :parse-mode parse-mode
+                                                        :disable-web-preview disable-web-preview
+                                                        :disable-notification disable-notification
+                                                        :reply-to reply-to
+                                                        :reply-markup reply-markup))
+                          (:voice (telegram-send-voice chat-id (cdr message)
+                                                       :duration duration
+                                                       :reply-to reply-to
+                                                       :reply-markup reply-markup))
+                          (:sticker (telegram-send-sticker chat-id (cdr message)
+                                                           :reply-to reply-to
+                                                           :reply-markup reply-markup)))
+                        (mapc #'(lambda (m) (bot-send-message chat-id m
+                                                              :parse-mode parse-mode
+                                                              :disable-web-preview disable-web-preview
+                                                              :disable-notification disable-notification
+                                                              :reply-to reply-to
+                                                              :reply-markup reply-markup
+                                                              :duration duration)) message))
+                    (telegram-send-message chat-id message
+                                           :parse-mode parse-mode
+                                           :disable-web-preview disable-web-preview
+                                           :disable-notification disable-notification
+                                           :reply-to reply-to
+                                           :reply-markup reply-markup))
     (error (e)
       (log:error e))))