Skip to content

Commit eea1570

Browse files
committed
update tdlib methods
1 parent 37a51ba commit eea1570

File tree

7 files changed

+83
-49
lines changed

7 files changed

+83
-49
lines changed

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ gem 'sidekiq-rate-limiter', '0.1.3', require: 'sidekiq-rate-limiter/server'
55
gem 'telegram-bot-ruby', '>= 0.11', '< 1.0'
66
gem 'slack-ruby-bot'
77
gem 'celluloid-io'
8-
gem 'tdlib-ruby', '3.0.2'
9-
gem 'tdlib-schema', git: 'https://github.com/southbridgeio/tdlib-schema', branch: 'update'
8+
gem 'tdlib-ruby', git: 'https://github.com/southbridgeio/tdlib-ruby', tag: "v3.0.3"
9+
gem 'tdlib-schema', git: 'https://github.com/southbridgeio/tdlib-schema'
1010
gem 'jwt'
1111
gem 'filelock'
1212
gem 'patron'

lib/redmine_bots/telegram/tdlib/add_bot.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ class AddBot < Command
33
def call(bot_name)
44
client.search_public_chat(username: bot_name).then do |chat|
55
message = TD::Types::InputMessageContent::Text.new(text: TD::Types::FormattedText.new(text: '/start', entities: []),
6-
disable_web_page_preview: true,
6+
link_preview_options: nil,
77
clear_draft: false)
88
client.send_message(chat_id: chat.id,
99
message_thread_id: nil,
10-
reply_to_message_id: nil,
10+
reply_to: nil,
1111
options: nil,
1212
reply_markup: nil,
1313
input_message_content: message)

lib/redmine_bots/telegram/tdlib/close_chat.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def call(chat_id)
66
when TD::Types::ChatType::BasicGroup
77
fetch_robot_ids.then { |*robot_ids| close_basic_group(chat, robot_ids) }.flat
88
when TD::Types::ChatType::Supergroup
9-
close_super_group(chat.type)
9+
close_super_group(chat.id)
1010
else
1111
raise 'Unsupported chat type'
1212
end
@@ -39,12 +39,12 @@ def close_basic_group(chat, robot_ids)
3939
end.flat
4040
end
4141

42-
def close_super_group(chat_type)
43-
client.delete_supergroup(supergroup_id: chat_type.supergroup_id)
42+
def close_super_group(chat_id)
43+
client.delete_chat(chat_id: chat_id)
4444
end
4545

4646
def delete_member(chat_id, user_id)
47-
client.set_chat_member_status(chat_id: chat_id, user_id: user_id, status: ChatMemberStatus::Left.new)
47+
client.set_chat_member_status(chat_id: chat_id, member_id: user_id, status: ChatMemberStatus::Left.new)
4848
end
4949
end
5050
end
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
module RedmineBots::Telegram::Tdlib
22
class CreateChat < Command
33
def call(title, user_ids)
4-
puts title
5-
puts user_ids
64
Promises.zip(*user_ids.map { |id| client.get_user(user_id: id) }).then do
7-
client.create_new_supergroup_chat(title: title, is_channel: false, description: '', location: nil).then do |chat|
5+
client.create_new_supergroup_chat(**supergroup_chat_params(title)).then do |chat|
86
client.add_chat_members(chat_id: chat.id, user_ids: user_ids).then do
97
client.set_chat_permissions(chat_id: chat.id, permissions: permissions).then { chat }
108
end.flat
@@ -15,14 +13,35 @@ def call(title, user_ids)
1513
private
1614

1715
def permissions
18-
ChatPermissions.new(can_send_messages: true,
19-
can_send_media_messages: true,
20-
can_send_polls: true,
21-
can_send_other_messages: true,
22-
can_add_web_page_previews: true,
23-
can_change_info: false,
24-
can_invite_users: false,
25-
can_pin_messages: false)
16+
ChatPermissions.new(
17+
can_send_basic_messages: true,
18+
can_send_audios: true,
19+
can_send_documents: true,
20+
can_send_photos: true,
21+
can_send_videos: true,
22+
can_send_video_notes: true,
23+
can_send_voice_notes: true,
24+
can_send_polls: true,
25+
can_send_other_messages: true,
26+
can_add_link_previews: true,
27+
can_send_media_messages: true,
28+
can_change_info: false,
29+
can_invite_users: false,
30+
can_pin_messages: false,
31+
can_create_topics: false
32+
)
33+
end
34+
35+
def supergroup_chat_params(title)
36+
{
37+
title: title,
38+
is_channel: false,
39+
description: '',
40+
location: nil,
41+
is_forum: false,
42+
message_auto_delete_time: 0,
43+
for_import: true
44+
}
2645
end
2746
end
2847
end

lib/redmine_bots/telegram/tdlib/fetch_all_chats.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ def initialize(*)
44
super
55

66
@chat_list = ChatList::Main.new
7-
@offset_order = 2**63 - 1
8-
@offset_chat_id = 0
97
@limit = 100
108
@chat_futures = []
119
end
@@ -17,18 +15,21 @@ def call
1715
private
1816

1917
attr_reader :limit
20-
attr_accessor :chat_list, :offset_order, :offset_chat_id
18+
attr_accessor :chat_list
2119

2220
def fetch
23-
client.get_chats(chat_list: chat_list, offset_order: offset_order, offset_chat_id: offset_chat_id, limit: limit).then do |update|
24-
chat_ids = update.chat_ids
25-
next Concurrent::Promises.fulfilled_future(nil) if chat_ids.empty?
26-
27-
client.get_chat(chat_id: chat_ids.last).then do |chat|
28-
self.offset_chat_id, self.offset_order = chat.id, chat.positions.find { |p| p.list.is_a?(ChatList::Main) }.order
21+
client.load_chats(chat_list: chat_list, limit: limit).then do |update|
22+
case update
23+
when TD::Types::Ok
2924
fetch.wait!
30-
end.flat
31-
end.flat
25+
else
26+
next Concurrent::Promises.fulfilled_future(nil)
27+
end
28+
end.flat.rescue do |error|
29+
if error.code != 404
30+
raise error
31+
end
32+
end
3233
end
3334
end
3435
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module RedmineBots::Telegram::Tdlib
22
class GetChatLink < Command
33
def call(chat_id)
4-
client.generate_chat_invite_link(chat_id: chat_id)
4+
client.create_chat_invite_link(chat_id: chat_id, expiration_date: 0, member_limit: 0, creates_join_request: false, name: "issue")
55
end
66
end
77
end

lib/redmine_bots/telegram/tdlib/toggle_chat_admin.rb

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,38 @@ module RedmineBots::Telegram::Tdlib
22
class ToggleChatAdmin < Command
33
def call(chat_id, user_id, admin = true)
44
status =
5-
if admin
6-
TD::Types::ChatMemberStatus::Administrator.new(
7-
is_anonymous: false,
8-
can_post_messages: true,
9-
can_be_edited: true,
10-
can_change_info: true,
11-
can_edit_messages: true,
12-
can_delete_messages: true,
13-
can_invite_users: true,
14-
can_restrict_members: true,
15-
can_pin_messages: true,
16-
can_promote_members: true,
17-
custom_title: 'Redmine admin'
18-
)
19-
else
20-
TD::Types::ChatMemberStatus::Member.new
21-
end
22-
client.get_user(user_id: user_id).then { client.set_chat_member_status(chat_id: chat_id, user_id: user_id, status: status) }.flat
5+
if admin
6+
TD::Types::ChatMemberStatus::Administrator.new(
7+
rights: rights,
8+
can_be_edited: true,
9+
custom_title: 'Redmine admin'
10+
)
11+
else
12+
TD::Types::ChatMemberStatus::Member.new
13+
end
14+
client.get_user(user_id: user_id).then { client.set_chat_member_status(chat_id: chat_id, member_id: user_id, status: status) }.flat
15+
end
16+
17+
private
18+
19+
def rights
20+
TD::Types::ChatAdministratorRights.new(
21+
can_manage_topics: true,
22+
can_manage_chat: true,
23+
can_change_info: true,
24+
can_post_messages: true,
25+
can_edit_messages: true,
26+
can_delete_messages: true,
27+
can_invite_users: true,
28+
can_restrict_members: true,
29+
can_pin_messages: true,
30+
can_promote_members: true,
31+
can_manage_video_chats: true,
32+
can_post_stories: false,
33+
can_edit_stories: false,
34+
can_delete_stories: false,
35+
is_anonymous: false
36+
)
2337
end
2438
end
2539
end

0 commit comments

Comments
 (0)