rubydium 0.3.3 → 0.3.4

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: 4a836e00b2a9935f623cd1479c6d89b13762bd4ffde066cef1d04bcfe0cec1e1
4
- data.tar.gz: 2b9748c9038fcf520d5b7ca4459c286a0f84b5e0b00b21fe9ca7ce4fdfc7e233
3
+ metadata.gz: ac65f322ea7e39ffe45816fa3c6bca190d5b54cf9b9df4a924812cb75149cbef
4
+ data.tar.gz: 67adfbef15a582969df215dd3d1dce9ff3d64f72eccc6a1047451dcbb343b9a5
5
5
  SHA512:
6
- metadata.gz: c0a6519c38a520142f004e4c9efe3d8512cfee374458de26a26d05a4188833f0e2e6edaa27302d4275ea6419fc15052a8bf8d6297ef86c7aed2e944cc0ee174a
7
- data.tar.gz: 30611da79633ed8a061b9e906c4fd4e9e9a34c8232dbb58c4cfd1706855f7fbb72ee5530fcebb2035215455e5b53c085afdbb7f995fd8c42a340f69414aac4ad
6
+ metadata.gz: 07fb47c76b5ea68fb15c0941bc9258dca98b97ec93efc672b42ccbb00f1a776de79c91255c4f44fa23e7b4a68b2632409b4f4c3c9e72d9f6474b48297677323d
7
+ data.tar.gz: 1e58b30bd79d21a8ce3c3dce06d3c40a57e1cb43f08f61ea17e321d3b8cbbb5b61f3f7701b045991a51e439061e01f4b6bb32ad61c644b53505306f66b539963
@@ -4,13 +4,38 @@ 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, **kwargs)
7
+ MAX_MESSAGE_SIZE = 4096
8
+
9
+ def chunks(string, size)
10
+ (string.length / size.to_f).ceil.times.map { |i| string[i * size, size] }
11
+ end
12
+
13
+ def send_message(text, split: false **kwargs)
14
+ return send_many_messages(text, **kwargs) if split
15
+
8
16
  @api.send_message(
9
17
  chat_id: @chat.id,
10
18
  message_thread_id: @topic_id,
11
- text: text,
19
+ text: chunk,
12
20
  **kwargs
13
21
  )
22
+ rescue Telegram::Bot::Exceptions::ResponseError => e
23
+ data = e.send(:data)
24
+ raise e unless data["error_code"] == 429
25
+
26
+ retry_after = data.dig("parameters", "retry_after")
27
+ raise e unless retry_after && retry_after > 0
28
+
29
+ sleep retry_after
30
+ retry
31
+ end
32
+
33
+ def send_many_messages(text, **kwargs)
34
+ text_chunks = chunks(text, MAX_MESSAGE_SIZE)
35
+
36
+ text_chunks.map do |chunk|
37
+ send_message(text, **kwargs)
38
+ end
14
39
  end
15
40
 
16
41
  def send_sticker(sticker, action: nil, **kwargs)
@@ -63,9 +88,7 @@ module Rubydium
63
88
  end
64
89
 
65
90
  def reply(text, **args)
66
- @api.send_message(
67
- chat_id: @chat.id,
68
- message_thread_id: @topic_id,
91
+ send_message(
69
92
  reply_to_message_id: @message_id,
70
93
  text: text,
71
94
  **args
@@ -77,9 +100,7 @@ module Rubydium
77
100
  end
78
101
 
79
102
  def reply_to_target(text)
80
- @api.send_message(
81
- chat_id: @chat.id,
82
- message_thread_id: @topic_id,
103
+ send_message(
83
104
  reply_to_message_id: @replies_to.message_id,
84
105
  text: text
85
106
  )
@@ -1,11 +1,17 @@
1
1
  module Rubydium
2
2
  module Mixins
3
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
4
+ def safe_delete(maybe_message)
5
+ message = definitely_message(maybe_message)
6
+ return false unless message&.from&.id == config.bot_id # also returns if it's nil
7
7
 
8
- result = @api.delete_message(chat_id: @chat.id, message_id: message.message_id)
8
+ safe_delete_by_id(message.id)
9
+ end
10
+
11
+ def safe_delete_by_id(id)
12
+ return false unless bot_can_delete_messages?
13
+
14
+ result = @api.delete_message(chat_id: @chat.id, message_id: id)
9
15
  result["ok"]
10
16
  rescue Telegram::Bot::Exceptions::ResponseError
11
17
  false
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rubydium
4
- VERSION = "0.3.3"
4
+ VERSION = "0.3.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubydium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - bulgakke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-17 00:00:00.000000000 Z
11
+ date: 2023-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: telegram-bot-ruby