@@ -2,102 +2,129 @@ module Telegram
2
2
using HTTP
3
3
using JSON3
4
4
5
- export TelegramClient
5
+ export TelegramClient, useglobally!
6
6
export getMe, sendMessage
7
7
8
- const DEFAULT_OPTS = Dict ( :chat_id => " " ,
9
- :parse_mode => " MarkdownV2 " ,
10
- )
8
+ struct TelegramError{T} <: Exception
9
+ msg :: T
10
+ end
11
11
12
12
mutable struct TelegramClient
13
13
token:: String
14
14
chat_id:: String
15
15
parse_mode:: String
16
+ end
16
17
17
- function TelegramClient (token; kwargs... )
18
- c = new ()
19
- c. token = token
20
- for (k, v) in DEFAULT_OPTS
21
- setfield! (c, k, get (kwargs, k, v))
22
- end
23
- return c
24
- end
18
+ function TelegramClient (token; kwargs... )
19
+ chat_id = get (kwargs, :chat_id , " " )
20
+ parse_mode = get (kwargs, :parse_mode , " " )
21
+ return TelegramClient (token, chat_id, parse_mode)
22
+ end
23
+
24
+ mutable struct TelegramOpts
25
+ client:: TelegramClient
26
+ upload_kw:: Vector{Symbol}
27
+ timeout:: Int
28
+ end
29
+
30
+ const DEFAULT_OPTS = TelegramOpts (TelegramClient (" " , " " , " " ), [:photo , :audio , :thumb , :docuemnt , :video , :animation , :voice , :video_note , :sticker , :png_sticker , :tgs_sticker , :certificate , :files ], 100 )
31
+
32
+ function useglobally! (client:: TelegramClient )
33
+ DEFAULT_OPTS. client = client
34
+
35
+ return client
25
36
end
26
37
27
38
token (client:: TelegramClient ) = client. token
28
39
40
+ ioify (elem:: IO ) = elem
41
+ ioify (elem) = IOBuffer (elem)
42
+ function ioify (elem:: IOBuffer )
43
+ seekstart (elem)
44
+ return elem
45
+ end
46
+
47
+ pushfiles! (body, keyword, file) = push! (body, keyword => file)
48
+ function pushfiles! (body, keyword, file:: Pair )
49
+ push! (body, keyword => HTTP. Multipart (file. first, ioify (file. second)))
50
+ end
51
+
52
+ process_params (x:: AbstractString ) = x
53
+ process_params (x) = JSON3. write (x)
54
+
29
55
function query (client:: TelegramClient , method; params = Dict ())
30
56
req_uri = " https://api.telegram.org/bot" * token (client) * " /" * method
31
- headers = [" Content-Type" => " application/json" ]
32
- json_params = JSON3. write (params)
33
- json_params = replace (json_params, " ." => " \\ ." )
34
- json_params = replace (json_params, " !" => " \\ !" )
35
- JSON3. read (String (HTTP. post (req_uri, headers, json_params). body))
57
+ intersection = intersect (keys (params), DEFAULT_OPTS. upload_kw)
58
+ if isempty (intersection)
59
+ headers = [" Content-Type" => " application/json" , " Connection" => " Keep-Alive" ]
60
+ json_params = JSON3. write (params)
61
+ res = HTTP. post (req_uri, headers, json_params; readtimeout = DEFAULT_OPTS. timeout + 1 )
62
+ else
63
+ body = Pair[]
64
+ for (k, v) in params
65
+ k in intersection && continue
66
+ push! (body, k => process_params (v))
67
+ end
68
+ for k in intersection
69
+ pushfiles! (body, k, params[k])
70
+ end
71
+ res = HTTP. post (req_uri, [" Connection" => " Keep-Alive" ], HTTP. Form (body); readtimeout = DEFAULT_OPTS. timeout + 1 )
72
+ end
73
+
74
+ response = JSON3. read (res. body)
75
+
76
+ if response. ok
77
+ return response. result
78
+ else
79
+ throw (TelegramError (response))
80
+ end
36
81
end
37
82
38
- getMe (client:: TelegramClient ) = query (client, " getMe" )
83
+ # getMe(client::TelegramClient = DEFAULT_OPTS.client ) = query(client, "getMe")
39
84
40
- function sendMessage (client:: TelegramClient ; kwargs... )
41
- sendMessage (client, Dict (kwargs))
42
- end
85
+ # function sendMessage(client::TelegramClient = DEFAULT_OPTS.client ; kwargs...)
86
+ # sendMessage(client, Dict(kwargs))
87
+ # end
43
88
44
- function sendMessage (client:: TelegramClient , params)
45
- params[:chat_id ] = get (params, :chat_id , client. chat_id)
46
- if isempty (params[:chat_id ])
47
- throw (error (" chat_id is not defined" ))
48
- end
89
+ # function sendMessage(client::TelegramClient, params)
90
+ # params[:chat_id] = get(params, :chat_id, client.chat_id)
91
+ # if isempty(params[:chat_id])
92
+ # throw(error("chat_id is not defined"))
93
+ # end
49
94
50
- params[:parse_mode ] = get (params, :parse_mode , client. parse_mode)
95
+ # params[:parse_mode] = get(params, :parse_mode, client.parse_mode)
51
96
52
- query (client, " sendMessage" , params = params)
97
+ # query(client, "sendMessage", params = params)
98
+ # end
99
+
100
+ function apiquery (method, client:: TelegramClient = DEFAULT_OPTS. client; kwargs... )
101
+ params = Dict {Symbol, Any} (kwargs)
102
+ params[:chat_id ] = get (params, :chat_id , client. chat_id)
103
+ params[:parse_mode ] = get (params, :parse_mode , client. parse_mode)
104
+ query (client, " method" , params = params)
53
105
end
54
106
55
107
# Methods which is checking only existence of chat_id
56
- for method in [:kickChatMember , :unbanChatMember , :restrictChatMember ,
108
+ for method in [:getMe , :sendMessage ,
109
+ :sendPhoto , :sendAudio , :sendDocument , :sendVideo , :sendAnimation , :sendVoice ,
110
+ :sendVideoNote ,
111
+ :kickChatMember , :unbanChatMember , :restrictChatMember ,
57
112
:promoteChatMember , :setChatAdministratorCustomTitle , :setChatPermissions ,
58
113
:exportChatInviteLink , :deleteChatPhoto , :setChatTitle ,
59
114
:setChatDescription , :pinChatMessage , :unpinChatMessage ,
60
115
:leaveChat , :getChat , :getChatAdministrators ,
61
116
:getChatMembersCount , :getChatMember ,
62
- :setChatStickerSet , :deleteChatStickerSet ]
117
+ :setChatStickerSet , :deleteChatStickerSet ,
118
+ :getUpdates ]
63
119
64
- @eval function $method (client:: TelegramClient ; kwargs... )
120
+ @eval function $method (client:: TelegramClient = DEFAULT_OPTS . client ; kwargs... )
65
121
params = Dict {Symbol, Any} (kwargs)
66
122
params[:chat_id ] = get (params, :chat_id , client. chat_id)
67
- if isempty (params[:chat_id ])
68
- throw (error (" chat_id is not defined" ))
69
- end
123
+ params[:parse_mode ] = get (params, :parse_mode , client. parse_mode)
70
124
query (client, String (Symbol ($ method)), params = params)
71
125
end
72
126
73
127
@eval export $ method
74
128
end
75
129
76
- # function getChat(client::TelegramClient; kwargs...)
77
- # params = Dict{Symbol, Any}(kwargs)
78
- # params[:chat_id] = get(params, :chat_id, client.chat_id)
79
- # if isempty(params[:chat_id])
80
- # throw(error("chat_id is not defined"))
81
- # end
82
- # query(client, "getChat", params = params)
83
- # end
84
-
85
- # function getChatAdministrators(client::TelegramClient; kwargs...)
86
- # params = Dict{Symbol, Any}(kwargs)
87
- # params[:chat_id] = get(params, :chat_id, client.chat_id)
88
- # if isempty(params[:chat_id])
89
- # throw(error("chat_id is not defined"))
90
- # end
91
- # query(client, "getChatAdministrators", params = params)
92
- # end
93
-
94
- # function getChatMembersCount(client::TelegramClient; kwargs...)
95
- # params = Dict{Symbol, Any}(kwargs)
96
- # params[:chat_id] = get(params, :chat_id, client.chat_id)
97
- # if isempty(params[:chat_id])
98
- # throw(error("chat_id is not defined"))
99
- # end
100
- # query(client, "getChatMembersCount", params = params)
101
- # end
102
-
103
130
end # module
0 commit comments