rubydium 0.2.5 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/example/example_bot.rb +3 -3
- data/lib/rubydium/bot.rb +12 -3
- data/lib/rubydium/config.rb +1 -1
- data/lib/rubydium/mixins/message_sending.rb +3 -2
- data/lib/rubydium/mixins/other_actions.rb +29 -0
- data/lib/rubydium/mixins/rights_checking.rb +46 -0
- data/lib/rubydium/mixins.rb +5 -1
- data/lib/rubydium/version.rb +1 -1
- data/rubydium.gemspec +2 -2
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71aa370cbe3fa625308d85852bf156ff4bb3c1d263b9e965dd8ae381c6f97b40
|
4
|
+
data.tar.gz: 3d9fec8f5e34b8fb9074a596167e6aa3306ecd4ba908b23d7898fdd25277fe45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afcaf39f388f5f94b5f79c8ed0f1daba65f011a260156c7455f6200989049102dec3331a23eccf8e01dd28576ebb57f3cc11708ebd699025d749ce9c3a6b2188
|
7
|
+
data.tar.gz: 2155285580e0f8f8752f56cdb6fa133f434f54bf06f5717897378897fba98c4ff0555e6be31c673832a72d99f4765639857724480cbc094ac6d0d8cec01b561e
|
data/Gemfile
CHANGED
data/example/example_bot.rb
CHANGED
@@ -29,9 +29,9 @@ class ExampleBot < Rubydium::Bot
|
|
29
29
|
end
|
30
30
|
|
31
31
|
ExampleBot.configure do |config|
|
32
|
-
config.token = "1043894792:AAGC_oA2Ztvo5bBZBPxX5_oUxFX-L_obZJI"#"1234567890:long_alphanumeric_string_goes_here"
|
33
|
-
config.bot_username = "bulgakkebot"#"ends_with_bot"
|
34
|
-
config.owner_username = "bulgakke"#"thats_you"
|
32
|
+
config.token = "1043894792:AAGC_oA2Ztvo5bBZBPxX5_oUxFX-L_obZJI" # "1234567890:long_alphanumeric_string_goes_here"
|
33
|
+
config.bot_username = "bulgakkebot" # "ends_with_bot"
|
34
|
+
config.owner_username = "bulgakke" # "thats_you"
|
35
35
|
config.privileged_usernames = []
|
36
36
|
# %w[
|
37
37
|
# your_friend your_chat_moderator
|
data/lib/rubydium/bot.rb
CHANGED
@@ -9,8 +9,16 @@ module Rubydium
|
|
9
9
|
|
10
10
|
COMMAND_REGEXP = %r{\A/}
|
11
11
|
|
12
|
+
def self.fetch_and_set_bot_id(client)
|
13
|
+
configure do |config|
|
14
|
+
config.bot_id = client.api.get_me.dig("result", "id") unless config.respond_to? :bot_id
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
12
18
|
def self.run
|
13
19
|
Telegram::Bot::Client.run(config.token) do |client|
|
20
|
+
fetch_and_set_bot_id(client)
|
21
|
+
|
14
22
|
Async do |task|
|
15
23
|
client.listen do |update|
|
16
24
|
task.async do
|
@@ -20,7 +28,7 @@ module Rubydium
|
|
20
28
|
if update.is_a? Telegram::Bot::Types::Message
|
21
29
|
new(client, update).handle_update
|
22
30
|
end
|
23
|
-
rescue => e
|
31
|
+
rescue StandardError => e
|
24
32
|
puts e.detailed_message, e.backtrace
|
25
33
|
end
|
26
34
|
end
|
@@ -39,8 +47,9 @@ module Rubydium
|
|
39
47
|
@text = @msg.text.to_s
|
40
48
|
@message_id = @msg.message_id
|
41
49
|
@command = get_command(@msg.text)
|
42
|
-
@text_without_command = @text.gsub(@command.to_s,
|
43
|
-
|
50
|
+
@text_without_command = @text.gsub(@command.to_s, "").gsub(/@#{config.bot_username}\b/,
|
51
|
+
"").strip
|
52
|
+
@text_without_bot_mentions = @text.gsub(/@#{config.bot_username}\b/, "")
|
44
53
|
@topic_id = @msg.message_thread_id if @chat.is_forum
|
45
54
|
end
|
46
55
|
|
data/lib/rubydium/config.rb
CHANGED
@@ -4,11 +4,12 @@ module Rubydium
|
|
4
4
|
module Mixins
|
5
5
|
# Shorthand methods for sending messages in different ways.
|
6
6
|
module MessageSending
|
7
|
-
def send_message(text)
|
7
|
+
def send_message(text, **kwargs)
|
8
8
|
@api.send_message(
|
9
9
|
chat_id: @chat.id,
|
10
10
|
message_thread_id: @topic_id,
|
11
|
-
text: text
|
11
|
+
text: text,
|
12
|
+
**kwargs
|
12
13
|
)
|
13
14
|
end
|
14
15
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Rubydium
|
2
|
+
module Mixins
|
3
|
+
module OtherActions
|
4
|
+
def safe_delete(message)
|
5
|
+
return false unless (message = definitely_message(message))
|
6
|
+
return false unless bot_can_delete_messages? || message&.from&.id == config.bot_id
|
7
|
+
|
8
|
+
result = @api.delete_message(chat_id: @chat.id, message_id: message.message_id)
|
9
|
+
result["ok"]
|
10
|
+
rescue Telegram::Bot::Exceptions::ResponseError
|
11
|
+
false
|
12
|
+
end
|
13
|
+
|
14
|
+
def definitely_message(maybe_message)
|
15
|
+
return maybe_message if maybe_message.is_a? Telegram::Bot::Types::Message
|
16
|
+
|
17
|
+
if maybe_message["message_id"]
|
18
|
+
Telegram::Bot::Types::Message.new(maybe_message)
|
19
|
+
elsif maybe_message["result"]["message_id"]
|
20
|
+
Telegram::Bot::Types::Message.new(maybe_message["result"])
|
21
|
+
else
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
rescue StandardError
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Rubydium
|
2
|
+
module Mixins
|
3
|
+
module RightsChecking
|
4
|
+
def user_info(user_id=config.bot_id)
|
5
|
+
@user_info ||= @api.get_chat_member(chat_id: @chat.id, user_id: user_id).dig("result")
|
6
|
+
end
|
7
|
+
|
8
|
+
boolean_permissions = %w[
|
9
|
+
can_be_edited
|
10
|
+
can_manage_chat
|
11
|
+
can_change_info
|
12
|
+
can_delete_messages
|
13
|
+
can_invite_users
|
14
|
+
can_restrict_members
|
15
|
+
can_pin_messages
|
16
|
+
can_manage_topics
|
17
|
+
can_promote_members
|
18
|
+
can_manage_video_chats
|
19
|
+
is_anonymous
|
20
|
+
can_manage_voice_chat
|
21
|
+
]
|
22
|
+
|
23
|
+
boolean_permissions.each do |permission|
|
24
|
+
define_method "#{permission}?" do |user_id|
|
25
|
+
user_info(user_id).dig(permission)
|
26
|
+
end
|
27
|
+
|
28
|
+
define_method "bot_#{permission}?" do
|
29
|
+
public_send("#{permission}?", config.bot_id)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
other_fields = %w[status custom_title]
|
34
|
+
|
35
|
+
other_fields.each do |field|
|
36
|
+
define_method field do |user_id|
|
37
|
+
user_info(user_id).dig(field)
|
38
|
+
end
|
39
|
+
|
40
|
+
define_method "bot_#{field}" do
|
41
|
+
public_send(field, config.bot_id)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/rubydium/mixins.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
require_relative "mixins/message_sending"
|
4
4
|
require_relative "mixins/command_macros"
|
5
5
|
require_relative "mixins/control_flow"
|
6
|
+
require_relative "mixins/other_actions"
|
7
|
+
require_relative "mixins/rights_checking"
|
6
8
|
|
7
9
|
module Rubydium
|
8
10
|
module Mixins # :nodoc:
|
@@ -10,7 +12,9 @@ module Rubydium
|
|
10
12
|
base.include(
|
11
13
|
MessageSending,
|
12
14
|
CommandMacros,
|
13
|
-
ControlFlow
|
15
|
+
ControlFlow,
|
16
|
+
OtherActions,
|
17
|
+
RightsChecking
|
14
18
|
)
|
15
19
|
end
|
16
20
|
end
|
data/lib/rubydium/version.rb
CHANGED
data/rubydium.gemspec
CHANGED
@@ -34,8 +34,8 @@ Gem::Specification.new do |spec|
|
|
34
34
|
spec.metadata["rubygems_mfa_required"] = "true"
|
35
35
|
|
36
36
|
{
|
37
|
-
"telegram-bot-ruby" => [
|
38
|
-
"async" => [
|
37
|
+
"telegram-bot-ruby" => ["~> 1.0.0"],
|
38
|
+
"async" => ["~> 2.3"]
|
39
39
|
}.each do |name, versions|
|
40
40
|
spec.add_dependency(name, *versions)
|
41
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubydium
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bulgakke
|
@@ -68,6 +68,8 @@ files:
|
|
68
68
|
- lib/rubydium/mixins/command_macros.rb
|
69
69
|
- lib/rubydium/mixins/control_flow.rb
|
70
70
|
- lib/rubydium/mixins/message_sending.rb
|
71
|
+
- lib/rubydium/mixins/other_actions.rb
|
72
|
+
- lib/rubydium/mixins/rights_checking.rb
|
71
73
|
- lib/rubydium/version.rb
|
72
74
|
- rubydium.gemspec
|
73
75
|
homepage: https://github.com/bulgakke/rubydium
|