gruubY 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +11 -6
- data/Rakefile +51 -51
- data/config/config.rb +1 -1
- data/example/bot.rb +122 -2
- data/example/generate_user_session.rb +81 -81
- data/example/tdlib.rb +77 -77
- data/example/userbot.rb +133 -133
- data/lib/gruubY/api.rb +540 -363
- data/lib/gruubY/async.rb +13 -13
- data/lib/gruubY/bot.rb +104 -90
- data/lib/gruubY/bound.rb +14 -14
- data/lib/gruubY/client.rb +310 -296
- data/lib/gruubY/context.rb +296 -242
- data/lib/gruubY/dispatcher.rb +60 -60
- data/lib/gruubY/enums.rb +57 -62
- data/lib/gruubY/exception.rb +5 -5
- data/lib/gruubY/file_stream.rb +17 -17
- data/lib/gruubY/filters.rb +292 -292
- data/lib/gruubY/group_manager.rb +213 -213
- data/lib/gruubY/handlers.rb +225 -170
- data/lib/gruubY/keyboard.rb +49 -49
- data/lib/gruubY/media.rb +38 -38
- data/lib/gruubY/middleware.rb +18 -18
- data/lib/gruubY/plugin.rb +27 -14
- data/lib/gruubY/raw.rb +25 -25
- data/lib/gruubY/retry.rb +14 -14
- data/lib/gruubY/server.rb +18 -18
- data/lib/gruubY/session.rb +30 -30
- data/lib/gruubY/tdlib/client.rb +565 -565
- data/lib/gruubY/tdlib/client_manager.rb +37 -37
- data/lib/gruubY/tdlib/decorators.rb +17 -17
- data/lib/gruubY/tdlib/errors.rb +6 -6
- data/lib/gruubY/tdlib/group_manager.rb +58 -58
- data/lib/gruubY/tdlib/native.rb +72 -72
- data/lib/gruubY/tdlib/schema_builder.rb +237 -237
- data/lib/gruubY/tdlib/tdjson.rb +49 -49
- data/lib/gruubY/tdlib/user_session.rb +69 -69
- data/lib/gruubY/types/base_object.rb +72 -72
- data/lib/gruubY/types/bound_entities.rb +289 -245
- data/lib/gruubY/types/chat.rb +93 -93
- data/lib/gruubY/types/chat_info.rb +32 -31
- data/lib/gruubY/types/extra.rb +289 -251
- data/lib/gruubY/types/message.rb +301 -295
- data/lib/gruubY/types/message_entity.rb +14 -14
- data/lib/gruubY/types/registry.rb +86 -67
- data/lib/gruubY/types/user.rb +47 -31
- data/lib/gruubY/webapp.rb +22 -22
- data/lib/gruubY.rb +39 -41
- data/plugins/logger.rb +7 -7
- metadata +3 -9
- data/example/ntgcalls.rb +0 -80
- data/lib/gruubY/ntgcalls/native.rb +0 -100
- data/lib/gruubY/ntgcalls.rb +0 -478
- data/lib/gruubY/raw_types.rb +0 -57
- data/plugins/media_tools.rb +0 -13
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 752c37c8a38c9c88be4f2105a2e1c03e46fcf144be23f39ba30f6b9a689c33ef
|
|
4
|
+
data.tar.gz: 26d249039e5715d4d9b4e1cf78663b72a3b9d449b07401a159c06e88a5146736
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 282cbb8f0ad25b2162050f3424f8b24526263a4409b481d6b1acda5615001551ade9a23a39889ab03e931e37895d184edf2020f0bea6a1416f64d11dae43f8eb
|
|
7
|
+
data.tar.gz: c4e3a1bae8eb847f86bce1bc23c366bd0d81334c32f68fd28bc2cf6445b92dd2cded0f9787894daaf50a2fc082085cbfe0ff751201153a183718b26894cd7ef9
|
data/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Sreyanshu
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sreyanshu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
> GrubY (Unstable)
|
|
2
|
-
|
|
3
|
-
[](https://www.ruby-lang.org/)
|
|
4
|
-
[](https://opensource.org/licenses/MIT)
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
> GrubY (Unstable)
|
|
2
|
+
|
|
3
|
+
[](https://www.ruby-lang.org/)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://rubygems.org/gems/gruubY)
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
*Maintained by [Sreyanshu](https://github.com/iSreyanshu)*
|
|
10
|
+
|
|
11
|
+
See the [full documentation](source/README.md) for Bot API usage, current API coverage, updates, message editing, sticker methods and examples.
|
data/Rakefile
CHANGED
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
require "rake/clean"
|
|
2
|
-
|
|
3
|
-
APP_ROOT = File.expand_path(__dir__)
|
|
4
|
-
|
|
5
|
-
CLEAN.include(*Dir["*.gem"])
|
|
6
|
-
|
|
7
|
-
desc "Build gem package"
|
|
8
|
-
task :build do
|
|
9
|
-
sh "gem build gruubY.gemspec"
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
desc "Install built gem locally"
|
|
13
|
-
task install_local: :build do
|
|
14
|
-
gem_file = Dir["gruubY-*.gem"].max_by { |f| File.mtime(f) }
|
|
15
|
-
abort "No gem package found" unless gem_file
|
|
16
|
-
|
|
17
|
-
sh "gem install #{gem_file}"
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
desc "Install Ruby dependencies"
|
|
21
|
-
task :bundle_install do
|
|
22
|
-
sh "bundle install"
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
desc "Prepare Ruby dependencies"
|
|
26
|
-
task setup: [:bundle_install]
|
|
27
|
-
|
|
28
|
-
namespace :ci do
|
|
29
|
-
desc "Minimal smoke check for gem load"
|
|
30
|
-
task :smoke do
|
|
31
|
-
ruby "-e", "require_relative 'lib/gruubY'; puts 'gruubY load ok'"
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
namespace :tdlib do
|
|
36
|
-
desc "Generate tdlib.json in project root (override via SRC, VERSION, COMMIT, OUT env vars)"
|
|
37
|
-
task :generate_json do
|
|
38
|
-
src = ENV.fetch("SRC", "https://raw.githubusercontent.com/tdlib/td/refs/heads/master/td/generate/scheme/td_api.tl")
|
|
39
|
-
version = ENV.fetch("VERSION", "")
|
|
40
|
-
commit = ENV.fetch("COMMIT", "")
|
|
41
|
-
out = ENV.fetch("OUT", File.join(APP_ROOT, "tdlib.json"))
|
|
42
|
-
|
|
43
|
-
ruby "scripts/generate-tdlib-json.rb",
|
|
44
|
-
"--src", src,
|
|
45
|
-
"--version", version,
|
|
46
|
-
"--commit", commit,
|
|
47
|
-
"--out", out
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
task default: :build
|
|
1
|
+
require "rake/clean"
|
|
2
|
+
|
|
3
|
+
APP_ROOT = File.expand_path(__dir__)
|
|
4
|
+
|
|
5
|
+
CLEAN.include(*Dir["*.gem"])
|
|
6
|
+
|
|
7
|
+
desc "Build gem package"
|
|
8
|
+
task :build do
|
|
9
|
+
sh "gem build gruubY.gemspec"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
desc "Install built gem locally"
|
|
13
|
+
task install_local: :build do
|
|
14
|
+
gem_file = Dir["gruubY-*.gem"].max_by { |f| File.mtime(f) }
|
|
15
|
+
abort "No gem package found" unless gem_file
|
|
16
|
+
|
|
17
|
+
sh "gem install #{gem_file}"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
desc "Install Ruby dependencies"
|
|
21
|
+
task :bundle_install do
|
|
22
|
+
sh "bundle install"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
desc "Prepare Ruby dependencies"
|
|
26
|
+
task setup: [:bundle_install]
|
|
27
|
+
|
|
28
|
+
namespace :ci do
|
|
29
|
+
desc "Minimal smoke check for gem load"
|
|
30
|
+
task :smoke do
|
|
31
|
+
ruby "-e", "require_relative 'lib/gruubY'; puts 'gruubY load ok'"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
namespace :tdlib do
|
|
36
|
+
desc "Generate tdlib.json in project root (override via SRC, VERSION, COMMIT, OUT env vars)"
|
|
37
|
+
task :generate_json do
|
|
38
|
+
src = ENV.fetch("SRC", "https://raw.githubusercontent.com/tdlib/td/refs/heads/master/td/generate/scheme/td_api.tl")
|
|
39
|
+
version = ENV.fetch("VERSION", "")
|
|
40
|
+
commit = ENV.fetch("COMMIT", "")
|
|
41
|
+
out = ENV.fetch("OUT", File.join(APP_ROOT, "tdlib.json"))
|
|
42
|
+
|
|
43
|
+
ruby "scripts/generate-tdlib-json.rb",
|
|
44
|
+
"--src", src,
|
|
45
|
+
"--version", version,
|
|
46
|
+
"--commit", commit,
|
|
47
|
+
"--out", out
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
task default: :build
|
data/config/config.rb
CHANGED
data/example/bot.rb
CHANGED
|
@@ -4,7 +4,8 @@ require_relative "../config/config"
|
|
|
4
4
|
client = GrubY::Client.new(Config::TOKEN)
|
|
5
5
|
gm = client.group_manager
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
plugin_file = File.join(__dir__, '../plugins/logger.rb')
|
|
8
|
+
client.use_plugin(plugin_file)
|
|
8
9
|
|
|
9
10
|
HELP = <<~TXT
|
|
10
11
|
Commands:
|
|
@@ -15,6 +16,12 @@ HELP = <<~TXT
|
|
|
15
16
|
/html <text> - HTML safe send
|
|
16
17
|
/poll - Send sample poll
|
|
17
18
|
/media - Send sample media methods
|
|
19
|
+
/edit - Send and edit a message
|
|
20
|
+
/rich - Send a rich message
|
|
21
|
+
/widget - Send an HTML widget in a private chat
|
|
22
|
+
/sticker <file_id> - Send a sticker by file ID
|
|
23
|
+
/stickerset <name> - Get a sticker set
|
|
24
|
+
/botname - Get the bot name
|
|
18
25
|
/whoami - Show user and chat info
|
|
19
26
|
/warn <user_ID> [reason] - Warn user
|
|
20
27
|
/mute <user_ID> - Mute user
|
|
@@ -69,7 +76,120 @@ end
|
|
|
69
76
|
|
|
70
77
|
client.on(:message, GrubY::Filters.command("media")) do |ctx|
|
|
71
78
|
client.send_chat_action(ctx.chat_id, "typing")
|
|
72
|
-
ctx.reply("Try these API methods from code: send_photo/
|
|
79
|
+
ctx.reply("Try these API methods from code: send_photo/send_document/send_video/send_animation/send_voice/send_video_note/send_media_group/send_paid_media")
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
client.on(:message, GrubY::Filters.command("edit")) do |ctx|
|
|
83
|
+
sent = ctx.reply("This message will be edited.")
|
|
84
|
+
client.edit_message_text(
|
|
85
|
+
ctx.chat_id,
|
|
86
|
+
sent.fetch("message_id"),
|
|
87
|
+
"Message edited with the Bot API."
|
|
88
|
+
)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
client.on(:message, GrubY::Filters.command("rich")) do |ctx|
|
|
92
|
+
client.send_rich_message(
|
|
93
|
+
chat_id: ctx.chat_id,
|
|
94
|
+
rich_message: { html: "<p>Rich message sent with GrubY.</p>" }
|
|
95
|
+
)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
client.on(:message, GrubY::Filters.command("widget")) do |ctx|
|
|
99
|
+
unless ctx.message&.chat&.type == "private"
|
|
100
|
+
ctx.reply("Widgets are supported here only in a private chat with the bot.")
|
|
101
|
+
next
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
client.send_widget(
|
|
105
|
+
chat_id: ctx.chat_id,
|
|
106
|
+
html: "<div><p>Hello from a Telegram widget.</p></div>",
|
|
107
|
+
allow_queries: true
|
|
108
|
+
)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
client.on(:message, GrubY::Filters.command("sticker")) do |ctx|
|
|
112
|
+
file_id = ctx.command_args.first
|
|
113
|
+
if file_id.to_s.empty?
|
|
114
|
+
ctx.reply("Usage: /sticker <file_id>")
|
|
115
|
+
next
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
client.send_sticker(ctx.chat_id, file_id)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
client.on(:message, GrubY::Filters.command("stickerset")) do |ctx|
|
|
122
|
+
name = ctx.command_args.first
|
|
123
|
+
if name.to_s.empty?
|
|
124
|
+
ctx.reply("Usage: /stickerset <sticker_set_name>")
|
|
125
|
+
next
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
sticker_set = client.get_sticker_set(name: name)
|
|
129
|
+
ctx.reply("#{sticker_set["title"]}: #{sticker_set["stickers"].length} stickers")
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
client.on(:message, GrubY::Filters.command("botname")) do |ctx|
|
|
133
|
+
result = client.get_my_name
|
|
134
|
+
ctx.reply(result["name"].to_s.empty? ? "Bot name is not set." : result["name"])
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
client.on_channel_post do |_app, post|
|
|
138
|
+
puts "Channel post #{post.message_id} received in #{post.chat&.id}"
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
client.on_edited_channel_post do |_app, post|
|
|
142
|
+
puts "Channel post #{post.message_id} was edited"
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
client.on_guest_message do |_app, message|
|
|
146
|
+
puts "Guest message #{message.message_id} received (query=#{message.guest_query_id})"
|
|
147
|
+
client.answer_guest_query(
|
|
148
|
+
guest_query_id: message.guest_query_id,
|
|
149
|
+
result: {
|
|
150
|
+
type: "article",
|
|
151
|
+
id: "guest-reply",
|
|
152
|
+
title: "Thanks",
|
|
153
|
+
input_message_content: { message_text: "Thanks for your message." }
|
|
154
|
+
}
|
|
155
|
+
)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
client.on_poll_answer do |_app, answer|
|
|
159
|
+
puts "Poll answer received for poll #{answer.poll_id}"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
client.on_chat_member do |_app, member_update|
|
|
163
|
+
puts "Chat member status changed in #{member_update.chat&.id}"
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
client.on_my_chat_member do |_app, member_update|
|
|
167
|
+
puts "Bot status changed in #{member_update.chat&.id}"
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
client.on_widget_query do |_app, query|
|
|
171
|
+
puts "Widget query received: #{query.id}"
|
|
172
|
+
client.answer_widget_query(widget_query_id: query.id, data: "received")
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
client.on_shared_widget_query do |_app, query|
|
|
176
|
+
puts "Shared widget query received: #{query.id}"
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
client.on_removed_chat_boost do |_app, boost|
|
|
180
|
+
puts "A boost was removed from #{boost.chat&.id}"
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
client.on_bot_subscription_updated do |_app, subscription|
|
|
184
|
+
puts "Subscription update received: #{subscription.to_h}"
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
client.on_message_generation_stopped do |_app, stopped|
|
|
188
|
+
puts "Message generation stopped: #{stopped.to_h}"
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
client.on_managed_bot do |_app, managed_bot|
|
|
192
|
+
puts "Managed bot update received: #{managed_bot.to_h}"
|
|
73
193
|
end
|
|
74
194
|
|
|
75
195
|
client.on(:message, GrubY::Filters.command("whoami")) do |ctx|
|
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
require_relative "../lib/gruubY"
|
|
2
|
-
|
|
3
|
-
def prompt(label)
|
|
4
|
-
print label
|
|
5
|
-
STDIN.gets.to_s.strip
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
api_id = ENV["TD_API_ID"]&.to_i
|
|
9
|
-
api_hash = ENV["TD_API_HASH"].to_s
|
|
10
|
-
session_name = ENV.fetch("TD_SESSION_NAME", "default")
|
|
11
|
-
|
|
12
|
-
abort("Missing TD_API_ID or TD_API_HASH") if api_id.nil? || api_hash.empty?
|
|
13
|
-
|
|
14
|
-
bundle = GrubY::TDLib::UserSession.build(session_name: session_name)
|
|
15
|
-
session_string = GrubY::TDLib::UserSession.encode(
|
|
16
|
-
api_id: api_id,
|
|
17
|
-
api_hash: api_hash,
|
|
18
|
-
database_directory: bundle[:database_directory],
|
|
19
|
-
files_directory: bundle[:files_directory],
|
|
20
|
-
database_encryption_key: bundle[:database_encryption_key],
|
|
21
|
-
session_name: bundle[:session_name]
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
client = GrubY::TDLib::Client.new(
|
|
25
|
-
api_id: api_id,
|
|
26
|
-
api_hash: api_hash,
|
|
27
|
-
database_directory: bundle[:database_directory],
|
|
28
|
-
files_directory: bundle[:files_directory],
|
|
29
|
-
database_encryption_key: bundle[:database_encryption_key],
|
|
30
|
-
tdjson_path: ENV["TDJSON_PATH"],
|
|
31
|
-
td_verbosity: 2,
|
|
32
|
-
workers: 2
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
client.on("updateAuthorizationState") do |update|
|
|
36
|
-
state = update.dig("authorization_state", "@type")
|
|
37
|
-
|
|
38
|
-
case state
|
|
39
|
-
when "authorizationStateWaitPhoneNumber"
|
|
40
|
-
loop do
|
|
41
|
-
phone = prompt("Enter phone number (+countrycode...): ")
|
|
42
|
-
next if phone.empty?
|
|
43
|
-
|
|
44
|
-
client.set_authentication_phone_number(phone_number: phone)
|
|
45
|
-
break
|
|
46
|
-
end
|
|
47
|
-
when "authorizationStateWaitCode"
|
|
48
|
-
loop do
|
|
49
|
-
code = prompt("Enter login code: ")
|
|
50
|
-
next if code.empty?
|
|
51
|
-
|
|
52
|
-
client.check_authentication_code(code)
|
|
53
|
-
break
|
|
54
|
-
end
|
|
55
|
-
when "authorizationStateWaitPassword"
|
|
56
|
-
loop do
|
|
57
|
-
pass = prompt("Enter 2FA password: ")
|
|
58
|
-
next if pass.empty?
|
|
59
|
-
|
|
60
|
-
client.check_authentication_password(pass)
|
|
61
|
-
break
|
|
62
|
-
end
|
|
63
|
-
when "authorizationStateWaitOtherDeviceConfirmation"
|
|
64
|
-
link = update.dig("authorization_state", "link")
|
|
65
|
-
puts "Open Telegram > Settings > Devices > Link Desktop Device"
|
|
66
|
-
puts link.to_s
|
|
67
|
-
when "authorizationStateReady"
|
|
68
|
-
me = client.get_me
|
|
69
|
-
puts "Authorized: #{me['first_name']} (id=#{me['id']})"
|
|
70
|
-
puts
|
|
71
|
-
puts "TD_USER_SESSION=#{session_string}"
|
|
72
|
-
puts
|
|
73
|
-
puts "Reuse in Ruby:"
|
|
74
|
-
puts " cfg = GrubY::TDLib::UserSession.client_kwargs(ENV.fetch('TD_USER_SESSION'))"
|
|
75
|
-
puts " client = GrubY::TDLib::Client.new(**cfg)"
|
|
76
|
-
client.stop
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
client.start
|
|
81
|
-
sleep 0.3 while client.authorized? == false
|
|
1
|
+
require_relative "../lib/gruubY"
|
|
2
|
+
|
|
3
|
+
def prompt(label)
|
|
4
|
+
print label
|
|
5
|
+
STDIN.gets.to_s.strip
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
api_id = ENV["TD_API_ID"]&.to_i
|
|
9
|
+
api_hash = ENV["TD_API_HASH"].to_s
|
|
10
|
+
session_name = ENV.fetch("TD_SESSION_NAME", "default")
|
|
11
|
+
|
|
12
|
+
abort("Missing TD_API_ID or TD_API_HASH") if api_id.nil? || api_hash.empty?
|
|
13
|
+
|
|
14
|
+
bundle = GrubY::TDLib::UserSession.build(session_name: session_name)
|
|
15
|
+
session_string = GrubY::TDLib::UserSession.encode(
|
|
16
|
+
api_id: api_id,
|
|
17
|
+
api_hash: api_hash,
|
|
18
|
+
database_directory: bundle[:database_directory],
|
|
19
|
+
files_directory: bundle[:files_directory],
|
|
20
|
+
database_encryption_key: bundle[:database_encryption_key],
|
|
21
|
+
session_name: bundle[:session_name]
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
client = GrubY::TDLib::Client.new(
|
|
25
|
+
api_id: api_id,
|
|
26
|
+
api_hash: api_hash,
|
|
27
|
+
database_directory: bundle[:database_directory],
|
|
28
|
+
files_directory: bundle[:files_directory],
|
|
29
|
+
database_encryption_key: bundle[:database_encryption_key],
|
|
30
|
+
tdjson_path: ENV["TDJSON_PATH"],
|
|
31
|
+
td_verbosity: 2,
|
|
32
|
+
workers: 2
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
client.on("updateAuthorizationState") do |update|
|
|
36
|
+
state = update.dig("authorization_state", "@type")
|
|
37
|
+
|
|
38
|
+
case state
|
|
39
|
+
when "authorizationStateWaitPhoneNumber"
|
|
40
|
+
loop do
|
|
41
|
+
phone = prompt("Enter phone number (+countrycode...): ")
|
|
42
|
+
next if phone.empty?
|
|
43
|
+
|
|
44
|
+
client.set_authentication_phone_number(phone_number: phone)
|
|
45
|
+
break
|
|
46
|
+
end
|
|
47
|
+
when "authorizationStateWaitCode"
|
|
48
|
+
loop do
|
|
49
|
+
code = prompt("Enter login code: ")
|
|
50
|
+
next if code.empty?
|
|
51
|
+
|
|
52
|
+
client.check_authentication_code(code)
|
|
53
|
+
break
|
|
54
|
+
end
|
|
55
|
+
when "authorizationStateWaitPassword"
|
|
56
|
+
loop do
|
|
57
|
+
pass = prompt("Enter 2FA password: ")
|
|
58
|
+
next if pass.empty?
|
|
59
|
+
|
|
60
|
+
client.check_authentication_password(pass)
|
|
61
|
+
break
|
|
62
|
+
end
|
|
63
|
+
when "authorizationStateWaitOtherDeviceConfirmation"
|
|
64
|
+
link = update.dig("authorization_state", "link")
|
|
65
|
+
puts "Open Telegram > Settings > Devices > Link Desktop Device"
|
|
66
|
+
puts link.to_s
|
|
67
|
+
when "authorizationStateReady"
|
|
68
|
+
me = client.get_me
|
|
69
|
+
puts "Authorized: #{me['first_name']} (id=#{me['id']})"
|
|
70
|
+
puts
|
|
71
|
+
puts "TD_USER_SESSION=#{session_string}"
|
|
72
|
+
puts
|
|
73
|
+
puts "Reuse in Ruby:"
|
|
74
|
+
puts " cfg = GrubY::TDLib::UserSession.client_kwargs(ENV.fetch('TD_USER_SESSION'))"
|
|
75
|
+
puts " client = GrubY::TDLib::Client.new(**cfg)"
|
|
76
|
+
client.stop
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
client.start
|
|
81
|
+
sleep 0.3 while client.authorized? == false
|
data/example/tdlib.rb
CHANGED
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
require_relative "../lib/gruubY"
|
|
2
|
-
|
|
3
|
-
# Set these in your environment before running:
|
|
4
|
-
# TD_API_ID=12345
|
|
5
|
-
# TD_API_HASH=xxxxx
|
|
6
|
-
# TD_BOT_TOKEN=12345:abc.. (or use TD_PHONE for user auth)
|
|
7
|
-
# TDJSON_PATH=/path/to/libtdjson.so|dylib|dll
|
|
8
|
-
|
|
9
|
-
api_id = ENV["TD_API_ID"]&.to_i
|
|
10
|
-
api_hash = ENV["TD_API_HASH"]
|
|
11
|
-
bot_token = ENV["TD_BOT_TOKEN"]
|
|
12
|
-
phone = ENV["TD_PHONE"]
|
|
13
|
-
|
|
14
|
-
abort("Missing TD_API_ID or TD_API_HASH") unless api_id && api_hash
|
|
15
|
-
|
|
16
|
-
client = GrubY::TDLib::Client.new(
|
|
17
|
-
api_id: api_id,
|
|
18
|
-
api_hash: api_hash,
|
|
19
|
-
bot_token: bot_token,
|
|
20
|
-
phone_number: phone,
|
|
21
|
-
tdjson_path: ENV["TDJSON_PATH"],
|
|
22
|
-
workers: 4,
|
|
23
|
-
default_handler_timeout: 10
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
gm = client.group_manager
|
|
27
|
-
|
|
28
|
-
client.on("clientReady") do
|
|
29
|
-
puts "[TDLIB] ready"
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
client.initializer do |update|
|
|
33
|
-
type = update["@type"]
|
|
34
|
-
puts "[TDLIB] update=#{type}" if type
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
client.on("authCodeNeeded") do
|
|
38
|
-
print "Enter login code: "
|
|
39
|
-
code = STDIN.gets.to_s.strip
|
|
40
|
-
client.check_authentication_code(code)
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
client.on("authPasswordNeeded") do
|
|
44
|
-
print "Enter 2FA password: "
|
|
45
|
-
pass = STDIN.gets.to_s.strip
|
|
46
|
-
client.check_authentication_password(pass)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
client.on_message do |message|
|
|
50
|
-
chat_id = message["chat_id"]
|
|
51
|
-
content = message.dig("content", "@type")
|
|
52
|
-
|
|
53
|
-
next unless content == "messageText"
|
|
54
|
-
|
|
55
|
-
text = message.dig("content", "text", "text").to_s
|
|
56
|
-
next unless text.start_with?("/")
|
|
57
|
-
|
|
58
|
-
case text
|
|
59
|
-
when "/ping"
|
|
60
|
-
client.sendMessage(
|
|
61
|
-
chat_id: chat_id,
|
|
62
|
-
input_message_content: {
|
|
63
|
-
"@type": "inputMessageText",
|
|
64
|
-
text: { "@type": "formattedText", text: "pong" }
|
|
65
|
-
}
|
|
66
|
-
)
|
|
67
|
-
when "/lock"
|
|
68
|
-
gm.set_slow_mode(chat_id: chat_id, delay: 30)
|
|
69
|
-
when "/title"
|
|
70
|
-
gm.set_chat_title(chat_id: chat_id, title: "GrubY TDLib Room")
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
client.finalizer do |_update|
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
client.run
|
|
1
|
+
require_relative "../lib/gruubY"
|
|
2
|
+
|
|
3
|
+
# Set these in your environment before running:
|
|
4
|
+
# TD_API_ID=12345
|
|
5
|
+
# TD_API_HASH=xxxxx
|
|
6
|
+
# TD_BOT_TOKEN=12345:abc.. (or use TD_PHONE for user auth)
|
|
7
|
+
# TDJSON_PATH=/path/to/libtdjson.so|dylib|dll
|
|
8
|
+
|
|
9
|
+
api_id = ENV["TD_API_ID"]&.to_i
|
|
10
|
+
api_hash = ENV["TD_API_HASH"]
|
|
11
|
+
bot_token = ENV["TD_BOT_TOKEN"]
|
|
12
|
+
phone = ENV["TD_PHONE"]
|
|
13
|
+
|
|
14
|
+
abort("Missing TD_API_ID or TD_API_HASH") unless api_id && api_hash
|
|
15
|
+
|
|
16
|
+
client = GrubY::TDLib::Client.new(
|
|
17
|
+
api_id: api_id,
|
|
18
|
+
api_hash: api_hash,
|
|
19
|
+
bot_token: bot_token,
|
|
20
|
+
phone_number: phone,
|
|
21
|
+
tdjson_path: ENV["TDJSON_PATH"],
|
|
22
|
+
workers: 4,
|
|
23
|
+
default_handler_timeout: 10
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
gm = client.group_manager
|
|
27
|
+
|
|
28
|
+
client.on("clientReady") do
|
|
29
|
+
puts "[TDLIB] ready"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
client.initializer do |update|
|
|
33
|
+
type = update["@type"]
|
|
34
|
+
puts "[TDLIB] update=#{type}" if type
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
client.on("authCodeNeeded") do
|
|
38
|
+
print "Enter login code: "
|
|
39
|
+
code = STDIN.gets.to_s.strip
|
|
40
|
+
client.check_authentication_code(code)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
client.on("authPasswordNeeded") do
|
|
44
|
+
print "Enter 2FA password: "
|
|
45
|
+
pass = STDIN.gets.to_s.strip
|
|
46
|
+
client.check_authentication_password(pass)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
client.on_message do |message|
|
|
50
|
+
chat_id = message["chat_id"]
|
|
51
|
+
content = message.dig("content", "@type")
|
|
52
|
+
|
|
53
|
+
next unless content == "messageText"
|
|
54
|
+
|
|
55
|
+
text = message.dig("content", "text", "text").to_s
|
|
56
|
+
next unless text.start_with?("/")
|
|
57
|
+
|
|
58
|
+
case text
|
|
59
|
+
when "/ping"
|
|
60
|
+
client.sendMessage(
|
|
61
|
+
chat_id: chat_id,
|
|
62
|
+
input_message_content: {
|
|
63
|
+
"@type": "inputMessageText",
|
|
64
|
+
text: { "@type": "formattedText", text: "pong" }
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
when "/lock"
|
|
68
|
+
gm.set_slow_mode(chat_id: chat_id, delay: 30)
|
|
69
|
+
when "/title"
|
|
70
|
+
gm.set_chat_title(chat_id: chat_id, title: "GrubY TDLib Room")
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
client.finalizer do |_update|
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
client.run
|