Skip to content

Instantly share code, notes, and snippets.

@timdorohin timdorohin/bot.lisp
Last active May 16, 2017

Embed
What would you like to do?
(ql:quickload :drakma)
(ql:quickload :cl-json)
(ql:quickload :cl-ppcre)
;(setf drakma:*header-stream* *standard-output*)
(defun prompt-read (prompt)
(format *query-io* "~a: " prompt)
(force-output *query-io*)
(read-line *query-io*))
(defvar *cookie-jar* (make-instance 'drakma:cookie-jar :cookies
(list (make-instance 'drakma:cookie
:name "csrftoken"
:value "bot"
:domain "beta.mlug.ru"
:path "/"))))
(defvar token nil)
(defvar sessionid nil)
(defvar *last-uuid* nil)
(defvar *messages-db* nil)
(defun init-poster ()
(drakma:http-request "https://beta.mlug.ru/id/key_authenticate"
:method :post
:parameters '(("csrfmiddlewaretoken" . "bot")("key" . "your key")
("redirect" . "https://beta.mlug.ru/"))
:additional-headers '(("Accept" . "text/html")
("Referer" . "https://beta.mlug.ru/id/?key_auth=1"))
:cookie-jar *cookie-jar*)
(setf token (drakma:cookie-value (second (drakma:cookie-jar-cookies *cookie-jar*))))
(setf sessionid (drakma:cookie-value (first (drakma:cookie-jar-cookies *cookie-jar*)))))
(defun say (text)
(drakma:http-request "https://beta.mlug.ru/chat/tsmr/message"
:method :post
:parameters (list
(cons "message" text)(cons "csrfmiddlewaretoken" token) )
:additional-headers (list (cons "Content-type" "application/x-www-form-urlencoded")
(cons "Referer" "https://beta.mlug.ru/chat/tsmr")
(cons "X-CSRFToken" token))
:cookie-jar *cookie-jar* :external-format-out :utf-8))
(init-poster)
(defun get-messages (uuid)
(cdr
(second
(cl-json:decode-json (drakma:http-request "https://beta.mlug.ru/chat/tsmr/poll"
:method :get
:parameters
(list (cons "uuid" uuid))
:additional-headers
(list (cons "Referer" "https://beta.mlug.ru/chat/tsmr")
(cons "X-CSRFToken" token))
:cookie-jar *cookie-jar* :external-format-out :utf-8 :want-stream t))))
)
(defun parse-messages (messages)
(let ((msg-list nil))
(dolist (msg messages)
(setf *last-uuid* (first msg))
(if (not (equalp (third msg) "Alice")) (setf msg-list (append msg-list (list(list (third msg) (fourth msg))))))
)
(return-from parse-messages msg-list)
)
)
(defun split-msg (message)
(cl-ppcre:split "\\s+" message :with-registers-p t :omit-unmatched-p t))
(defun respond (message)
(when (dolist (word (split-msg (second message)))
(print word)
(if (equalp word "хуй") (return t))
)
(print (first message))
(say (concatenate 'string "Сам иди нахуй, " (first message) " #" (write-to-string (get-universal-time))))
)
)
(defun chatting ()
(let ((start (get-universal-time))(stop nil)(temp-mess nil))
(when (setf temp-mess (get-messages *last-uuid*))
(setf temp-mess (parse-messages temp-mess))
(dolist (msg temp-mess)
(print msg)
(respond msg)
(print msg)
)
)
(setf stop (get-universal-time))
(if (< (- stop start) 3) (sleep (- 3 (- stop start))))
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.