Some additional middleware provided are:
And part of tg-clj-server.defaults
we have:
Logs the request & response for every request, useful for debugging.
Optionally takes a name
which is prepended to the log line.
(-> handler
log/middleware)
; In a `defaults` app:
(defaults/make-app routes {:middleware [log/middleware]})
; Optionally supply a name
(-> handler
(log/middleware "pre-middleware1"))
; In a `defaults` app:
(defaults/make-app routes {:middleware [[log/middleware "pre-middleware1"]]})
; Log outputs look something like:
; INFO: Request(pre-middleware1): {<the request>}
; ... some other log lines
; INFO: Response(pre-middleware1): {<the response>}
There are a few ways to control access to routes/commands on a bot:
- Manually checking the
:status
of a user (be careful of users messaging the bot in other chats) - Set the
:scope
of commands when setting them up (same warning as above) - Have a pool of users who are admins for the bot regardless of chat
- Something else
This middleware helps with the third option by having a global pool of admins.
By including :admin-only true
in the route data for a route you can ensure that only admins in the global list will be allowed to use that route:
(defn public-handler [_]
...)
(defn secret-handler [_]
...)
(def routes
{"/public" #'public-handler
"/secret" {:handler #'secret-handler
:admin-only true}})
(defaults/make-app routes {:middleware [admin/middleware]})
The admin list is split into two parts: base admins
and stored admins
.
Base admins
are provided statically a set on the middleware and can only be removed by changing this list and restarting the app:
(defaults/make-app routes {:middleware [[admin/middleware #{"me" 1234}]]})
And stored admins
are stored at [:store :admins]
. Use the simple store
to persist this list of admins.
A set of routes is provided at tg-clj-server.middleware.middleware/routes
:
(def routes
(merge {...} admin/routes))
; For a list of routes:
(def routes
(concat [...] admin/routes))
These commands are:
/admin_list
- List the stored admins (not thebase admins
)/admin_add
- Add an admin to thestored admins
/admin_remove
- Remove and admin from thestored admins
NOTE: These routes make use of the invoke
middleware to reply to users, but other than the /admin_list
command this isn't required
See /examples/with_admin.clj
for a complete example.
See the defaults docs for a description.
Usage of the raw middleware:
(defn handler [_]
{:op :sendMessage
:request {:chat_id 1234
:text "hi"}})
(-> handler
invoke/middleware)
; Optionally supply a retry function
; Retries on client errors, not telegram api errors
; Defaults to retrying 3 times every 100ms
(-> handler
(invoke/middleware {:retry #(u/retry % {:max-retries 4})}))
See the defaults docs for a description.
Usage of the raw middleware:
(defn handler [{:keys [store]}]
(println store)
{:set-store (assoc store :a 1)})
(-> handler
store/middleware)
; Optionally supply a path
(-> handler
(store/middleware {:path "/tmp/file.edn"}))
; Optionally supply an atom
(def my-store (atom nil))
(def app
(-> handler
(store/middleware {:atom my-store})))
(app {})
@my-store ; => {:a 1}
See the defaults docs for a description.
Usage of the raw middleware:
(defn handler [{:keys [me]}]
...)
(-> handler
me/middleware)