rubydium 0.2.4 → 0.3.0
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/Gemfile +1 -1
- data/example/example_bot.rb +3 -3
- data/lib/rubydium/bot.rb +13 -3
- data/lib/rubydium/config.rb +1 -1
- data/lib/rubydium/mixins/message_sending.rb +10 -9
- data/lib/rubydium/mixins/other_actions.rb +27 -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: a50bfd6f358154dfca5a595b467c9c0604160877dffe0266da927a134a41360f
|
4
|
+
data.tar.gz: 3afaba84242a07fcf5f9c31df0d6eb6927e4f821d7ec4fbcfcdc12d2b0a797c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5453bb526aa757ab9011463b620079f17d67269013a347496416e623115122c6e793c2bdfd90a19b2886ecd9ba5c8924418f75341bf828315ef155f86365e179
|
7
|
+
data.tar.gz: a712f2f38cb8f47f428f9837c7283de9168e6875e5a84d584749940ca831e272874dc08b77e6b8dbe61b335ad0cda648d40baf4f8b19ee9f3cbc5dcf40bb3ef8
|
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,10 @@ 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/, "")
|
53
|
+
@topic_id = @msg.message_thread_id if @chat.is_forum
|
44
54
|
end
|
45
55
|
|
46
56
|
def handle_update
|
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
|
-
message_thread_id: @
|
11
|
-
text: text
|
10
|
+
message_thread_id: @topic_id,
|
11
|
+
text: text,
|
12
|
+
**kwargs
|
12
13
|
)
|
13
14
|
end
|
14
15
|
|
@@ -17,7 +18,7 @@ module Rubydium
|
|
17
18
|
|
18
19
|
@api.send_sticker(
|
19
20
|
chat_id: @chat.id,
|
20
|
-
message_thread_id: @
|
21
|
+
message_thread_id: @topic_id,
|
21
22
|
sticker: sticker,
|
22
23
|
**kwargs
|
23
24
|
)
|
@@ -34,7 +35,7 @@ module Rubydium
|
|
34
35
|
@api.send_chat_action(
|
35
36
|
chat_id: @chat.id,
|
36
37
|
action: action,
|
37
|
-
message_thread_id: @
|
38
|
+
message_thread_id: @topic_id,
|
38
39
|
**kwargs
|
39
40
|
)
|
40
41
|
end
|
@@ -44,7 +45,7 @@ module Rubydium
|
|
44
45
|
|
45
46
|
@api.send_video(
|
46
47
|
chat_id: @chat.id,
|
47
|
-
message_thread_id: @
|
48
|
+
message_thread_id: @topic_id,
|
48
49
|
video: video,
|
49
50
|
**kwargs
|
50
51
|
)
|
@@ -55,7 +56,7 @@ module Rubydium
|
|
55
56
|
|
56
57
|
@api.send_photo(
|
57
58
|
chat_id: @chat.id,
|
58
|
-
message_thread_id: @
|
59
|
+
message_thread_id: @topic_id,
|
59
60
|
photo: photo,
|
60
61
|
**kwargs
|
61
62
|
)
|
@@ -64,7 +65,7 @@ module Rubydium
|
|
64
65
|
def reply(text, **args)
|
65
66
|
@api.send_message(
|
66
67
|
chat_id: @chat.id,
|
67
|
-
message_thread_id: @
|
68
|
+
message_thread_id: @topic_id,
|
68
69
|
reply_to_message_id: @message_id,
|
69
70
|
text: text,
|
70
71
|
**args
|
@@ -78,7 +79,7 @@ module Rubydium
|
|
78
79
|
def reply_to_target(text)
|
79
80
|
@api.send_message(
|
80
81
|
chat_id: @chat.id,
|
81
|
-
message_thread_id: @
|
82
|
+
message_thread_id: @topic_id,
|
82
83
|
reply_to_message_id: @replies_to.message_id,
|
83
84
|
text: text
|
84
85
|
)
|
@@ -0,0 +1,27 @@
|
|
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
|
+
if maybe_message["message_id"]
|
16
|
+
Telegram::Bot::Types::Message.new(maybe_message)
|
17
|
+
elsif maybe_message["result"]["message_id"]
|
18
|
+
Telegram::Bot::Types::Message.new(maybe_message["result"])
|
19
|
+
else
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
rescue StandardError
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
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.0
|
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
|