-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from macielti/telegram-producer-component
Telegram producer component
- Loading branch information
Showing
5 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
test/integration/integration/telegram_producer_component_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |