Class: GrubY::Message

Inherits:
BaseObject show all
Defined in:
lib/gruubY/types/message.rb

Constant Summary collapse

SENDERS =
{
  animation: "sendAnimation",
  audio: "sendAudio",
  contact: "sendContact",
  document: "sendDocument",
  game: "sendGame",
  invoice: "sendInvoice",
  location: "sendLocation",
  media_group: "sendMediaGroup",
  photo: "sendPhoto",
  poll: "sendPoll",
  dice: "sendDice",
  sticker: "sendSticker",
  venue: "sendVenue",
  video: "sendVideo",
  video_note: "sendVideoNote",
  voice: "sendVoice",
  paid_media: "sendPaidMedia",
  cached_media: "sendDocument",
  checklist: "sendChecklist"
}.freeze

Instance Attribute Summary

Attributes inherited from BaseObject

#api, #client

Instance Method Summary collapse

Methods inherited from BaseObject

#[], #dig, field_names, fields, #method_missing, #respond_to_missing?, #to_h

Constructor Details

#initialize(data, api: nil, client: nil) ⇒ Message

Returns a new instance of Message.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gruubY/types/message.rb', line 41

def initialize(data, api: nil, client: nil)
  super(data, api: api, client: client)
  @from = User.new(@from, api: api, client: client) if @from.is_a?(Hash)
  @chat = Chat.new(@chat, api: api, client: client) if @chat.is_a?(Hash)
  @sender_chat = Chat.new(@sender_chat, api: api, client: client) if @sender_chat.is_a?(Hash)
  @sender_business_bot = User.new(@sender_business_bot, api: api, client: client) if @sender_business_bot.is_a?(Hash)
  @via_bot = User.new(@via_bot, api: api, client: client) if @via_bot.is_a?(Hash)
  @reply_to_message = Message.new(@reply_to_message, api: api, client: client) if @reply_to_message.is_a?(Hash)
  @external_reply = ExternalReplyInfo.new(@external_reply) if @external_reply.is_a?(Hash)
  @quote = TextQuote.new(@quote) if @quote.is_a?(Hash)
  @reply_to_story = Story.new(@reply_to_story) if @reply_to_story.is_a?(Hash)
  @entities = Array(@entities).map { |e| MessageEntity.new(e) }
  @caption_entities = Array(@caption_entities).map { |e| MessageEntity.new(e) }
  @photo = Array(@photo).map { |p| PhotoSize.new(p) }
  @animation = Animation.new(@animation) if @animation.is_a?(Hash)
  @audio = Audio.new(@audio) if @audio.is_a?(Hash)
  @document = Document.new(@document) if @document.is_a?(Hash)
  @story = Story.new(@story) if @story.is_a?(Hash)
  @video = Video.new(@video) if @video.is_a?(Hash)
  @video_note = VideoNote.new(@video_note) if @video_note.is_a?(Hash)
  @voice = Voice.new(@voice) if @voice.is_a?(Hash)
  @paid_media = PaidMediaInfo.new(@paid_media) if @paid_media.is_a?(Hash)
  @contact = Contact.new(@contact) if @contact.is_a?(Hash)
  @dice = Dice.new(@dice) if @dice.is_a?(Hash)
  @poll = Poll.new(@poll) if @poll.is_a?(Hash)
  @new_chat_members = Array(@new_chat_members).map { |u| User.new(u, api: api, client: client) }
  @left_chat_member = User.new(@left_chat_member, api: api, client: client) if @left_chat_member.is_a?(Hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GrubY::BaseObject

Instance Method Details

#accept_gift_purchase_offer(**opts) ⇒ Object



160
161
162
# File 'lib/gruubY/types/message.rb', line 160

def accept_gift_purchase_offer(**opts)
  call_raw_api("processGiftPurchaseOffer", { message_id: message_id, accept: true }.merge(opts))
end

#answer(text = nil, **opts) ⇒ Object



78
79
80
# File 'lib/gruubY/types/message.rb', line 78

def answer(text = nil, **opts)
  reply(text, **opts)
end

#answer_inline_bot_result(result_id:, query_id:, **opts) ⇒ Object



184
185
186
# File 'lib/gruubY/types/message.rb', line 184

def answer_inline_bot_result(result_id:, query_id:, **opts)
  reply_inline_bot_result(result_id: result_id, query_id: query_id, **opts)
end

#clickObject

Raises:

  • (NotImplementedError)


198
199
200
# File 'lib/gruubY/types/message.rb', line 198

def click
  raise NotImplementedError, "click() requires callback query context"
end

#compose_text_with_ai(**opts) ⇒ Object



176
177
178
# File 'lib/gruubY/types/message.rb', line 176

def compose_text_with_ai(**opts)
  call_raw_api("composeTextWithAi", { chat_id: chat_id!, message_id: message_id }.merge(opts))
end

#copy(to_chat_id:, from_chat_id: chat_id!, , **opts) ⇒ Object



116
117
118
# File 'lib/gruubY/types/message.rb', line 116

def copy(to_chat_id:, from_chat_id: chat_id!, **opts)
  call_api("copyMessage", { chat_id: to_chat_id, from_chat_id: from_chat_id, message_id: message_id }.merge(opts))
end

#copy_media_group(to_chat_id:, from_chat_id: chat_id!, , **opts) ⇒ Object



120
121
122
# File 'lib/gruubY/types/message.rb', line 120

def copy_media_group(to_chat_id:, from_chat_id: chat_id!, **opts)
  call_api("copyMessages", { chat_id: to_chat_id, from_chat_id: from_chat_id, message_ids: [message_id] }.merge(opts))
end

#delete(**opts) ⇒ Object



124
125
126
# File 'lib/gruubY/types/message.rb', line 124

def delete(**opts)
  call_api("deleteMessage", { chat_id: chat_id!, message_id: message_id }.merge(opts))
end

#download(file_name = "file.dat") ⇒ Object

Raises:

  • (ArgumentError)


202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/gruubY/types/message.rb', line 202

def download(file_name = "file.dat")
  file_id = resolve_file_id
  raise ArgumentError, "message has no downloadable media" if file_id.nil?

  f = call_api("getFile", { file_id: file_id })
  call_raw_api("getFile", { file_id: file_id }) unless f
  file_path = f && f["file_path"]
  raise ArgumentError, "file_path not found for file_id=#{file_id}" if file_path.to_s.empty?

  @api.download_file(file_path, file_name)
  file_name
end

#edit_caption(caption, **opts) ⇒ Object



88
89
90
# File 'lib/gruubY/types/message.rb', line 88

def edit_caption(caption, **opts)
  call_api("editMessageCaption", { chat_id: chat_id!, message_id: message_id, caption: caption.to_s }.merge(opts))
end

#edit_checklist(checklist, **opts) ⇒ Object



96
97
98
# File 'lib/gruubY/types/message.rb', line 96

def edit_checklist(checklist, **opts)
  call_api("editMessageChecklist", { chat_id: chat_id!, message_id: message_id, checklist: checklist }.merge(opts))
end

#edit_live_location(latitude:, longitude:, **opts) ⇒ Object



104
105
106
# File 'lib/gruubY/types/message.rb', line 104

def edit_live_location(latitude:, longitude:, **opts)
  call_api("editMessageLiveLocation", { chat_id: chat_id!, message_id: message_id, latitude: latitude, longitude: longitude }.merge(opts))
end

#edit_media(media, **opts) ⇒ Object



92
93
94
# File 'lib/gruubY/types/message.rb', line 92

def edit_media(media, **opts)
  call_api("editMessageMedia", { chat_id: chat_id!, message_id: message_id, media: media }.merge(opts))
end

#edit_reply_markup(reply_markup = nil, **opts) ⇒ Object



100
101
102
# File 'lib/gruubY/types/message.rb', line 100

def edit_reply_markup(reply_markup = nil, **opts)
  call_api("editMessageReplyMarkup", { chat_id: chat_id!, message_id: message_id, reply_markup: reply_markup }.merge(opts))
end

#edit_text(text, **opts) ⇒ Object Also known as: edit



82
83
84
# File 'lib/gruubY/types/message.rb', line 82

def edit_text(text, **opts)
  call_api("editMessageText", { chat_id: chat_id!, message_id: message_id, text: text.to_s }.merge(opts))
end

#fix_text_with_ai(**opts) ⇒ Object



172
173
174
# File 'lib/gruubY/types/message.rb', line 172

def fix_text_with_ai(**opts)
  call_raw_api("fixTextWithAi", { chat_id: chat_id!, message_id: message_id }.merge(opts))
end

#forward(to_chat_id:, from_chat_id: chat_id!, , **opts) ⇒ Object



112
113
114
# File 'lib/gruubY/types/message.rb', line 112

def forward(to_chat_id:, from_chat_id: chat_id!, **opts)
  call_api("forwardMessage", { chat_id: to_chat_id, from_chat_id: from_chat_id, message_id: message_id }.merge(opts))
end

#get_media_groupObject



188
189
190
191
192
# File 'lib/gruubY/types/message.rb', line 188

def get_media_group
  return nil if media_group_id.to_s.empty?

  call_raw_api("getMediaGroup", { chat_id: chat_id!, message_id: message_id })
end

#pay(**opts) ⇒ Object



156
157
158
# File 'lib/gruubY/types/message.rb', line 156

def pay(**opts)
  call_raw_api("sendPaymentForm", { chat_id: chat_id!, message_id: message_id }.merge(opts))
end

#pin(**opts) ⇒ Object



140
141
142
# File 'lib/gruubY/types/message.rb', line 140

def pin(**opts)
  call_api("pinChatMessage", { chat_id: chat_id!, message_id: message_id }.merge(opts))
end

#react(reaction:, is_big: false) ⇒ Object



128
129
130
# File 'lib/gruubY/types/message.rb', line 128

def react(reaction:, is_big: false)
  call_api("setMessageReaction", { chat_id: chat_id!, message_id: message_id, reaction: reaction, is_big: is_big })
end

#readObject



148
149
150
# File 'lib/gruubY/types/message.rb', line 148

def read
  call_raw_api("readChatHistory", { chat_id: chat_id! })
end

#reject_gift_purchase_offer(**opts) ⇒ Object



164
165
166
# File 'lib/gruubY/types/message.rb', line 164

def reject_gift_purchase_offer(**opts)
  call_raw_api("processGiftPurchaseOffer", { message_id: message_id, accept: false }.merge(opts))
end

#reply(text = nil, **opts) ⇒ Object



70
71
72
# File 'lib/gruubY/types/message.rb', line 70

def reply(text = nil, **opts)
  call_api("sendMessage", { chat_id: chat_id!, text: text.to_s, reply_to_message_id: message_id }.merge(opts))
end

#reply_chat_action(action) ⇒ Object



194
195
196
# File 'lib/gruubY/types/message.rb', line 194

def reply_chat_action(action)
  call_api("sendChatAction", { chat_id: chat_id!, action: action })
end

#reply_inline_bot_result(result_id:, query_id:, **opts) ⇒ Object



180
181
182
# File 'lib/gruubY/types/message.rb', line 180

def reply_inline_bot_result(result_id:, query_id:, **opts)
  call_raw_api("sendInlineBotResult", { chat_id: chat_id!, query_id: query_id, result_id: result_id }.merge(opts))
end

#reply_text(text = nil, **opts) ⇒ Object



74
75
76
# File 'lib/gruubY/types/message.rb', line 74

def reply_text(text = nil, **opts)
  reply(text, **opts)
end

#retract_vote(**opts) ⇒ Object



132
133
134
# File 'lib/gruubY/types/message.rb', line 132

def retract_vote(**opts)
  call_api("stopPoll", { chat_id: chat_id!, message_id: message_id }.merge(opts))
end

#stop_live_location(**opts) ⇒ Object



108
109
110
# File 'lib/gruubY/types/message.rb', line 108

def stop_live_location(**opts)
  call_api("stopMessageLiveLocation", { chat_id: chat_id!, message_id: message_id }.merge(opts))
end

#summarize(summary_language_code: "en") ⇒ Object



168
169
170
# File 'lib/gruubY/types/message.rb', line 168

def summarize(summary_language_code: "en")
  call_raw_api("summarizeMessage", { chat_id: chat_id!, message_id: message_id, summary_language_code: summary_language_code })
end

#unpin(**opts) ⇒ Object



144
145
146
# File 'lib/gruubY/types/message.rb', line 144

def unpin(**opts)
  call_api("unpinChatMessage", { chat_id: chat_id!, message_id: message_id }.merge(opts))
end

#viewObject



152
153
154
# File 'lib/gruubY/types/message.rb', line 152

def view
  call_raw_api("viewMessages", { chat_id: chat_id!, message_ids: [message_id] })
end

#vote(option_ids, **opts) ⇒ Object



136
137
138
# File 'lib/gruubY/types/message.rb', line 136

def vote(option_ids, **opts)
  call_api("sendPoll", { chat_id: chat_id!, options: option_ids }.merge(opts))
end