(ns ^{:doc "Send events to Telegram"}
riemann.telegram
(:require [clj-http.client :as client]
[clojure.string :refer [escape join]]))
(defn- html-parse-mode []
"Formats html message."
(fn [e, my_message_to_bot]
(str
"<strong>Host:</strong> " (or (:host e) "-") "\n"
"<strong>Service:</strong> " (or (:service e) "-") "\n"
"<strong>State:</strong> " (or (:state e) "-") "\n"
"<strong>Metric:</strong> " (or (:metric e) "-") "\n"
;"<strong>Description:</strong> " (or (:description e) "-")
"<strong>My message to bot:</strong> " (or (:my_message_to_bot my_message_to_bot) "-")))) ; don't want the full event description, only my custom message
(defn- markdown-parse-mode []
"Formats markdown message."
(fn [e, my_message_to_bot]
(str
"*Host:* " (or (:host e) "-") "\n"
"*Service:* " (or (:service e) "-") "\n"
"*State:* " (or (:state e) "-") "\n"
"*Metric:* " (or (:metric e) "-") "\n"
;"*Description:* " (or (:description e) "-")
"*My message to bot:* " (or (:my_message_to_bot my_message_to_bot) "-")))) ; don't want the full event description, only my custom message
(defn- format-message [parse-mode event my_message_to_bot] ; add the my_message_to_bot param
"Formats a message, accepts a single
event or a sequence of events."
(join "\n\n"
(map
(if (re-matches #"(?i)html" parse-mode)
(html-parse-mode)
(markdown-parse-mode))
(flatten [event, my_message_to_bot])))) ; not really sure if I can add the my_message_to_bot param here to flatten
(defn- post
"POST to the Telegram API."
[token chat_id event parse_mode]
(client/post (format api-url token "sendMessage")
{:form-params {:chat_id chat_id
:parse_mode (or parse_mode "markdown")
:text (format-message parse_mode event)}
:throw-entire-message? true}))
(defn telegram
"
; defining my custom event string to send to the chat bot
(def my_event_str (str "You have an event in host " :host " in timestamp: " :timestamp))