(in-package #:chatikbot) (defparameter *fsq-checkins-url* "https://api.foursquare.com/v2/checkins/recent" "URL of recent checkins API") (defparameter *fsq-api-url* "https://api.foursquare.com/v2/~A" "Foursquare API URL") (defvar *fsq-access-token* nil "Access token for a user under which the process is run") (defun %fsq-api-call (method &optional params) (let* ((resp (json-request (format nil *fsq-api-url* method) :parameters (list* (cons "oauth_token" *fsq-access-token*) (cons "v" "20150811") params))) (meta (aget "meta" resp))) (when (not (= 200 (aget "code" meta))) (error (format nil "Foursquare API error, code ~A, errorType '~A', errorDetail '~A'" (aget "code" meta) (aget "errorType" meta) (aget "errorDetail" meta)))) (aget "response" resp))) (defun fsq-fetch-checkins (&optional after-timestamp limit) (aget "recent" (%fsq-api-call "checkins/recent" (list (cons "afterTimestamp" (or after-timestamp "0")) (cons "limit" (or limit "20")))))) (defun fsq-fetch-friends (&optional offset) (list* (aget "user" (%fsq-api-call "users/self")) (aget "items" (aget "friends" (%fsq-api-call "users/self/friends" (list (cons "offset" (or offset "0")))))))) (defun fsq-format-checkin (checkin &optional with-dates) (when checkin (let ((user (aget "user" checkin)) (venue (aget "venue" checkin))) (format nil "📍 ~@[~A~]~@[ ~A~]~@[ в ~A~]~@[ (~A)~]~@[ 📢 ~A~]~:[~; ~A~]" (aget "firstName" user) (aget "lastName" user) (aget "name" venue) (first (aget "formattedAddress" (aget "location" venue))) (aget "shout" checkin) with-dates (local-time:format-timestring nil (local-time:unix-to-timestamp (aget "createdAt" checkin)) :format '(:year "-" (:month 2) "-" (:day 2) " " (:hour 2) ":" (:min 2)))))))