Innokenty Enikeev 10 jaren geleden
bovenliggende
commit
d6d38175e2
3 gewijzigde bestanden met toevoegingen van 28 en 0 verwijderingen
  1. 1 0
      chatikbot.asd
  2. 6 0
      chatikbot.lisp
  3. 21 0
      finance.lisp

+ 1 - 0
chatikbot.asd

@@ -19,4 +19,5 @@
                (:file "telegram")
                (:file "forecast")
                (:file "vk")
+               (:file "finance")
                (:file "chatikbot")))

+ 6 - 0
chatikbot.lisp

@@ -84,6 +84,7 @@
               (:weather (handle-cmd-weather chat-id id args))
               (:hourly (handle-cmd-weather chat-id id '("hourly")))
               (:daily (handle-cmd-weather chat-id id '("daily")))
+	      (:rates (handle-cmd-rates chat-id id args))
               (:help (handle-cmd-help chat-id id args))
               (otherwise (handle-admin-cmd chat-id text cmd args))))
           (send-dont-understand chat-id text)))
@@ -208,6 +209,11 @@
   (log:info "handle-cmd-help" chat-id message-id args)
   (telegram-send-message chat-id (random-choice *help-responses*)))
 
+(defun handle-cmd-rates (chat-id message-id args)
+  (log:info "handle-cmd-rates" chat-id message-id args)
+  (let (rates (get-rates))
+    (telegram-send-message chat-id
+			   (format nil "Зеленый ~A, гейро ~A" (cdar rates) (cdadr rates)))))
 
 (defun handle-cmd-weather (chat-id message-id args)
   (log:info "handle-cmd-weather" chat-id message-id args)

+ 21 - 0
finance.lisp

@@ -0,0 +1,21 @@
+(in-package #:chatikbot)
+
+(defparameter +yahoo-url+ "https://query.yahooapis.com/v1/public/yql" "Yahoo Finance API endpoint")
+(defparameter +yahoo-query+ "select Name,Rate from yahoo.finance.xchange where pair in (~{\"~A\"~^,~})")
+
+(defvar *rate-pairs* '("USDRUB" "EURRUB"))
+
+(defun get-rates (&optional (pairs *rate-pairs*))
+  (let ((response (yason:parse
+		   (flexi-streams:octets-to-string
+		    (drakma:http-request +yahoo-url+
+					 :parameters (append '(("format" . "json")
+							       ("env" . "store://datatables.org/alltableswithkeys"))
+							     (list (cons "q" (format nil +yahoo-query+ pairs)))))
+		    :external-format :utf-8)
+		   :object-as :alist)))
+    (when (aget "error" response)
+      (error "Error in rates request"))
+    (loop for rate in (aget "rate" (aget "results" (aget "query" response)))
+       collect (cons (aget "Name" rate)
+		     (aget "Rate" rate)))))