Skip to content

Commit e05d563

Browse files
author
Andrey Oskin
committed
Increased functionality
1 parent 25a34a4 commit e05d563

File tree

2 files changed

+89
-61
lines changed

2 files changed

+89
-61
lines changed

extras/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experiments*

src/Telegram.jl

+88-61
Original file line numberDiff line numberDiff line change
@@ -2,102 +2,129 @@ module Telegram
22
using HTTP
33
using JSON3
44

5-
export TelegramClient
5+
export TelegramClient, useglobally!
66
export getMe, sendMessage
77

8-
const DEFAULT_OPTS = Dict(:chat_id => "",
9-
:parse_mode => "MarkdownV2",
10-
)
8+
struct TelegramError{T} <: Exception
9+
msg::T
10+
end
1111

1212
mutable struct TelegramClient
1313
token::String
1414
chat_id::String
1515
parse_mode::String
16+
end
1617

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
2536
end
2637

2738
token(client::TelegramClient) = client.token
2839

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+
2955
function query(client::TelegramClient, method; params = Dict())
3056
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
3681
end
3782

38-
getMe(client::TelegramClient) = query(client, "getMe")
83+
# getMe(client::TelegramClient = DEFAULT_OPTS.client) = query(client, "getMe")
3984

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
4388

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
4994

50-
params[:parse_mode] = get(params, :parse_mode, client.parse_mode)
95+
# params[:parse_mode] = get(params, :parse_mode, client.parse_mode)
5196

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)
53105
end
54106

55107
# 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,
57112
:promoteChatMember, :setChatAdministratorCustomTitle, :setChatPermissions,
58113
:exportChatInviteLink, :deleteChatPhoto, :setChatTitle,
59114
:setChatDescription, :pinChatMessage, :unpinChatMessage,
60115
:leaveChat, :getChat, :getChatAdministrators,
61116
:getChatMembersCount, :getChatMember,
62-
:setChatStickerSet, :deleteChatStickerSet]
117+
:setChatStickerSet, :deleteChatStickerSet,
118+
:getUpdates]
63119

64-
@eval function $method(client::TelegramClient; kwargs...)
120+
@eval function $method(client::TelegramClient = DEFAULT_OPTS.client; kwargs...)
65121
params = Dict{Symbol, Any}(kwargs)
66122
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)
70124
query(client, String(Symbol($method)), params = params)
71125
end
72126

73127
@eval export $method
74128
end
75129

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-
103130
end # module

0 commit comments

Comments
 (0)