1
0
Pārlūkot izejas kodu

Minor fix and new telegram API

Innocenty Enikeew 10 gadi atpakaļ
vecāks
revīzija
2604809bf5
3 mainītis faili ar 97 papildinājumiem un 9 dzēšanām
  1. 5 4
      chatikbot.lisp
  2. 68 5
      telegram.lisp
  3. 24 0
      utils.lisp

+ 5 - 4
chatikbot.lisp

@@ -26,10 +26,11 @@
 (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))
-	    (:sticker (telegram-send-sticker chat-id (cdr response) :reply-to reply-id)))
-	  (mapc #'(lambda (r) (send-response chat-id r reply-id)) 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 send-dont-understand (chat-id &optional text reply-id)

+ 68 - 5
telegram.lisp

@@ -9,7 +9,7 @@
                                                     (princ-to-string k)
                                                     (if (pathnamep v) v
                                                         (princ-to-string v)))))
-         (timeout (+ 5 (or (cdr (assoc :timeout args))
+         (timeout (+ 5 (or (aget "timeout" args)
                            *telegram-timeout*)))
          (response
           (handler-case
@@ -30,15 +30,51 @@
          (cons "limit" limit)
          (cons "timeout" timeout))))
 
-(defun telegram-send-message (chat-id text &key disable-web-preview reply-to reply-markup)
+(defun telegram-send-message (chat-id text &key parse-mode disable-web-preview reply-to reply-markup)
   (%telegram-api-call
    "sendMessage"
    (list (cons "chat_id" chat-id)
          (cons "text" text)
+         (cons "parse_mode" parse-mode)
          (cons "disable_web_page_preview" disable-web-preview)
          (cons "reply_to_message_id" reply-to)
          (cons "reply_markup" reply-markup))))
 
+(defun telegram-forward-message (chat-id from-chat-id message-id)
+  (%telegram-api-call
+   "forwardMessage"
+   `(("chat_id" . ,chat-id)
+     ("from_chat_id" . ,from-chat-id)
+     ("message_id" . ,message-id))))
+
+(defun telegram-send-photo (chat-id photo &key caption reply-to reply-markup)
+  (%telegram-api-call
+   "sendPhoto"
+   (list (cons "chat_id" chat-id)
+         (cons "photo" photo)
+         (cons "caption" caption)
+         (cons "reply_to_message_id" reply-to)
+         (cons "reply_markup" reply-markup))))
+
+(defun telegram-send-audio (chat-id audio &key duration performer title reply-to reply-markup)
+  (%telegram-api-call
+   "sendAudio"
+   (list (cons "chat_id" chat-id)
+         (cons "audio" audio)
+         (cons "duration" duration)
+         (cons "performer" performer)
+         (cons "title" title)
+         (cons "reply_to_message_id" reply-to)
+         (cons "reply_markup" reply-markup))))
+
+(defun telegram-send-document (chat-id document &key reply-to reply-markup)
+  (%telegram-api-call
+   "sendDocument"
+   (list (cons "chat_id" chat-id)
+         (cons "document" document)
+         (cons "reply_to_message_id" reply-to)
+         (cons "reply_markup" reply-markup))))
+
 (defun telegram-send-sticker (chat-id sticker &key reply-to reply-markup)
   (%telegram-api-call
    "sendSticker"
@@ -47,17 +83,44 @@
          (cons "reply_to_message_id" reply-to)
          (cons "reply_markup" reply-markup))))
 
-(defun telegram-send-photo (chat-id photo &key caption reply-to reply-markup)
+(defun telegram-send-video (chat-id video &key duration caption reply-to reply-markup)
   (%telegram-api-call
-   "sendPhoto"
+   "sendVideo"
    (list (cons "chat_id" chat-id)
-         (cons "photo" photo)
+         (cons "video" video)
+         (cons "duration" duration)
          (cons "caption" caption)
          (cons "reply_to_message_id" reply-to)
          (cons "reply_markup" reply-markup))))
 
+(defun telegram-send-voice (chat-id voice &key duration reply-to reply-markup)
+  (%telegram-api-call
+   "sendVoice"
+   (list (cons "chat_id" chat-id)
+         (cons "voice" voice)
+         (cons "duration" duration)
+         (cons "reply_to_message_id" reply-to)
+         (cons "reply_markup" reply-markup))))
+
+(defun telegram-send-location (chat-id latitude longitude &key reply-to reply-markup)
+  (%telegram-api-call
+   "sendLocation"
+   (list (cons "chat_id" chat-id)
+         (cons "latitude" latitude)
+         (cons "longitude" longitude)
+         (cons "reply_to_message_id" reply-to)
+         (cons "reply_markup" reply-markup))))
+
 (defun telegram-send-chat-action (chat-id action)
   (%telegram-api-call
    "sendChatAction"
    (list (cons "chat_id" chat-id)
          (cons "action" action))))
+
+(defun telegram-get-user-profile-photos (user-id &key offset limit)
+  (%telegram-api-call
+   "getUserProfilePhotos"
+   `(("user_id" . ,user-id) ("offset" . ,offset) ("limit" . ,limit))))
+
+(defun telegram-get-file (file-id)
+  (%telegram-api-call "getFile" `(("file_id" . ,file-id))))

+ 24 - 0
utils.lisp

@@ -134,6 +134,30 @@ is replaced with replacement."
                                 :format '(:year "-" (:month 2) "-" (:day 2) " "
                                           (:hour 2) ":" (:min 2) ":" (:sec 2))))
 
+(defun google-tts (text &key (lang "en"))
+  (let ((path #P"google_tts.mp3"))
+    (with-open-file (s path :direction :output
+                       :element-type '(unsigned-byte 8)
+                       :if-exists :supersede
+                       :if-does-not-exist :create)
+      (write-sequence
+       (drakma:http-request
+        "http://translate.google.com/translate_tts"
+        :parameters `(("ie" . "UTF-8")
+                      ("client" . "t")
+                      ("tl" . ,lang)
+                      ("q" . ,text))
+        :user-agent "stagefright/1.2 (Linux;Android 5.0)"
+        :additional-headers '((:referer . "http://translate.google.com/"))
+        :external-format-out :utf-8
+        :force-binary t)
+       s)
+      path)))
+
+(defun say-it (lang words)
+  (cons :voice
+        (google-tts (print-with-spaces words) :lang lang)))
+
 ;; Fix bug in local-time (following symlinks in /usr/share/zoneinfo/
 ;; leads to bad cutoff)
 (in-package #:local-time)