-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.clj
104 lines (82 loc) · 3.28 KB
/
example.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
(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 close []
(c/client-send {ttype "close"}))
(defn log-out []
(c/client-send {ttype "logOut"}))
(defn resolve-auth [message]
(let [state (get-in message [:authorization_state, ttype])]
(cond
(#{"authorizationStateClosed" "authorizationStateLoggingOut"} state)
(c/client-destroy)
(= 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 ttype)]
(cond
(= type "updateAuthorizationState")
(resolve-auth message)
(= type "updateNewMessage")
(pp/pprint message)))
(when @c/client
(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)))