Skip to content

Commit

Permalink
update tdlib methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nevrfl committed Sep 2, 2024
1 parent 37a51ba commit eea1570
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ gem 'sidekiq-rate-limiter', '0.1.3', require: 'sidekiq-rate-limiter/server'
gem 'telegram-bot-ruby', '>= 0.11', '< 1.0'
gem 'slack-ruby-bot'
gem 'celluloid-io'
gem 'tdlib-ruby', '3.0.2'
gem 'tdlib-schema', git: 'https://github.com/southbridgeio/tdlib-schema', branch: 'update'
gem 'tdlib-ruby', git: 'https://github.com/southbridgeio/tdlib-ruby', tag: "v3.0.3"
gem 'tdlib-schema', git: 'https://github.com/southbridgeio/tdlib-schema'
gem 'jwt'
gem 'filelock'
gem 'patron'
Expand Down
4 changes: 2 additions & 2 deletions lib/redmine_bots/telegram/tdlib/add_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ class AddBot < Command
def call(bot_name)
client.search_public_chat(username: bot_name).then do |chat|
message = TD::Types::InputMessageContent::Text.new(text: TD::Types::FormattedText.new(text: '/start', entities: []),
disable_web_page_preview: true,
link_preview_options: nil,
clear_draft: false)
client.send_message(chat_id: chat.id,
message_thread_id: nil,
reply_to_message_id: nil,
reply_to: nil,
options: nil,
reply_markup: nil,
input_message_content: message)
Expand Down
8 changes: 4 additions & 4 deletions lib/redmine_bots/telegram/tdlib/close_chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def call(chat_id)
when TD::Types::ChatType::BasicGroup
fetch_robot_ids.then { |*robot_ids| close_basic_group(chat, robot_ids) }.flat
when TD::Types::ChatType::Supergroup
close_super_group(chat.type)
close_super_group(chat.id)
else
raise 'Unsupported chat type'
end
Expand Down Expand Up @@ -39,12 +39,12 @@ def close_basic_group(chat, robot_ids)
end.flat
end

def close_super_group(chat_type)
client.delete_supergroup(supergroup_id: chat_type.supergroup_id)
def close_super_group(chat_id)
client.delete_chat(chat_id: chat_id)
end

def delete_member(chat_id, user_id)
client.set_chat_member_status(chat_id: chat_id, user_id: user_id, status: ChatMemberStatus::Left.new)
client.set_chat_member_status(chat_id: chat_id, member_id: user_id, status: ChatMemberStatus::Left.new)
end
end
end
41 changes: 30 additions & 11 deletions lib/redmine_bots/telegram/tdlib/create_chat.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module RedmineBots::Telegram::Tdlib
class CreateChat < Command
def call(title, user_ids)
puts title
puts user_ids
Promises.zip(*user_ids.map { |id| client.get_user(user_id: id) }).then do
client.create_new_supergroup_chat(title: title, is_channel: false, description: '', location: nil).then do |chat|
client.create_new_supergroup_chat(**supergroup_chat_params(title)).then do |chat|
client.add_chat_members(chat_id: chat.id, user_ids: user_ids).then do
client.set_chat_permissions(chat_id: chat.id, permissions: permissions).then { chat }
end.flat
Expand All @@ -15,14 +13,35 @@ def call(title, user_ids)
private

def permissions
ChatPermissions.new(can_send_messages: true,
can_send_media_messages: true,
can_send_polls: true,
can_send_other_messages: true,
can_add_web_page_previews: true,
can_change_info: false,
can_invite_users: false,
can_pin_messages: false)
ChatPermissions.new(
can_send_basic_messages: true,
can_send_audios: true,
can_send_documents: true,
can_send_photos: true,
can_send_videos: true,
can_send_video_notes: true,
can_send_voice_notes: true,
can_send_polls: true,
can_send_other_messages: true,
can_add_link_previews: true,
can_send_media_messages: true,
can_change_info: false,
can_invite_users: false,
can_pin_messages: false,
can_create_topics: false
)
end

def supergroup_chat_params(title)
{
title: title,
is_channel: false,
description: '',
location: nil,
is_forum: false,
message_auto_delete_time: 0,
for_import: true
}
end
end
end
23 changes: 12 additions & 11 deletions lib/redmine_bots/telegram/tdlib/fetch_all_chats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ def initialize(*)
super

@chat_list = ChatList::Main.new
@offset_order = 2**63 - 1
@offset_chat_id = 0
@limit = 100
@chat_futures = []
end
Expand All @@ -17,18 +15,21 @@ def call
private

attr_reader :limit
attr_accessor :chat_list, :offset_order, :offset_chat_id
attr_accessor :chat_list

def fetch
client.get_chats(chat_list: chat_list, offset_order: offset_order, offset_chat_id: offset_chat_id, limit: limit).then do |update|
chat_ids = update.chat_ids
next Concurrent::Promises.fulfilled_future(nil) if chat_ids.empty?

client.get_chat(chat_id: chat_ids.last).then do |chat|
self.offset_chat_id, self.offset_order = chat.id, chat.positions.find { |p| p.list.is_a?(ChatList::Main) }.order
client.load_chats(chat_list: chat_list, limit: limit).then do |update|
case update
when TD::Types::Ok
fetch.wait!
end.flat
end.flat
else
next Concurrent::Promises.fulfilled_future(nil)
end
end.flat.rescue do |error|
if error.code != 404
raise error
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/redmine_bots/telegram/tdlib/get_chat_link.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module RedmineBots::Telegram::Tdlib
class GetChatLink < Command
def call(chat_id)
client.generate_chat_invite_link(chat_id: chat_id)
client.create_chat_invite_link(chat_id: chat_id, expiration_date: 0, member_limit: 0, creates_join_request: false, name: "issue")
end
end
end
50 changes: 32 additions & 18 deletions lib/redmine_bots/telegram/tdlib/toggle_chat_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@ module RedmineBots::Telegram::Tdlib
class ToggleChatAdmin < Command
def call(chat_id, user_id, admin = true)
status =
if admin
TD::Types::ChatMemberStatus::Administrator.new(
is_anonymous: false,
can_post_messages: true,
can_be_edited: true,
can_change_info: true,
can_edit_messages: true,
can_delete_messages: true,
can_invite_users: true,
can_restrict_members: true,
can_pin_messages: true,
can_promote_members: true,
custom_title: 'Redmine admin'
)
else
TD::Types::ChatMemberStatus::Member.new
end
client.get_user(user_id: user_id).then { client.set_chat_member_status(chat_id: chat_id, user_id: user_id, status: status) }.flat
if admin
TD::Types::ChatMemberStatus::Administrator.new(
rights: rights,
can_be_edited: true,
custom_title: 'Redmine admin'
)
else
TD::Types::ChatMemberStatus::Member.new
end
client.get_user(user_id: user_id).then { client.set_chat_member_status(chat_id: chat_id, member_id: user_id, status: status) }.flat
end

private

def rights
TD::Types::ChatAdministratorRights.new(
can_manage_topics: true,
can_manage_chat: true,
can_change_info: true,
can_post_messages: true,
can_edit_messages: true,
can_delete_messages: true,
can_invite_users: true,
can_restrict_members: true,
can_pin_messages: true,
can_promote_members: true,
can_manage_video_chats: true,
can_post_stories: false,
can_edit_stories: false,
can_delete_stories: false,
is_anonymous: false
)
end
end
end

0 comments on commit eea1570

Please sign in to comment.