Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,34 @@ Sends an answer to an callback query sent from inline keyboards.
show-alert)
```


## Q-Code

Q-Codes in Morse are shorthands for common patterns. `morse.qcodes/direct-reply` & `morse.qcodes/req-morse` are the Q-Codes already available. Here is an example usage of both.

```clojure
(ns look.anotherbadlynamedns
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rest of the docs uses slightly different style of imports

(:require [morse.qcodes :as q]
[morse.handlers :as h]
[somehttpserver :as http]))

(def token "")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the is no need repeat what was there before

(def webhook "")

(defn just-say-hi [name]
(str "Hi " name "!"))

(defhandler thisismyapp
(message {:keys [text]} (just-say-hi text)))

(def therealhandler (q/direct-reply token thisismyapp))

(morse.api/set-webhook token (str webhook "/" token)) ;; make webhooks safe by using the token as path
(def myserver (http/start-server (q/req->morse token therealhandler) {:port 80}))
;; req->morse will convert json to edn and make sure the proper path (token) is used.
```


## License

Copyright © 2017 Anton Chebotaev
Expand Down
11 changes: 11 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{:paths ["src"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is quite confusing to have two sources of dependencies, because people would need to keep them both up to date

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about this PR as well, this was added to do some troubleshooting. I'll look into fixing up this PR again next weekend.

:deps {org.clojure/clojure {:mvn/version "1.9.0"}
org.clojure/tools.macro {:mvn/version "0.1.5"}
org.clojure/core.async {:mvn/version "0.4.474"}
org.clojure/tools.logging {:mvn/version "0.4.1"}
org.clojure/spec.alpha {:mvn/version "0.1.143"}
clj-stacktrace {:mvn/version "0.2.8"}
cheshire {:mvn/version "5.8.1"}
clj-http {:mvn/version "3.9.1"}}
:mvn/repos {"central" {:url "https://repo1.maven.org/maven2/"}
"clojars" {:url "https://repo.clojars.org/"}}}
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

:url "https://github.com/otann/morse/"

:dependencies [[org.clojure/clojure "1.8.0" :scope "provided"]
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/tools.macro "0.1.5"]
[org.clojure/core.async "0.4.474"]
[org.clojure/tools.logging "0.4.1"]
[org.clojure/spec.alpha "0.1.143"]
[clj-stacktrace "0.2.8"]
[cheshire "5.8.1"]
[clj-http "3.9.1"]]
Expand Down
24 changes: 23 additions & 1 deletion src/morse/api.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
(ns morse.api
(:require [clojure.tools.logging :as log]
(:require [clojure.spec.alpha :as s]
[clojure.tools.logging :as log]
[clj-http.client :as http]
[clojure.string :as string]
[cheshire.core :as json]
[clojure.core.async :as a])
(:import (java.io File)))

(s/def ::token
(s/and string?
(partial re-matches #"^\d{9}:.{35}")))

(s/def ::text string?)
(s/def ::chat-id int?)
(s/def ::parse-mode #{"Markdown" "HTML"})
(s/def ::disable-webpage-preview boolean?)

(s/def ::getMe nil?)
(s/def ::sendMessage
(s/keys :req [::chat-id ::text]
:opt [::parse-mode ::disable-webpage-preview]))

(s/def ::method
#{(s/tuple "getMe" ::getMe)
(s/tuple "sendMessage" ::sendMessage)})

(s/def ::request
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it used anywhere, maybe left here by mistake?

(s/keys :req [::token ::method]))


(def base-url "https://api.telegram.org/bot")

Expand Down
23 changes: 23 additions & 0 deletions src/morse/qcodes.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns morse.qcodes
(:require [compojure.core :refer [make-route]]
[clojure.core.async :as a]
[ring.middleware.json :refer [wrap-json-response wrap-json-body]]
[morse.api :as api]
[morse.polling :as pol]))

(defn- only [pred val]
(when (pred val)
val))

(defn req->morse [safe-url handler]
(make-route :post (str "/" safe-url)
(wrap-json-response
(wrap-json-body
(comp handler :body)
{:keywords? true :bigdecimals? true}))))

(defn direct-reply [token handler]
(fn [{{{chatid :id} :chat} :message :as req}]
(when-let [reply (only string? (handler req))]
(api/send-text token chatid reply))
{:status 200}))