Line data Source code
1 4 : (ns matchscore.application.event.telegram-invite
2 : "Creates short-lived Telegram chat invites for registered event participants.
3 :
4 : Invite creation is deliberately best-effort: joining an event must not be
5 : rolled back because Telegram is unavailable or the bot lacks chat rights."
6 : (:require
7 : [clojure.string :as str]
8 : [matchscore.domain.event.repository.postgres :as event-repo]
9 : [matchscore.domain.telegram-link.query :as telegram-link-query]
10 : [taoensso.telemere :as t]
11 : [telegrambot-lib.core :as tbot])
12 : (:import
13 : (java.time Instant)))
14 :
15 :
16 8 : (def ^:private invite-ttl-seconds 3600)
17 :
18 :
19 4 : (defn- permission-error?
20 : [response]
21 8 : (let [description (str/lower-case (str (:description response)))]
22 19 : (or
23 4 : (str/includes? description "not enough rights")
24 4 : (str/includes? description "administrator rights")
25 4 : (str/includes? description "chat_admin_required")
26 4 : (str/includes? description "chat not found")
27 4 : (str/includes? description "bot is not a member")
28 2 : (str/includes? description "bot is not an administrator"))))
29 :
30 :
31 4 : (defn- unavailable
32 : [link status message]
33 5 : (cond->
34 4 : {:status status
35 1 : :message message}
36 7 : (:chat-title link) (assoc :chat-title (:chat-title link))))
37 :
38 :
39 4 : (defn create-for-event!
40 : "Returns Telegram invite metadata for an event.
41 :
42 : Statuses:
43 : - ready: a single-use, one-hour invite was created;
44 : - not-linked: the event/template has no linked Telegram chat;
45 : - host-action-required: the bot is missing membership/admin invite rights;
46 : - unavailable: Telegram or bot configuration is temporarily unavailable."
47 : [{:keys [repos]
48 : :as ctx}
49 : bot
50 : event-id]
51 20 : (let [event (event-repo/find-by-id (:event-postgres-repo repos) event-id {:joins #{}})
52 18 : link (first (when event (telegram-link-query/resolve-links-for-event ctx event)))]
53 4 : (cond
54 8 : (nil? link)
55 10 : {:status "not-linked"
56 : :message "No Telegram chat is linked to this event."}
57 :
58 4 : (nil? bot) (unavailable link "unavailable" "Telegram invitations are temporarily unavailable.")
59 :
60 : :else
61 1 : (try
62 5 : (let [expires-at (.plusSeconds (Instant/now) invite-ttl-seconds)
63 2 : response (tbot/create-chat-invite-link
64 1 : bot
65 10 : {:chat_id (:chat-id link)
66 : :name "MatchScore tournament"
67 1 : :expire_date (.getEpochSecond expires-at)
68 : :member_limit 1})]
69 18 : (if (and (true? (:ok response)) (seq (get-in response [:result :invite_link])))
70 5 : (cond->
71 5 : {:status "ready"
72 6 : :invite-link (get-in response [:result :invite_link])
73 3 : :expires-at (str expires-at)}
74 7 : (:chat-title link) (assoc :chat-title (:chat-title link)))
75 1 : (do
76 92 : (t/log!
77 : :warn
78 7 : {:message "Failed to create Telegram event invite"
79 1 : :event-id event-id
80 3 : :chat-id (:chat-id link)
81 3 : :error-code (:error_code response)
82 3 : :description (:description response)})
83 4 : (if (permission-error? response)
84 4 : (unavailable
85 1 : link
86 : "host-action-required"
87 : "The Telegram bot cannot invite members. Ask the event host to add you.")
88 4 : (unavailable
89 1 : link
90 : "unavailable"
91 : "Telegram invitations are temporarily unavailable.")))))
92 : (catch Exception e
93 0 : (t/log!
94 : :warn
95 0 : {:message "Telegram event invite creation failed"
96 0 : :event-id event-id
97 0 : :chat-id (:chat-id link)
98 0 : :exception e})
99 0 : (unavailable link "unavailable" "Telegram invitations are temporarily unavailable."))))))
|