rubydium 0.3.2 → 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: '06454492015a165a13f48b512e35f28d01c23d971f2e747a63db539f99a74dba'
4
- data.tar.gz: 992290fd34805a84438d848067e2281b13d6542e80671160bbf46f8699cdb353
3
+ metadata.gz: ac65f322ea7e39ffe45816fa3c6bca190d5b54cf9b9df4a924812cb75149cbef
4
+ data.tar.gz: 67adfbef15a582969df215dd3d1dce9ff3d64f72eccc6a1047451dcbb343b9a5
5
5
  SHA512:
6
- metadata.gz: ce02e7b72e9459d4fa11deb91a26a40e8987831e6169019f5c6376796effda5207dfff30ea6216b8f481439665a89987708d368109233fe0e07d46236ace3d12
7
- data.tar.gz: 2bb0b1579d55d817779ede9f4a672517a9630f2a84274901018b9d31286cf44541dc3b777cf0377ef482dca68f99bea5ce51ad0113b224a7491fa7cf3fd37cec
6
+ metadata.gz: 07fb47c76b5ea68fb15c0941bc9258dca98b97ec93efc672b42ccbb00f1a776de79c91255c4f44fa23e7b4a68b2632409b4f4c3c9e72d9f6474b48297677323d
7
+ data.tar.gz: 1e58b30bd79d21a8ce3c3dce06d3c40a57e1cb43f08f61ea17e321d3b8cbbb5b61f3f7701b045991a51e439061e01f4b6bb32ad61c644b53505306f66b539963
data/lib/rubydium/bot.rb CHANGED
@@ -43,7 +43,7 @@ module Rubydium
43
43
  @user = @msg.from
44
44
  @chat = @msg.chat
45
45
  @topic_id = @msg.message_thread_id if @chat.is_forum
46
- @replies_to = @msg.reply_to_message unless @msg.reply_to_message.id == @topic_id
46
+ @replies_to = @msg.reply_to_message unless @msg.reply_to_message&.message_id == @topic_id
47
47
  @target = @replies_to&.from
48
48
  @text = @msg.text.to_s
49
49
  @message_id = @msg.message_id
@@ -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.2"
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.2
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