-
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
03337d0
commit 0d2cb19
Showing
9 changed files
with
166 additions
and
171 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,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)))) | ||
|
||
|
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,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))) | ||
|
||
|
||
|
||
|
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 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,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)))) |