Skip to content

Commit

Permalink
Merge pull request #105 from macielti/telegram-producer-component
Browse files Browse the repository at this point in the history
Telegram producer component
  • Loading branch information
macielti committed Nov 26, 2023
2 parents c5e5080 + 9c0c710 commit de1ae16
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ of [keepachangelog.com](http://keepachangelog.com/).

## [Unreleased]

## [24.47.47] - 2023-11-26

## Added

- Add Telegram producer component in order to make easier to write integration tests for telegram bots.

## [24.46.47] - 2023-11-25

### Changed
Expand Down Expand Up @@ -629,7 +635,9 @@ of [keepachangelog.com](http://keepachangelog.com/).

- Add `loose-schema` function.

[Unreleased]: https://github.com/macielti/common-clj/compare/v24.46.47...HEAD
[Unreleased]: https://github.com/macielti/common-clj/compare/v24.47.47...HEAD

[24.47.47]: https://github.com/macielti/common-clj/compare/v24.46.47...v24.47.47

[24.46.47]: https://github.com/macielti/common-clj/compare/v23.46.47...v24.46.47

Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject net.clojars.macielti/common-clj "24.46.47"
(defproject net.clojars.macielti/common-clj "24.47.47"
:description "Just common Clojure code that I use across projects"
:url "https://github.com/macielti/common-clj"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand Down
7 changes: 4 additions & 3 deletions src/common_clj/component/service.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[io.pedestal.http :as http]
[plumbing.core :as plumbing]))

(defrecord Service [routes config datomic datalevin postgresql rabbitmq-producer producer http-client prometheus rate-limiter]
(defrecord Service [routes config datomic datalevin postgresql rabbitmq-producer producer http-client prometheus rate-limiter telegram-producer]
component/Lifecycle
(start [component]
(let [{{{:keys [host port]} :service} :config} config
Expand All @@ -22,7 +22,8 @@
:postgresql (:postgresql postgresql)
:http-client (:http-client http-client)
:prometheus (:prometheus prometheus)
:rate-limiter (:rate-limiter rate-limiter))]
:rate-limiter (:rate-limiter rate-limiter)
:telegram-producer (:telegram-producer telegram-producer))]
(assoc component :service (http/start (-> service-map
http/default-interceptors
(update ::http/interceptors concat (io.interceptors/common-interceptors
Expand All @@ -33,4 +34,4 @@
(assoc component :service nil)))

(defn new-service []
(->Service {} {} {} {} {} {} {} {} {} {}))
(->Service {} {} {} {} {} {} {} {} {} {} {}))
41 changes: 41 additions & 0 deletions src/common_clj/component/telegram/producer.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(ns common-clj.component.telegram.producer
(:require [com.stuartsierra.component :as component]
[medley.core :as medley]
[morse.api :as morse-api]
[schema.core :as s]))

(s/defschema SendTextPayload
{:chat-id s/Str
:text s/Str
(s/optional-key :options) (s/pred map?)})

(defmulti send-text!
(fn [_ {:keys [current-env]}]
current-env))

(s/defmethod send-text! :prod
[{:keys [chat-id text options]
:or {options {}}} :- SendTextPayload
{:keys [token]}]
(morse-api/send-text token chat-id options text))

(s/defmethod send-text! :test
[payload :- SendTextPayload
{:keys [produced]}]
(swap! produced conj payload))

(defrecord TelegramProducer [config]
component/Lifecycle
(start [component]
(let [{{:keys [telegram] :as config-content} :config} config]
(assoc component :telegram-producer (medley/assoc-some {:token (:token telegram)
:current-env (:current-env config-content)}
:produced (when (= (:current-env config-content) :test)
(atom []))))))

(stop [component]
component))

(defn new-telegram-producer
[]
(->TelegramProducer {}))
28 changes: 28 additions & 0 deletions test/integration/integration/telegram_producer_component_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns integration.telegram-producer-component-test
(:require [clojure.test :refer :all]
[com.stuartsierra.component :as component]
[common-clj.component.config :as component.config]
[common-clj.component.helper.core :as component.helper]
[common-clj.component.telegram.producer :as component.telegram.producer]
[schema.test :as s]))

(def system-test
(component/system-map
:config (component.config/new-config "resources/config_test.json" :test :json)
:telegram-producer (component/using (component.telegram.producer/new-telegram-producer) [:config])))

(s/deftest telegram-consumer-component-test
(let [system (component/start system-test)
{:keys [produced] :as producer} (component.helper/get-component-content :telegram-producer system)]

(testing "That we can consume a telegram bot command"
(component.telegram.producer/send-text! {:chat-id "123456789"
:text "Hello World"
:options {:test-option :text}} producer)

(is (= [{:chat-id "123456789"
:options {:test-option :text}
:text "Hello World"}]
@produced)))

(component/stop system)))

0 comments on commit de1ae16

Please sign in to comment.