Skip to content

Commit

Permalink
Merge pull request #20 from dawsonfi/master
Browse files Browse the repository at this point in the history
Add answerCallbackQuery api function
  • Loading branch information
Otann authored Aug 27, 2017
2 parents 0d551f0 + 99da462 commit 624e172
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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)))

Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/morse/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
11 changes: 11 additions & 0 deletions test/morse/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]]}}
Expand Down Expand Up @@ -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]))))

0 comments on commit 624e172

Please sign in to comment.