Skip to content

Commit

Permalink
Moved auth loop out of lib
Browse files Browse the repository at this point in the history
  • Loading branch information
MityaSaray committed May 22, 2019
1 parent 03337d0 commit 0d2cb19
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 171 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pom.xml.asc
.idea/
/doc
tdlib-json-wrapper.iml
src/main.tdlib_json_clojure_wrapper/tg_connector/build
*.log
*.binlog
tg-db
6 changes: 3 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(defproject tdlib-json-wrapper "0.1.4"
:description "main.tdlib_json_clojure_wrapper"
:url "https://github.com/MityaSaray/clojure-tdlib-json-wrapper"
(defproject tdlib-json-wrapper "0.2.0"
:description "tdlib_json"
:url "https://github.com/MityaSaray/clojure-tdlib-json"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.10.0"]
Expand Down
24 changes: 9 additions & 15 deletions src/main/java/TgClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@
import com.sun.jna.Native;
import com.sun.jna.Pointer;

import java.util.Objects;

public class TgClient {
private static String pathToLib;
private static Pointer tgInstancePointer;
private static String errorMessage = "Tdlib client was null";
private TdLibrary client;
private double timeout;

public TgClient(String path) {
System.out.println(path);
public TgClient(String path, double timeout) {
pathToLib = path;
this.timeout = timeout;
}

public void startClient() {
public void startClient(Integer verbosityLevel) {
if (tgInstancePointer == null) {
client = TdLibrary.INSTANCE;
tgInstancePointer = client.td_json_client_create();
client.td_set_log_verbosity_level(2);
}
}

public void send(String message) {
System.out.println(message);
if (tgInstancePointer != null) {
client.td_json_client_send(tgInstancePointer, message);
} else {
throw new Error(errorMessage);
client.td_set_log_verbosity_level(Objects.requireNonNullElse(verbosityLevel, 2));
}
}

Expand All @@ -43,9 +37,9 @@ public void send(String message, boolean logout) {
}
}

public String receive(double timeout) {
public String receive() {
if (tgInstancePointer != null) {
return client.td_json_client_receive(tgInstancePointer, timeout);
return client.td_json_client_receive(tgInstancePointer, this.timeout);
} else {
throw new Error(errorMessage);
}
Expand Down
49 changes: 49 additions & 0 deletions src/main/tdlib_json/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(ns main.tdlib_json.core
(:require [cheshire.core :as che]
[clojure.core.async :as async])
(:import [main.java TgClient]))

(def client (atom nil))

(defn create-client [path-to-lib timeout]
(TgClient. path-to-lib timeout))

(def message-queue (async/chan))

(defn jsonify [hash] (che/generate-string hash))

(defn json-parse [string]
(che/parse-string string))

(defn client-execute
([hash] (. @client execute (jsonify hash))))

(defn client-receive
"argument is timeout in seconds and it has to be double"
[]
(json-parse (. @client receive)))

(defn client-destroy []
(. @client destroyClient)
(reset! client nil))

(defn client-send
"If we want to logout we send additional argument that will delete pointer to tdlib client"
([hash]
(. @client send (jsonify hash) false))
([hash logout]
(. @client send (jsonify hash) logout)))

(defn client-start [path-to-lib timeout verbosity-level]
(reset! client (create-client path-to-lib timeout))
(. @client startClient verbosity-level))

(defn init-reader-loop []
(async/go-loop []
(let [message (client-receive)]
(when-not
(nil? message)
(async/>! message-queue message))
(recur))))


98 changes: 98 additions & 0 deletions src/main/tdlib_json/example.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
(ns main.tdlib_json.example
(:require [main.tdlib_json.core :as c]
[clojure.core.async :as async]
[clojure.pprint :as pp])
(:import (java.util.concurrent CountDownLatch)))


(def ttype (keyword "@type"))

(def config {ttype "setTdlibParameters"
:parameters {:api_id "your api id",
:api_hash "your api hash"
:application_version "0.1"
:system_version "Ubuntu 18.04",
:system_language_code "en",
:device_model "PC",
:database_directory "tg-db"}})


(defn get-auth-state [] (c/client-send {ttype "getAuthorizationState"}))

(defn get-contacts []
(c/client-send {ttype "getContacts"}))

(defn send-phone [phone-number]
(c/client-send {ttype "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]
(c/client-send {ttype "checkAuthenticationCode" :code code}))

(defn get-and-send-code []
(println "Enter your code")
(let [code (read-line)]
(send-auth-code code)))

(defn get-chats []
(c/client-send {ttype "getChats" :limit 1}))

(defn send-text-message
;; wrap anything you want like this
[chat-id message]
(c/client-send {ttype "sendMessage"
:chat_id chat-id
:input_message_content {ttype "inputMessageText"
:text {ttype "formattedText"
:text message}}}))

(defn log-out
[] (c/client-send {ttype "logOut"} true))

(defn resolve-auth [message]
(let [state (get-in message ["authorization_state", "@type"])]
(cond
(= state "authorizationStateWaitTdlibParameters")
(c/client-send config)
(= state "authorizationStateWaitEncryptionKey")
(c/client-send {ttype "checkDatabaseEncryptionKey"})
(= state "authorizationStateWaitPhoneNumber")
((get-and-send-phone))
(= state "authorizationStateWaitCode")
(get-and-send-code))))

(defn mq-handler
"Loops through all incoming messages and applies your logic"
[]
(async/go-loop []
(let [message (async/<! c/message-queue)
type (get message "@type")]
(cond
(= type "updateAuthorizationState")
(resolve-auth message)
(= type "updateNewMessage")
(pp/pprint message)))
(recur)))

;; Absolute path to tdlibjson.so and timeout is a double that sets timeout in receive method of tdlib
;; Verbosity level is a param sent to tdlib
(defn start-telegram [path-to-lib timeout & verbosity-level]
(when-not (and (nil? path-to-lib) (nil? timeout))
(c/client-start path-to-lib timeout (first verbosity-level))
(c/init-reader-loop)
(mq-handler)))

(defn start-tg
"Start client connection and message handling, will block input on repl"
[path-to-lib timeout]
(let [signal (CountDownLatch. 1)]
(start-telegram path-to-lib timeout)
(.await signal)))




110 changes: 0 additions & 110 deletions src/main/tdlib_json_clojure_wrapper/core.clj

This file was deleted.

35 changes: 0 additions & 35 deletions src/main/tdlib_json_clojure_wrapper/example.clj

This file was deleted.

7 changes: 0 additions & 7 deletions test/main/tdlib_json_clojure_wrapper/core_test.clj

This file was deleted.

7 changes: 7 additions & 0 deletions test/tdlib_json/core_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns tdlib-json.core-test
(:require [clojure.test :refer :all]
[tdlib-json.example :refer :all]))

(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))

0 comments on commit 0d2cb19

Please sign in to comment.