Bladeren bron

Merge branch 'master' of gitlab.enikesha.net:enikesha/chatikbot

Innocenty Enikeew 10 jaren geleden
bovenliggende
commit
92b2cf8199
4 gewijzigde bestanden met toevoegingen van 30 en 1 verwijderingen
  1. 1 0
      chatikbot.asd
  2. 6 0
      chatikbot.lisp
  3. 21 0
      finance.lisp
  4. 2 1
      forecast.lisp

+ 1 - 0
chatikbot.asd

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

+ 6 - 0
chatikbot.lisp

@@ -81,6 +81,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)))
@@ -205,6 +206,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)))))

+ 2 - 1
forecast.lisp

@@ -19,8 +19,9 @@
 (defvar *forecast-point-formats*
   '((:current . (:year "-" (:month 2) "-" (:day 2) " " (:hour 2) ":" (:min 2)))
     (:hour . ((:hour 2) ":" (:min 2)))
-    (:day . (:year "-" (:month 2) "-" (:day 2))))
+    (:day . ((:day 2) " " :short-month " " :short-weekday)))
   "local-time:format-timestring formats for different points")
+
 (defvar *forecast-emojis*
   '(("clear-day" . "☀")
     ("clear-night" . "⭐")