Line data Source code
1 4 : (ns matchscore.telegram.messages
2 : (:require
3 : [clojure.string :as str]
4 : [telegrambot-lib.core :as tbot]))
5 :
6 :
7 4 : (defn request!
8 : "The Telegram client normally RETURNS API failures instead of throwing them.
9 : Only expose sanitized error fields to callers/logs (never the token-bearing URL)."
10 : [operation bot params]
11 8 : (let [response (try
12 16 : (operation bot params)
13 6 : (catch Exception _ {:description "Telegram transport failure"}))]
14 24 : (if (true? (:ok response))
15 4 : response
16 4 : (throw
17 12 : (ex-info
18 : "Telegram request failed"
19 28 : (select-keys response [:error_code :description :parameters]))))))
20 :
21 :
22 4 : (defn error-matches?
23 : [exception description]
24 12 : (let [data (ex-data exception)]
25 14 : (and
26 15 : (= 400 (:error_code data))
27 33 : (str/includes? (str/lower-case (or (:description data) "")) description))))
28 :
29 :
30 4 : (defn edit!
31 : "Returns :ok (including unchanged text), or :missing. Other failures are retryable
32 : by the job and must never cause a duplicate post."
33 : [bot params]
34 6 : (try
35 15 : (request! tbot/edit-message-text bot params)
36 : :ok
37 : (catch clojure.lang.ExceptionInfo e
38 14 : (cond (error-matches? e "message is not modified") :ok
39 8 : (error-matches? e "message to edit not found") :missing
40 4 : :else (throw e)))))
|