Skip to content

Commit 033d6e5

Browse files
committed
Add new utility fns for dealing with bot tokens
1 parent 4ee2c1e commit 033d6e5

4 files changed

Lines changed: 35 additions & 11 deletions

File tree

src/marksto/clj_tg_bot_api/impl/client/core.clj

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
[marksto.clj-tg-bot-api.impl.api.martian :as api-martian]
1010
[marksto.clj-tg-bot-api.impl.client.rate-limiter :as rl]
1111
[marksto.clj-tg-bot-api.impl.utils :as utils]
12+
[marksto.clj-tg-bot-api.impl.utils.bot :as bot]
1213
[marksto.clj-tg-bot-api.impl.utils.response :as response])
1314
(:import (java.io Writer)))
1415

@@ -39,12 +40,6 @@
3940

4041
(def global-server-url "https://api.telegram.org/bot")
4142

42-
(defn parse-bot-id [bot-token]
43-
(try
44-
(parse-long (subs bot-token 0 (str/index-of bot-token \:)))
45-
(catch Exception ex
46-
(throw (ex-info "Failed to parse an ID from the bot auth token" {} ex)))))
47-
4843
(defn ->client
4944
[{:keys [bot-token server-url responses limiter-opts interceptors]
5045
:or {server-url global-server-url
@@ -60,7 +55,7 @@
6055
(-> (mt/respond-with responses)
6156
;; TODO: Drop this temp patch when Martian PR #243 is merged.
6257
(update :interceptors vec)))
63-
(assoc :bot-id (parse-bot-id bot-token)
58+
(assoc :bot-id (bot/parse-bot-id bot-token)
6459
:limiter-opts limiter-opts)
6560
(with-meta {:type ::tg-bot-api-client})))
6661

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(ns marksto.clj-tg-bot-api.impl.utils.bot
2+
(:require [clojure.string :as str]))
3+
4+
(defn parse-bot-id
5+
[bot-token]
6+
(try
7+
(parse-long (subs bot-token 0 (str/index-of bot-token \:)))
8+
(catch Exception ex
9+
(throw (ex-info "Failed to parse an ID from the bot auth token" {} ex)))))
10+
11+
(defn mask-bot-token
12+
[bot-token mask-str]
13+
(str (parse-bot-id bot-token) \: mask-str))

src/marksto/clj_tg_bot_api/utils.clj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
(ns marksto.clj-tg-bot-api.utils
22
"Some utilities for Telegram Bot API types, updates and responses"
3-
(:require [marksto.clj-tg-bot-api.impl.utils.types :as types]
3+
(:require [marksto.clj-tg-bot-api.impl.utils.bot :as bot]
4+
[marksto.clj-tg-bot-api.impl.utils.types :as types]
45
[marksto.clj-tg-bot-api.impl.utils.update :as update]
56
[marksto.clj-tg-bot-api.impl.utils.response :as response]))
67

8+
;;;; BOT
9+
10+
(defn parse-bot-id
11+
"Retrieves a Telegram Bot ID from a plain `bot-token` string.
12+
Useful during tests, e.g. when mocking the Telegram Bot API."
13+
[bot-token]
14+
(bot/parse-bot-id bot-token))
15+
16+
(defn mask-bot-token
17+
"Masks the sensitive portion of the given `bot-token` string."
18+
([bot-token]
19+
(bot/mask-bot-token bot-token "..."))
20+
([bot-token mask-str]
21+
(bot/mask-bot-token bot-token mask-str)))
22+
723
;;;; TYPES
824

925
;;; Date/Time

test/integration/marksto/clj_tg_bot_api/core_i9n_test.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(:require [clojure.string :as str]
33
[clojure.test :refer [deftest is testing use-fixtures]]
44
[marksto.clj-tg-bot-api.core :as sut]
5-
[marksto.clj-tg-bot-api.impl.client.core :as client]
5+
[marksto.clj-tg-bot-api.utils :as utils]
66
[marksto.clj-tg-bot-api.impl.api.martian :as api-martian]
77
[marksto.clj-tg-bot-api.net-utils :as net-utils]
88
[marksto.clj-tg-bot-api.vcr-utils :as vcr-utils]
@@ -11,10 +11,10 @@
1111

1212
(def real-bot-token (or (System/getenv "BOT_AUTH_TOKEN")
1313
(throw (ex-info "Missing BOT_AUTH_TOKEN env var" {}))))
14-
(def real-bot-id (client/parse-bot-id real-bot-token))
14+
(def real-bot-id (utils/parse-bot-id real-bot-token))
1515

1616
(def fake-bot-token "1234567890:TEST_pxWA8lDi7uLc3oadqNivHCALHBQ7sM")
17-
(def fake-bot-id (client/parse-bot-id fake-bot-token))
17+
(def fake-bot-id (utils/parse-bot-id fake-bot-token))
1818

1919
(def replacements
2020
[[:string-val real-bot-token fake-bot-token]

0 commit comments

Comments
 (0)