rubydium 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2a77cb279ad0d586b2d071f893fe2ea50189f016002c1582309698e3b04d447
4
- data.tar.gz: eda4c5570c608ecdaf31e4731808532b68cc9edd58bda94af3c1d9c08c5e665a
3
+ metadata.gz: a50bfd6f358154dfca5a595b467c9c0604160877dffe0266da927a134a41360f
4
+ data.tar.gz: 3afaba84242a07fcf5f9c31df0d6eb6927e4f821d7ec4fbcfcdc12d2b0a797c9
5
5
  SHA512:
6
- metadata.gz: ea97d3e9a465f3cd5b47e8b74896d7d60d6c5c4788d3ab967a10d52c3bde820310c4b997f52cb13d1f6da8db6b6fdc725a8efc48f4d8ff7c99ad225a8404479c
7
- data.tar.gz: adb5e1eeb8cab687442ab398f9c8d26c8f2ab7decab3ae5f3a082581e65d038ab467a146af3e0adf9882212329e4c99501487394d30279a5a38ff6df3c136184
6
+ metadata.gz: 5453bb526aa757ab9011463b620079f17d67269013a347496416e623115122c6e793c2bdfd90a19b2886ecd9ba5c8924418f75341bf828315ef155f86365e179
7
+ data.tar.gz: a712f2f38cb8f47f428f9837c7283de9168e6875e5a84d584749940ca831e272874dc08b77e6b8dbe61b335ad0cda648d40baf4f8b19ee9f3cbc5dcf40bb3ef8
data/Gemfile CHANGED
@@ -4,9 +4,9 @@ source "https://rubygems.org"
4
4
 
5
5
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
6
6
 
7
+ gem "async"
7
8
  gem "pry"
8
9
  gem "rspec"
9
10
  gem "rubocop"
10
11
  gem "rubocop-rspec"
11
12
  gem "telegram-bot-ruby", "~> 1.0.0"
12
- gem "async"
@@ -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, '').gsub(/@#{config.bot_username}\b/, '').strip
43
- @text_without_bot_mentions = @text.gsub(/@#{config.bot_username}\b/, '')
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
 
@@ -25,7 +25,7 @@ module Rubydium
25
25
  # Not necessary for basic functionality, though.
26
26
  class Config
27
27
  def method_missing(method, ...)
28
- if method[-1] == '='
28
+ if method[-1] == "="
29
29
  self.class.attr_accessor method[0..-2]
30
30
  public_send(method, ...)
31
31
  else
@@ -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,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
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rubydium
4
- VERSION = "0.2.5"
4
+ VERSION = "0.3.0"
5
5
  end
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" => ['~> 1.0.0'],
38
- "async" => ['~> 2.3']
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.2.5
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