-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from 40ants/many-changes
Add inline keyboards, callbacks and more.
- Loading branch information
Showing
22 changed files
with
860 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
.#* | ||
.*.~undo-tree~ | ||
.DS_Store | ||
*.fasl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
(uiop:define-package #:example-bot/bot | ||
(:use #:cl) | ||
(:import-from #:cl-telegram-bot | ||
#:start-processing | ||
#:on-message | ||
#:defbot) | ||
(:import-from #:cl-telegram-bot/chat | ||
#:private-chat | ||
#:get-username) | ||
(:import-from #:cl-telegram-bot/message | ||
#:get-current-chat) | ||
(:import-from #:serapeum | ||
#:dict | ||
#:fmt) | ||
(:import-from #:cl-telegram-bot/response | ||
#:alert | ||
#:notify | ||
#:reply) | ||
(:import-from #:cl-telegram-bot/inline-keyboard | ||
#:callback-button | ||
#:inline-keyboard)) | ||
(in-package #:example-bot/bot) | ||
|
||
|
||
(defbot example-bot) | ||
|
||
|
||
(defmethod on-message ((bot example-bot) | ||
text) | ||
(let* ((chat (get-current-chat)) | ||
(username (get-username chat))) | ||
(log:info "Talking to" username) | ||
(let ((keyboard (when (string-equal text "show") | ||
(inline-keyboard | ||
(list | ||
(callback-button "Alert" "alert") | ||
(callback-button "Notify" "notify") | ||
(callback-button "Text me" "text")))))) | ||
(reply (fmt "Привет ~A!" | ||
username) | ||
:reply-markup keyboard)))) | ||
|
||
|
||
(defmethod cl-telegram-bot/callback:on-callback ((bot example-bot) | ||
callback) | ||
(let ((data (cl-telegram-bot/callback:callback-data callback))) | ||
(cond | ||
((string-equal data | ||
"alert") | ||
(cl-telegram-bot/response:alert "You pressed alert button!")) | ||
|
||
((string-equal data | ||
"notify") | ||
(cl-telegram-bot/response:reply "Just replying with text.") | ||
(cl-telegram-bot/response:notify "You pressed notify button!")) | ||
(t | ||
(cl-telegram-bot/response:reply "Just replying with text."))))) | ||
|
||
|
||
(defun start (&key token) | ||
(start-processing (make-example-bot (or token | ||
(uiop:getenv "TELEGRAM_TOKEN") | ||
(error "Define TELEGRAM_TOKEN env var."))) | ||
:debug t)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
(uiop:define-package #:cl-telegram-bot/callback | ||
(:use #:cl) | ||
(:import-from #:cl-telegram-bot/message | ||
#:message | ||
#:get-chat | ||
#:*current-message* | ||
#:get-rest-args | ||
#:get-text | ||
#:*current-bot* | ||
#:send-message) | ||
(:import-from #:cl-telegram-bot/pipeline | ||
#:process) | ||
(:import-from #:cl-telegram-bot/chat | ||
#:get-chat-id) | ||
(:import-from #:cl-telegram-bot/response-processing | ||
#:process-response) | ||
(:export #:callback-data | ||
#:callback | ||
#:make-callback | ||
#:on-callback | ||
#:callback-id | ||
#:callback-message | ||
#:callback-chat)) | ||
(in-package #:cl-telegram-bot/callback) | ||
|
||
|
||
(defclass callback () | ||
((id :initarg :id | ||
:type string | ||
:reader callback-id) | ||
(data :initarg :data | ||
:type string | ||
:reader callback-data) | ||
(message :initarg :message | ||
:type message | ||
:reader callback-message))) | ||
|
||
|
||
(defgeneric on-callback (bot callback) | ||
(:documentation "Called when user clicks callback button. Second argument is an object of CALLBACK type.") | ||
(:method ((bot t) (callback t)) | ||
;; Doing nothing | ||
(values))) | ||
|
||
|
||
(defgeneric make-callback (bot callback-data) | ||
(:documentation "Called when user clicks callback button. Should return an instance of CALLBACK class. | ||
Application may override this method to return objects of different callback classes depending on | ||
callback-data string. This way it mab be easier to define more specific methods for | ||
ON-CALLBACK generic-function.") | ||
(:method ((bot t) (callback-data t)) | ||
(let ((id (getf callback-data :|id|)) | ||
(data (getf callback-data :|data|)) | ||
(message-data (getf callback-data :|message|))) | ||
(make-instance 'callback | ||
:id id | ||
:data data | ||
:message (cl-telegram-bot/message:make-message message-data))))) | ||
|
||
|
||
(defmethod process ((bot t) (callback callback)) | ||
"" | ||
(log:debug "Processing callback" callback) | ||
|
||
(let ((*current-bot* bot) | ||
(*current-message* callback)) | ||
(handler-case | ||
(on-callback bot callback) | ||
(cl-telegram-bot/response-processing:interrupt-processing (condition) | ||
(declare (ignore condition)) | ||
(log:debug "Interrupting callback processing" callback)))) | ||
(values)) | ||
|
||
|
||
(defgeneric callback-chat (callback) | ||
(:documentation "Returns a chat from where callback was sent.") | ||
|
||
(:method ((callback callback)) | ||
(cl-telegram-bot/message:get-chat (callback-message callback)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
(uiop:define-package #:cl-telegram-bot/commands | ||
(:use #:cl) | ||
(:import-from #:log4cl) | ||
(:import-from #:cl-telegram-bot/chat | ||
#:get-chat-id | ||
#:make-chat | ||
#:chat) | ||
(:import-from #:cl-telegram-bot/entities/core | ||
#:make-entity) | ||
(:import-from #:cl-telegram-bot/network | ||
#:make-request) | ||
(:import-from #:cl-telegram-bot/pipeline | ||
#:process) | ||
(:import-from #:cl-telegram-bot/bot | ||
#:bot) | ||
(:import-from #:serapeum | ||
#:soft-alist-of | ||
#:defvar-unbound) | ||
(:import-from #:cl-telegram-bot/utils | ||
#:def-telegram-call) | ||
(:import-from #:cl-telegram-bot/response-processing | ||
#:process-response | ||
#:interrupt-processing) | ||
(:export)) | ||
(in-package #:cl-telegram-bot/commands) | ||
|
||
|
||
(declaim (ftype (function (bot (or (soft-alist-of string string) | ||
(serapeum:soft-list-of string))) | ||
(values)) | ||
set-my-commands)) | ||
|
||
;; TODO: Support scope and language optional arguments | ||
(defun set-my-commands (bot commands) | ||
"https://core.telegram.org/bots/api#setmycommands" | ||
(log:debug "Sending commands to the server" commands) | ||
(make-request bot "setMyCommands" | ||
:|commands| (loop for command in commands | ||
collect (etypecase command | ||
(string (list :|command| command | ||
:|description| "No documentation.")) | ||
(cons (list :|command| (car command) | ||
:|description| (cdr command)))))) | ||
(values)) |
Oops, something went wrong.