-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37cdaed
commit 67db9e0
Showing
11 changed files
with
157 additions
and
194 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
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 |
---|---|---|
@@ -1,14 +1,110 @@ | ||
(ns tdlib-json-clojure-wrapper.core | ||
(:require [tdlib-json-clojure-wrapper.tg-connector.core :as conn] | ||
[tdlib-json-clojure-wrapper.tg-connector.handlers :as handlers]) | ||
(:import (java.util.concurrent CountDownLatch)) | ||
(:gen-class)) | ||
|
||
(defn -main | ||
"i start client connection and message handling" | ||
[& args] | ||
(let [signal (CountDownLatch. 1)] | ||
(println "Disable autostart if you are encountering problems") | ||
(conn/start-telegram) | ||
(handlers/mq-handler) | ||
(.await signal))) | ||
(:require [cheshire.core :as che] | ||
[clojure.core.async :as async]) | ||
(:import (tdlib_json_clojure_wrapper TgClient))) | ||
|
||
(def client (atom nil)) | ||
|
||
(defn create-client [path-to-lib] | ||
(TgClient. path-to-lib)) | ||
|
||
;; Telegram uses this format of keys | ||
(def ttype (keyword "@type")) | ||
|
||
(def telegram-ready-state (atom false)) | ||
|
||
(def message-queue (async/chan)) | ||
|
||
(defn jsonify [hash] (che/generate-string hash)) | ||
|
||
(defn json-parse [string] | ||
(che/parse-string string)) | ||
|
||
(defn client-execute | ||
([type] (. @client execute (jsonify {ttype type}))) | ||
;; if has some option to command | ||
([type key command] | ||
(. @client execute | ||
(jsonify | ||
{ttype type (keyword key) command})))) | ||
|
||
(defn client-receive | ||
"argument is timeout in seconds and it has to be double" | ||
[] | ||
(json-parse (. @client receive 0.3))) | ||
|
||
(defn client-destroy [] | ||
(reset! telegram-ready-state false) | ||
(. @client destroyClient) | ||
(reset! client nil)) | ||
|
||
(defn client-send | ||
([type] | ||
(. @client send (jsonify {ttype type}))) | ||
;; if we want to log out there is slightly different behaviour | ||
;; it does not just stop server, it clears our data from telegram and you will need to log in next time | ||
([type logout] (. @client send (jsonify {ttype type}) logout)) | ||
([type key message] | ||
(. @client send | ||
(jsonify | ||
{ttype type (keyword key) message})))) | ||
|
||
(defn send-phone [phone-number] | ||
(client-send "setAuthenticationPhoneNumber" "phone_number" phone-number)) | ||
|
||
(defn get-and-send-phone [] | ||
(println "Enter your phone number") | ||
(let [phone (read-line)] | ||
(send-phone phone))) | ||
|
||
(defn send-auth-code [code] | ||
(client-send "checkAuthenticationCode" "code" code)) | ||
|
||
(defn get-and-send-code [] | ||
(println "Enter your code") | ||
(let [code (read-line)] | ||
(send-auth-code code))) | ||
|
||
(defn client-start [path-to-lib] | ||
(reset! client (create-client path-to-lib)) | ||
(. @client startClient)) | ||
|
||
(defn init-reader-loop [] | ||
(async/go-loop [] | ||
(let [message | ||
(try | ||
(client-receive) | ||
(catch Exception e (str "caught " (.getMessage e) "exception")))] | ||
(if-not | ||
(nil? message) | ||
(async/>! message-queue message)) | ||
(recur)))) | ||
|
||
(defn start-service [] | ||
(reset! telegram-ready-state true) | ||
(init-reader-loop)) | ||
|
||
(defn start-telegram [path-to-lib config] | ||
(when-not (nil? path-to-lib) | ||
(client-start path-to-lib) | ||
(loop [message nil] | ||
(let [state (get-in message ["authorization_state", "@type"])] | ||
(if-not (= state "authorizationStateReady") | ||
(cond | ||
(= state "authorizationStateWaitTdlibParameters") | ||
(do | ||
(client-send ((keyword "@type") config) :parameters (:parameters config)) | ||
(recur (client-receive))) | ||
(= state "authorizationStateWaitEncryptionKey") | ||
(do | ||
(client-send "checkDatabaseEncryptionKey") | ||
(recur (client-receive))) | ||
(= state "authorizationStateWaitPhoneNumber") | ||
(do (get-and-send-phone) | ||
(recur (client-receive))) | ||
(= state "authorizationStateWaitCode") | ||
(do (get-and-send-code) | ||
(recur (client-receive))) | ||
:else | ||
(recur (client-receive))) | ||
(start-service)))))) |
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,35 @@ | ||
(ns tdlib-json-clojure-wrapper.example | ||
(:require [tdlib-json-clojure-wrapper.core :as c] | ||
[clojure.core.async :as async] | ||
[clojure.pprint :as pp]) | ||
(:import (java.util.concurrent CountDownLatch))) | ||
(def config {(keyword "@type") "setTdlibParameters" | ||
:parameters {:api_id "your application api", | ||
:api_hash "your application hash" | ||
:application_version "0.1" | ||
:system_version "Ubuntu 18.04", | ||
:system_language_code "en", | ||
:device_model "PC", | ||
:database_directory "tg-db"}}) | ||
|
||
(defn mq-handler | ||
"Loops through all incoming messages and applies your logic" | ||
[] | ||
(async/go-loop [] | ||
(let [message (async/<! c/message-queue)] | ||
(pp/pprint message)) | ||
(recur))) | ||
|
||
(defn start-tg | ||
"Start client connection and message handling" | ||
[path-to-lib] | ||
(let [signal (CountDownLatch. 1)] | ||
(c/start-telegram path-to-lib config) | ||
(mq-handler) | ||
(.await signal))) | ||
|
||
(defn get-auth-state [] (c/client-send "getAuthorizationState")) | ||
|
||
(defn log-out | ||
"logout is a very special case, its the only function that provides client-send with 2 arguments" | ||
[] (c/client-send "logOut" true)) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.