Module: Rubydium::Mixins::MessageSending

Defined in:
lib/rubydium/mixins/message_sending.rb

Overview

Shorthand methods for sending messages in different ways.

Constant Summary collapse

MAX_MESSAGE_SIZE =
4096

Instance Method Summary collapse

Instance Method Details

#chat_action_if_provided(duration, action_name) ⇒ Object



52
53
54
55
56
57
# File 'lib/rubydium/mixins/message_sending.rb', line 52

def chat_action_if_provided(duration, action_name)
  if duration
    send_chat_action(action_name)
    sleep duration if duration.is_a? Numeric
  end
end

#chunks(string, size) ⇒ Object



9
10
11
# File 'lib/rubydium/mixins/message_sending.rb', line 9

def chunks(string, size)
  (string.length / size.to_f).ceil.times.map { |i| string[i * size, size] }
end

#reply(text, **kwargs) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/rubydium/mixins/message_sending.rb', line 90

def reply(text, **kwargs)
  send_message(
    text,
    reply_to_message_id: @message_id,
    **kwargs
  )
end

#reply_code(text) ⇒ Object



98
99
100
# File 'lib/rubydium/mixins/message_sending.rb', line 98

def reply_code(text)
  reply("```\n#{text}```", parse_mode: "Markdown")
end

#reply_to_target(text, **kwargs) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/rubydium/mixins/message_sending.rb', line 102

def reply_to_target(text, **kwargs)
  send_message(
    text,
    reply_to_message_id: @replies_to.message_id,
    **kwargs
  )
end

#send_chat_action(action, **kwargs) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/rubydium/mixins/message_sending.rb', line 59

def send_chat_action(action, **kwargs)
  @api.send_chat_action(
    chat_id: @chat.id,
    action: action,
    message_thread_id: @topic_id,
    **kwargs
  )
end

#send_many_messages(text, **kwargs) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rubydium/mixins/message_sending.rb', line 33

def send_many_messages(text, **kwargs)
  text_chunks = chunks(text, MAX_MESSAGE_SIZE)

  text_chunks.map do |chunk|
    send_message(text, **kwargs)
  end
end

#send_message(text, split: false, **kwargs) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubydium/mixins/message_sending.rb', line 13

def send_message(text, split: false, **kwargs)
  return send_many_messages(text, **kwargs) if split

  @api.send_message(
    chat_id: @chat.id,
    message_thread_id: @topic_id,
    text: text,
    **kwargs
  )
rescue Telegram::Bot::Exceptions::ResponseError => e
  data = e.send(:data)
  raise e unless data["error_code"] == 429

  retry_after = data.dig("parameters", "retry_after")
  raise e unless retry_after && retry_after > 0

  sleep retry_after
  retry
end

#send_photo(photo, action: nil, **kwargs) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/rubydium/mixins/message_sending.rb', line 79

def send_photo(photo, action: nil, **kwargs)
  chat_action_if_provided(action, :upload_photo)

  @api.send_photo(
    chat_id: @chat.id,
    message_thread_id: @topic_id,
    photo: photo,
    **kwargs
  )
end

#send_sticker(sticker, action: nil, **kwargs) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/rubydium/mixins/message_sending.rb', line 41

def send_sticker(sticker, action: nil, **kwargs)
  chat_action_if_provided(action, :choose_sticker)

  @api.send_sticker(
    chat_id: @chat.id,
    message_thread_id: @topic_id,
    sticker: sticker,
    **kwargs
  )
end

#send_video(video, action: nil, **kwargs) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/rubydium/mixins/message_sending.rb', line 68

def send_video(video, action: nil, **kwargs)
  chat_action_if_provided(action, :upload_video)

  @api.send_video(
    chat_id: @chat.id,
    message_thread_id: @topic_id,
    video: video,
    **kwargs
  )
end