diff --git a/README.md b/README.md index 6e773da..36babcf 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,12 @@ There is also a template which you can use to bootstrap your project: export TELEGRAM_TOKEN=... lein run -## Detecting user's actions +## Detecting user's actions Telegram sends updates about events in chats in form of [Update](https://core.telegram.org/bots/api#update) objects. -Inside those there could be commands, inline queries and many more. +Inside those there could be commands, inline queries and many more. To help you with these Morse provides you helpers and some macros in `morse.handlers` namespace. @@ -38,7 +38,7 @@ you'll find similarities here: (ns user (:require [morse.handlers :as h] [morse.api :as t])) - + (def token "YOUR-BIG-SECRET") ; This will define bot-api function, which later could be @@ -48,20 +48,20 @@ you'll find similarities here: ; This could be done in form of a function: (h/command-fn "start" (fn [{{id :id :as chat} :chat}] (println "Bot joined new chat: " chat) - (t/send-text token id "Welcome!"))) + (t/send-text token id "Welcome!"))) ; You can use short syntax for same purposes ; Destructuring works same way as in function above (h/command "help" {{id :id :as chat} :chat} (println "Help was requested in " chat) (t/send-text token id "Help is on the way")) - + ; Handlers will be applied until there are any of those ; returns non-nil result processing update. - - ; Note that sending stuff to the user returns non-nil + + ; Note that sending stuff to the user returns non-nil ; response from Telegram API. - + ; So match-all catch-through case would look something like this: (h/message message (println "Intercepted message:" message))) @@ -101,7 +101,7 @@ in a similar form: ### Callbacks You can provide handlers for [Callbacks](https://core.telegram.org/bots/api#answercallbackquery) -which are sent from [inline keyboards](https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating) +which are sent from [inline keyboards](https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating) ```clojure (callback-fn (fn [data] (println "Received callback: " inline))) @@ -251,6 +251,17 @@ Sends an answer to an inline query. :gif_url "http://funnygifs/gif.gif"}]) ``` +### [`answerCallbackQuery`](https://core.telegram.org/bots/api#answercallbackquery) + +Sends an answer to an callback query sent from inline keyboards. + +```clojure +(api/answer-callback token + callback-query-id + text + show-alert) +``` + ## License Copyright © 2016 Anton Chebotaev diff --git a/src/morse/api.clj b/src/morse/api.clj index a484631..bb77751 100644 --- a/src/morse/api.clj +++ b/src/morse/api.clj @@ -140,3 +140,15 @@ :as :json :form-params body})] (-> resp :body)))) + +(defn answer-callback + "Sends an answer to an callback query" + ([token callback-query-id] (answer-callback token "" false)) + ([token callback-query-id text] (answer-callback token text false)) + ([token callback-query-id text show-alert] + (let [url (str base-url token "/answerCallbackQuery") + body {:callback_query_id callback-query-id :text text :show_alert show-alert} + resp (http/post url {:content-type :json + :as :json + :form-params body})] + (-> resp :body)))) diff --git a/test/morse/api_test.clj b/test/morse/api_test.clj index 8c7024c..a60f39e 100644 --- a/test/morse/api_test.clj +++ b/test/morse/api_test.clj @@ -8,6 +8,7 @@ (def chat-id 239) (def message-id 1) (def inline-query-id 1337) +(def callback-query-id 1338) (deftest send-text-request (let [options {:parse_mode "Markdown" :reply_markup {:keyboard [[{:text "button"}]]}} @@ -98,3 +99,13 @@ (is (u/has-subset? {:inline_query_id inline-query-id} [body])) (is (u/has-subset? {:results [{:type "gif" :id 31337 :gif_url "gif.gif"}]} [body])) (is (u/has-subset? {:is_personal true} [body])))) + +(deftest answer-callback-request + (let [req (-> (api/answer-callback token callback-query-id "text" true) + (u/capture-request)) + body (json/decode (slurp (:body req)) true)] + + (is (= :post (:request-method req))) + (is (u/has-subset? {:callback_query_id callback-query-id} [body])) + (is (u/has-subset? {:text "text"} [body])) + (is (u/has-subset? {:show_alert true} [body]))))