-
Notifications
You must be signed in to change notification settings - Fork 48
Q-Code #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Q-Code #29
Changes from all commits
cd5a511
5ba98ea
5f16dba
bf4f479
089bc2c
14a1005
6783924
f8615d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| (:require [morse.qcodes :as q] | ||
| [morse.handlers :as h] | ||
| [somehttpserver :as http])) | ||
|
|
||
| (def token "") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| {:paths ["src"] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/"}}} | ||
| 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
|
|
||
|
|
||
| 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})) |
There was a problem hiding this comment.
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