<commit_before>(ns consolelifebot.telegram
  (:require [consolelifebot.configuration :as configuration]
            [org.httpkit.client :as http]
            [clojure.data.json :as json]
            [clojure.tools.logging :as log]))

(def base-url (str "https://api.telegram.org/bot"
                   configuration/telegram-bot-api-token))

(defn- get-updates [offset]
  (http/get (str base-url "/getUpdates")
            {:query-params {:offset offset
                            :limit 100
                            :timeout 60}
             :keep-alive 60000}
            (fn [{:keys [status headers body error]}]
              (:result (json/read-str body :key-fn keyword)))))

(defn- post [method body]
  (log/info "[telegram] post " method " " body) 
  (http/post (str base-url method)
             {:headers {"Content-Type" "application/json"}
              :body (json/write-str body)}))

(defn post-message [& {with-text :with-text at :at disable-preview :disable-preview}]
  (post "/sendMessage" {:chat_id at
                        :text with-text
                        :parse_mode "HTML"
                        :disable_web_page_preview (or disable-preview false)}))

(defn reply-with-message [& {with-text :with-text to :to at :at}]
  (post "/sendMessage" {:chat_id at
                        :reply_to_message_id to
                        :text with-text
                        :parse_mode "HTML"
                        :disable_web_page_preview true}))

(defn for-each-new-message [handle]
  (future
    (log/info "[telegram] start")
    (loop [offset 0]
      (let [updates @(get-updates offset)
            next-offset (if (seq updates)
                          (-> updates last :update_id inc)
                          0)]
        (when (seq updates)
          (doseq [message (map :message updates)]
            (log/info "[telegram] receive " message)
            (future
                (handle message))))
        (when (not (Thread/interrupted))
          (recur next-offset))))))

<commit_msg>handle telegram polling errors #9<commit_after>(ns consolelifebot.telegram
  (:require [consolelifebot.configuration :as configuration]
            [org.httpkit.client :as http]
            [clojure.data.json :as json]
            [clojure.tools.logging :as log]))

(def base-url (str "https://api.telegram.org/bot"
                   configuration/telegram-bot-api-token))

(defn- get-updates [offset]
  (http/get (str base-url "/getUpdates")
            {:query-params {:offset offset
                            :limit 100
                            :timeout 60}
             :keep-alive 60000}
            (fn [{:keys [status headers body error]}]
              (println status)
              (if (not= status 200)
                (do
                  (log/error body)
                  (Thread/sleep 500)
                  [])
                (:result (json/read-str body :key-fn keyword))))))

(defn- post [method body]
  (log/info "[telegram] post " method " " body) 
  (http/post (str base-url method)
             {:headers {"Content-Type" "application/json"}
              :body (json/write-str body)}))

(defn post-message [& {with-text :with-text at :at disable-preview :disable-preview}]
  (post "/sendMessage" {:chat_id at
                        :text with-text
                        :parse_mode "HTML"
                        :disable_web_page_preview (or disable-preview false)}))

(defn reply-with-message [& {with-text :with-text to :to at :at}]
  (post "/sendMessage" {:chat_id at
                        :reply_to_message_id to
                        :text with-text
                        :parse_mode "HTML"
                        :disable_web_page_preview true}))

(defn for-each-new-message [handle]
  (future
    (log/info "[telegram] start")
    (loop [offset 0]
      (let [updates @(get-updates offset)
            next-offset (if (seq updates)
                          (-> updates last :update_id inc)
                          0)]
        (when (seq updates)
          (doseq [message (map :message updates)]
            (log/info "[telegram] receive " message)
            (future
                (handle message))))
        (when (not (Thread/interrupted))
          (recur next-offset))))))


{'fraction_non_alphanumeric': 0.12435476302205538, 'fraction_numerical': 0.007273580478648522, 'mean_word_length': 1.6363636363636365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2246394', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1192, 'n_words': 313}